Elastic Beanstalk: How would I run an ebextension

2019-02-17 11:25发布

问题:

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.

回答1:

  • 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"'