Amazon Elastic Beanstalk Hooks

I’ve been working a lot with Amazon Elastic Beanstalk lately to scale our application out. I noticed that there were some nice hooks that elastic beanstalk provides in the /opt/elasticbeanstalk/hooks/ directory. I don’t know why, but Amazon never documented these hooks, but they are very useful to setup your Elastic Beanstalk environment before and/or after your application is deployed. I’ve seen pre-deploy scripts setup additional applications or services on each box (local redis cache service) or a post-deploy script to run a rake task after everything is setup (Did I mention I don’t program in PHP anymore?).

One thing I wish Amazon would do is document and lock down this feature because it is very useful. It seems that Amazon doesn’t really want to do that and the forums warn that it is undocumented and the hooks are subject to change. Well, it is still good to note what order the hooks are fired and what actually triggers them, so here’s my attempt to document all that. You can easily see this in eb log or logging into the server via ssh and checking /var/log/eb-activity.log.

Here are the events that are fired from the AMI (64bit Amazon Linux 2015.03 v1.4.1 running Ruby 2.2 (Puma) ) that I am on for ruby:

Deploy New Instance

1
2
3
4
5
6
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/preinit.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/pre.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/enact.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/post.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/postinit.
  Successfully execute hooks in directory /opt/elasticbeanstalk/addons/logpublish/hooks/config.

eb deploy

1
2
3
4
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/pre.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/enact.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/post.
  Successfully execute hooks in directory /opt/elasticbeanstalk/addons/logpublish/hooks/config.

Reconfigure Scaling

No hooks executed

Changing Environment Properties (eb setenv)

1
2
3
4
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/configdeploy/pre.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/configdeploy/enact.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/configdeploy/post.
  Successfully execute hooks in directory /opt/elasticbeanstalk/addons/logpublish/hooks/config.

Restart App Server

1
2
3
4
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/restartappserver/pre.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/restartappserver/enact.
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/restartappserver/post.
  Successfully execute hooks in directory /opt/elasticbeanstalk/addons/logpublish/hooks/config.

Did I miss any event? Please let me know in the comments and I can add it to the list.

If you want your app to deploy with files into these directories, elastic beanstalk provides a way for you to place arbitrary files on the instance and run commands on deploy via the .ebextensions config files. You can check out Danne Manne’s blog on how to do this.

It appears each hook directory is run in sorted order, so if you want something to load up before all the other hooks, prepend it with 01* such as 01script. If you want it run after the other scripts, give it a high number such as 99script.

One thought on “Amazon Elastic Beanstalk Hooks

  1. Pingback: Code Miner's journal | Changing Apache to Nginx with PHP FPM on Elastic Beanstalk – Amazon Web Services

Leave a Reply

Your email address will not be published. Required fields are marked *