Musings of a remote software developer…

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:

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.