Building a scalable web site using AWS/Docker/Terraform: adding redundancy

In my last post I showed how to use Terraform to create an EC2 instance as well as a RDS to host WordPress.  One problem with this setup is that we only have one host inside a single availability zone.  The RDS already uses a multi AZ deployment so if the availability zone fails we will lose access to our website. To solve this we can use an Autoscaling Group to ensure that we have at least 2 instances running in separate availability zones.

We will accomplish this by adding an AutoScaling Group and an Elastic File System. The autoscaling group will be responsible for maintaining the correct number of instances and the Elastic File System will be used for content that must be shared across instances.

Elastic File System (EFS)

First we will create an EFS since its highly available within a region and can be used to store media such as images, css, and javascript that is shared by multiple instances of WordPress.

After creating the EFS we must also create a mount point within each availability zone that we wish to access it from.

We also must add a security group that the EFS will use to control access via port 2049.


Remove the resource “aws_instance” “docker” .  It is no longer required since we will be launching the instance via scaling. In addition we need to make some changes to the bootstrap script to allow EFS to be connected on startup. We also need to tell docker to use the new mount point for the content directory.

In addition a small change is required to our template definition to pass in the id of the EFS.

Elastic Load Balancer (ELB)

An ELB will route traffic in a round robin fashion to any web server that is available.  It also performs a health check so that traffic is not routed to unhealthy nodes.

Route 53

At this point we can also update our zone records to point to the ELB rather than a particular instance.  The DNS record for the ELB never changes so unlike in the last post we don’t have to worry about restarting the instances.  The Autoscaling group will register the instances with the ELB.  A DNS CNAME is used to point to the DNS entry for the load balancer.

Auto Scaling Group (ASG)

Lastly we define an ASG.  The ASG will ensure that we always have the desired number of instances.  If an instance goes offline the instance will be automatically replaced.  In addition the ASG will launch the instances in a balanced way across availability zones.

It is within the launch configuration of the ASG that we define the ec2 settings such as AMI, instance type and security groups.

After applying this configuration with Terraform you should be able to visit and view the WordPress welcome screen.  If you terminate one of the EC2 instances it should automatically replace it.  The load balancer will assure that you always route traffic to an available instance.

While this creates a great deal of resiliency it does not yet address the issue of elasticity. We need to be able to respond to additional demands when a popular new post is made to the WordPress site.  I will cover how to do that in the next post.

 Continued in: Building a scalable web site using AWS/Docker/Terraform: adding elasticity

You may also like...

1 Response

  1. August 13, 2017

    […] Adding Redundancy In this part we will work on improving the redundancy of our site. […]