Who already worked with Elastic Beanstalk knows the lack of documentation of the platform. If you need to deploy a simple application it’s straight forward, but if you need to do more complex stuff, it can be a pain in the ass.
The problem with Cron and Elastic Beanstalk begins when we are using Auto Scaling.
Generally we need only one of our machines in the cluster to run the jobs, not all of them.
We can use the
leader_only option to ensure the job will just run in the leader of the cluster,
but it’s not enough, since the leader can change over time, for example, during deploys.
To ensure all the machines crontab is wiped in every deploy we need to add
container_command (before the command to add the crontab).
Add this to your configuration file.
container_commands: 01_remove_old_cron_jobs: command: "crontab -r || exit 0" 02_cronjobs: command: "cat .ebextensions/crontab | crontab" leader_only: true
|| exit 0 is mandatory because if there is no crontab in the machine the
command will return a status code > 0 (an error). Elastic Beanstalk stop the deploy process
if one of the