Saturday, 14 October 2017

Wordpress AWS Part 6 - ebextensions Folder

The ".ebextensions" Folder


When Beanstalk deploys an application, here's what happens...

1. Extract zip file (i.e. the deployed application) into "/var/app/ondeck" in the EC2 instance.

2. If extracted contents include a ".ebextensions" folder, contents of that folder will be processed.

Contents of the ".ebextensions" folder allows us to issue commands to the EC2 instance after it is created. In our case, we want our EC2 instance to auto-mount the EFS. We are basically trying to issue these 4 commands in the EC2 instance.

# sudo mkdir /wpfiles
# sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-574ca06e.efs.ap-southeast-2.amazonaws.com:/ /wpfiles
# sudo chown webapp:webapp /wpfiles
# sudo -u webapp ln -s /wpfiles /var/app/ondeck/demo-app-files

3. If contents of ".ebextensions" folder are processed without errors, "/var/app/ondeck" will be renamed to "/var/app/current", which is the document root of the Apache server.

Let's try it out....

Creating our simple application...
In this example, the compressed file will be called "DemoApp-v1.zip".
Click "Upload and Deploy".
Choose the compressed file, click "Deploy".
Deployment in progress...
Deployment success.
The EC2 instance.
Notice that the EFS is mounted, sym-linked, ownership changed.
Notice that "/var/app/current/" contains our "index.php" file.
Point to note:
The contents of the ".ebextensions" folder will be executed everytime the application is deployed, not only when the EC2 instance starts. When Beanstalk creates an EC2 instance for us, it will auto-deploy our application, and run the codes in the ".ebextensions" folder. Let's say we made changes to our application, and we want to deploy the updated application. We zip it up, including the ".ebextensions" folder, and deploy. The contents of the ".ebextensions" folder gets executed again. If the codes in the the ".ebextensions" folder does not include checks like "if folder '/wpfiles' does not exist, then 'sudo mkdir /wpfiles', else do nothing", it is going to encounter an error, and cause the deployment to fail.
For the curious, click here to download "DemoApp-v1.zip".

Part 1 - Overview
Part 2 - RDS
Part 3 - EFS
Part 4 - Key Pairs
Part 5 - Beanstalk
Part 6 - ebextensions Folder
Part 7 - Deploy Wordpress
Part 8 - 4000 Concurrent Connections