alytvynov
alytvynov

Reputation: 81

Run Elastic Beanstalk commands after deploy

I deploy Symfony application on AWS. I use Elastic Beanstalk for that. After deploy process I want to run some symfony commands : clear cache, update db, ...

So I create .ebextention with .config inside

commands:
  01updateComposer:
    command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update 1.0.0-alpha11
  cache:
    command: sudo php /var/www/html/bin/console cahce:clear --env=prod

option_settings:
  - namespace: aws:elasticbeanstalk:application:environment
    option_name: COMPOSER_HOME
    value: /root

But it seams my cache command does not work or it works in wrong moment.

Could anybody help me please ?

Updated : Actually I updated config file. In /tmp right now I see end.txt, collect.txt, dump.txt. And They are OK. But I dont have modifications on site. For example no assetics. It looks like commands run in wrong moment.

commands:
  01updateComposer:
    command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update 1.0.0-alpha11
  collect:
    command: sudo php /var/www/html/bin/console collect --nb=1 --env=prod >> /tmp/collect.txt
  assetic:
    command: sudo php /var/www/html/bin/console assets:install --symlink --env=prod >> /tmp/dump.txt
  dump:
    command: sudo php /var/www/html/bin/console assetic:dump --env=prod >> /tmp/dump.txt
  end:
    command: touch /tmp/end.txt

option_settings:
  - namespace: aws:elasticbeanstalk:application:environment
    option_name: COMPOSER_HOME
    value: /root

I also triend container_commands instead of commands. That did help me.

P.s. I have only one instance EC2. So this could not be wront instance.

Upvotes: 2

Views: 2711

Answers (2)

Chuck Le Butt
Chuck Le Butt

Reputation: 48758

If you look in your /var/log/eb-activity.log file you'll see the order that everything is being executed in. In particular you'll see one moment in the deployment where the whole project is moved folder:

[2021-03-05T19:24:43.872Z] INFO  [17486] - [Application update app-1ee2-210305_192347@12/AppDeployStage1/AppDeployEnactHook/01_flip.sh] : Completed activity. Result:
  ++ /opt/elasticbeanstalk/bin/get-config container -k app_staging_dir
  + EB_APP_STAGING_DIR=/var/app/ondeck
  ++ /opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir
  + EB_APP_DEPLOY_DIR=/var/app/current
  + '[' -d /var/app/current ']'
  + mv /var/app/current /var/app/current.old
  + mv /var/app/ondeck /var/app/current
  + nohup rm -rf /var/app/current.old

Certain commands (such as php artisan view:cache) need to be run after the project is moved, otherwise they won't function correctly.

To do this you can use Elastic Beanstalk custom platform hooks (note: only with Amazon Linux, not the new nginx based Amazon Linux 2).

In one of your later .config files, you can simply add something like:

files:
   "/opt/elasticbeanstalk/hooks/appdeploy/post/99_z_post_deploy.sh":
      mode: "000755"
      owner: root
      group: root
      content: |
        #!/usr/bin/env bash
        echo "Running post.sh script"
        # Turn on maintenance mode
        #php /var/app/current/artisan down || true

        # Run database migrations
        php /var/app/current/artisan migrate --force

This will execute any commands under the "/opt/elasticbeanstalk/hooks/appdeploy/post/99_z_post_deploy.sh" line in the "post" phase of the "appdeploy" stage. And 99_ will bring it towards the end of that (everything is executed alphabetically).

Again you can look in your eb-activity.log file to see where it's being executed. The example above will execute the shell script "post.sh".

For Amazon Linux 2 you need to use Application deployment platform hooks.

This was all a bit late, but hopefully it helps. This is also not a bad guide that I found while writing this answer: How To Use the Appdeploy Filesystem Hook. It may help further.

Upvotes: 1

Tom
Tom

Reputation: 172

I had a similar problem with logging, and I think you need this to be in the container_commands section, not the commands section.

container_commands:
  01updateComposer:
    command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update 1.0.0-alpha11
  02collect:
    command: sudo php /var/www/html/bin/console collect --nb=1 --env=prod >> /tmp/collect.txt
  03assetic:
    command: sudo php /var/www/html/bin/console assets:install --symlink --env=prod >> /tmp/dump.txt
  04dump:
    command: sudo php /var/www/html/bin/console assetic:dump --env=prod >> /tmp/dump.txt
  05end:
    command: touch /tmp/end.txt

option_settings:
  - namespace: aws:elasticbeanstalk:application:environment
    option_name: COMPOSER_HOME
    value: /root

You can see more details about my problems and solution here.

Upvotes: 3

Related Questions