Building a scalable web site using AWS/Docker/Terraform: adding RDS and WordPress

In my last post I talked about using Terraform to use infrastructure as code principles to create a VPC environment to host a WordPress site. In this article we will deploy an RDS and an EC2 instance to host WordPress.

The RDS will live in two availability zones in US east.  This will provide redundancy if the AZ hosting the primary replica is lost.


For RDS we must create a subnet group to enumerate the availability zones we wish to host our database in. Once that is done we can create the MySQL instance in the already defined database security group. Lastly we want to output the hostname of the database so we can plug it into our EC2 bootstrap script.


For the EC2 instance we need to launch the instance using our desired AMI.   In addition we need to pass along the DB host to our user data script.  We use a template for the script and pass in the host name returned by terraform.

The bootstrap prepares the AMI by installing updates, installing the docker engine and starting wordpress.


Lastly we need to add a DNS record set for our EC2 instance.  We add a regular A record for the EC2 instance public IP address and an DNS Alias to handle the bare apex record

Once the terraform script is applied we should be able to navigate to and see the WordPress welcome screen.

One thing you may have noticed is that we did not use an Elastic IP Address for the EC2 instance.  This means that if we stop the instance and restart it the DNS entry for will no longer work. This is because the public IP will change after reboot.  I will show you how to deal with this by adding an ELB in the next post when we talk about scaling the number of WordPress EC2 instances.

Code for this section can be found at

While it may be possible to copy the terraform state file from the previous section into the terraform for this section I can not guarantee it will work.  It’s best to remove the resources from the previous section before proceeding.

Continued in: Building a scalable web site using AWS/Docker/Terraform: Adding Redundancy


You may also like...

1 Response

  1. August 13, 2017

    […] Adding WordPress and RDS In this part we will add our database and web tier […]