Elastic Beanstalk for wordpress
This wordpress site used to run on AWS Elastic Load Balancer with Auto Scaling that I configured manually. I realized pretty soon that I’ll be facing issues when wordpress is updated or new plugins are added/updated. Since the wordpress folder will have new files, you have to make sure that new instances launched by Auto Scaling will also contain these updates as well.
You can create a new AMI with all the updates, but then you would have to create a new Launch Configuration and edit Auto Scaling to use the new Launch Configuration. You can store wordpress installation files on S3 bucket and use a user data script to pull the files when a new instance launches, but S3 synchronization doesn’t quite do mirroring (it doesn’t delete non-existing items in source from S3 buckets) and synchronizing couple of thousand files is not very cost efficient. Thankfully, Amazon Web Services provide Elastic Beanstalk service which allows you to push updates easily.
Migrate to Elastic Beanstalk infrastructure
First thing to do is zip up your current wordpress installation. On the Amazon Linux EC2 instance, execute
$ zip peacefulan.zip -r * .[^.]*
Upload it to S3 bucket with aws cli (installed by default on Amazon Linux AMIs)
$ aws s3 cp peacefulan.zip s3://your-bucket-name/peacefulan.zip
From AWS Management Console go to Elastic Beanstalk. Click on “Create Environment” and “Create web server”.
Select PHP as Preferred configuration. I chose the latest AWS Linux AMI. Select Load balancing, auto scaling as Environment Type.
Choose S3 URI as the Source and enter the link of the previously uploaded file. I chose Batch size as 1 instance at a time because percentage based batch doesn’t really matter for a handful of instances.
Choose an environment name and be sure to check its availability.
Since this is not a new installation and there is a database in place already for wordpress, only check Create the environment inside a VPC.
I selected t2.micro, which is a free tier instance, for Instance Type. Make sure to choose a key pair that you already have. Select Basic health reporting as Enhanced service will cost extra. You can leave the Root Volume settings as default or set it to your preference.
In VPC Configuration, choose your VPC and make sure to check all Availability Zones for both ELB and EC2. ELB visibility has to be External for your site to be accessible from the internet.
For the Permissions, make sure the Instance profile is assigned a role that has a S3 privileges.
On the final screen, review your information, make any changes as necessary, and click Create.
If everything went right, you will see a large green check mark on the status screen of your environment after several minutes.
You can verify that your wordpress site is live by clicking on the environment URL.
If you are using Route53, then change your A record to point to Elastic Beanstalk environment URL at this point.
Make deployments easier
The Elastic Beanstalk setup is complete. However, zipping up the wordpress folder, downloading it and then uploading to the Elastic Beanstalk for deployment is still time consuming. We can automate the process with a small script.
Create a file in the root directory of your wordpress installation (“deploy.sh” for example) with this code.
#!/bin/bash timestamp=$(date +%s) filename=peacefulan-$timestamp version=$(date +'%m.%d.%y %T') zip $filename.zip -r * .[^.]* aws s3 cp $filename.zip s3://elasticbeanstalk-us-west-1-xxxxxxxxxxxx/$filename.zip aws --region us-west-1 elasticbeanstalk create-application-version --application-name applicationname --version-label "$version" --source-bundle S3Bucket=elasticbeanstalk-us-west-1-xxxxxxxxxxxx,S3Key=$filename.zip aws --region us-west-1 elasticbeanstalk update-environment --environment-name environmentname --version-label "$version" rm $filename.zip
Make sure you edit the script to match your environment. Change the region to match your configuration and use your own Elastic Beanstalk S3 bucket name. Note that you have to provide your application name when creating application version and environment name when updating environment.
Execute with the following command.
$ sudo sh deploy.sh
The script will create a zip file with a unique filename, upload it to the Elastic Beanstalk’s S3 bucket, create an application version with the timestamp as the version label, and then deploy it.
Watch your environment’s dashboard. The history will be updated real time with deployment information. Now anytime you want to deploy a new version, simply execute this script.
You can rollback to previous versions or delete previous versions in Application Versions section of the Elastic Beanstalk.
Update: To figure out on which EC2 instance you just updated your wordpress site, use the Instance Metadata.