Create a cron job in AWS’s Elastic Beanstalk using Django

The basics on how a cron job is configured

This article will tell you how to write a cron job in AWS’s Elastic Beanstalk (EB) using Django. For reference, my EB environment is using Python 3.7 running on 64bit Amazon Linux 2/3.1.3. It is probably different if you’re using a different version.

Step 1 — Create the file you want to run

The example file I used is up on github here. It is a simple scraper that pulls NFL’s Next Gen Statistics and pushes them to the local database. Your file will be stored in the directory <application>/management/commands/<>

That naming convention is needed, as it allows you to type python <file_name> to run that file. Just a reminder that you need the class Command(BaseCommand) in order for it to run properly.

from import BaseCommand, CommandErrorclass Command(BaseCommand):
def handle(self, *args, **options):
pass # here is where you will start your function

Step 2 — Create your crons.config file

Once your file is created, you will then create a file in the project directory .ebextensions/crons.config

This will hold all of your crons job. There is a lot to take in, so you might have to just copy and paste it in your own file (lol). In the places where I put <> it’s where you put your own values.

The first part checks if there are multiple EC2 instances, and if there are only use 1 instance for cron jobs.

The final part removes all the cron jobs, so when you reupload this it will not put the same cron job multiple times.

mode: "000755"
owner: root
group: root
content: |
INSTANCE_ID=`curl 2>/dev/null`
REGION=`curl -s 2>/dev/null | jq -r .region`
# Find the Auto Scaling Group name from the Elastic Beanstalk environment
ASG=`aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" \
--region $REGION --output json | jq -r '.[][] | select(.Key=="aws:autoscaling:groupName") | .Value'`

# Find the first instance in the Auto Scaling Group
FIRST=`aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $ASG \
--region $REGION --output json | \
jq -r '.AutoScalingGroups[].Instances[] | select(.LifecycleState=="InService") | .InstanceId' | sort | head -1`
# If the instance ids are the same exit 0
mode: "000755"
owner: root
group: root
content: |
/usr/local/bin/ || exit
source /var/app/venv/*/bin/activate
cd /var/app/current
python <whatever your python name is>
mode: "000644"
owner: root
group: root
content: |
* * * * * root /usr/local/bin/cron_<cron_name>.sh
command: "rm -fr /etc/cron.d/*.bak"
ignoreErrors: true

Step 3 — Push!

Run eb deploy, and once it’s all setup you’re good to go! That’s how you create a cron job and push it to AWS.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store