Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Beanstalk: How would I run an ebextension command on the worker tier only?

I have an elastic beanstalk application that utilises both the web tier and the worker tier. Jobs are offloaded onto the worker tier from the web tier via SQS to keep the web-facing servers speedy. Both environments use the exact same codebase, and use an RDS instance under them.

I need to run a cron job on the leader server of the worker tier. I've created a .ebextensions folder with a file called crontab in it as follows (it's a Laravel web app):

* * * * * root php /var/www/html/artisan do:something:with:database

Then, I've created a file called 01cronjobs.config, which updates the environments crontab under root as follows:

container_commands:
  01_remove_old_cron_jobs:
  command: "crontab -r || exit 0"
  02_cronjobs:
  command: "cat .ebextensions/crontab | crontab"
  leader_only: true

.. all good. Now, I want to deploy this to EB using the eb deploy command. However, I only want the worker tier to take on the cron job, as we can only have one server run the crons throughout the group.

Is there a way to tell the ebextensions config file to only run the config command on the worker tier? Something like worker_only: true would be great here, but it doesn't seem to exist.

Can anybody provide some insight on how I might achieve this? Thanks.

like image 214
ArkadePaulG Avatar asked Feb 10 '15 06:02

ArkadePaulG


People also ask

What is the worker environment used for in Elastic Beanstalk?

Worker environments run a daemon process provided by Elastic Beanstalk. This daemon is updated regularly to add features and fix bugs. To get the latest version of the daemon, update to the latest platform version.

What two types of environments can be created when using Elastic Beanstalk?

In AWS Elastic Beanstalk, you can create a load-balanced, scalable environment or a single-instance environment. The type of environment that you require depends on the application that you deploy.


1 Answers

  • Set an Environment Property like "tier=worker". Elastic Beanstalk --> Application --> Environment --> Configuration --> Software Configuration.
  • Use the "test" attribute of the "command" key to test for this property, so the command only get executed when the environment property is set.

Sample from the AWS doc:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

commands:
  python_install: 
    command: myscript.py
    cwd: /home/ec2-user
    env: 
      myvarname: myvarvalue
    test: '[ ! /usr/bin/python ] && echo "python not installed"'
like image 175
Frank Avatar answered Nov 12 '22 00:11

Frank