AWS Elastic Beanstalk: Deploy Hooks for a PHP App

UPDATE May 13th, 2013: This article is no longer relevant to the current Elastic Beanstalk container, but still might works for the legacy containers. Please, see my reply to Adrian F. in comments below and also this gist example illustrating how to trigger composer.phar install command during deployment.

As you know, on the 20th of March this year AWS announced PHP and Git Deployment for AWS Elastic Beanstalk which, among with others features, reduce apps deployment to single git aws.push command, i.e. similar to hows many PaaS providers works this days, but versus PaaS providers you also still have a fully customizable server instances.

In this post I’ll show you how to enhance your server instances with a custom deploy hooks.

Deploy hooks

So, what is deploy hooks?

Deploy hooks is a scripts that you write which are executed at designated points in the deployment process. This allows you to customize the deployment of your application to meet its particular needs.

For example, if your application uses Composer, then deploy hooks provide a way for you to issue php composer.phar install command to resolve and download dependencies when you deploy a new version of your application.

AWS Elastic Beanstalk apps doesn’t support app nor configuration level deploy hooks yet, but uses it internally and so we can too. See the file I found on the EC2 instance under:

/opt/elasticbeanstalk/srv/hostmanager/lib/elasticbeanstalk/hostmanager/applications/

Custom AMI

In order to tweak those deploy hooks for our needs we’ve to create custom AMI on top of the default one used by AWS Elastic Beanstalk. The process of creation of custom AMIs for use with AWS Elastic Beanstalk is pretty well documented, see Using Custom AMIs.

Simply follow the guide up to the step #6 inclusive, then put your tweaks into:

/opt/elasticbeanstalk/srv/hostmanager/lib/elasticbeanstalk/hostmanager/applications/phpapplication.rb

For example, add the following after the line #147:

logmsg "Retrieve Dependencies"
cd #{PHPApplication.web_root_dir}
/usr/bin/sudo php -d memory_limit=256M composer.phar install

Remember we creating a custom AMI which is entirely yours, so, you’re free to adjust any others server configurations as needed.

When done, continue straight from the step #8, as far I see you can skip step #7 at least at the moment of writing, because of monitoring app configured in the original AMI, which will start/restart all necessary services.

That’s it, now you can make own deploy hooks ;)

Comments