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

In order to cope with increased demand we need a method to add additional capacity when required.  To do this we can utilize cloud watch alarms to trigger our autoscaling group.  By having additional nodes in the web tier we can increase our capacity.  When the demand tapers off we can remove nodes. We can also use Cloud Front to distribute our images, javascript and css to edge nodes that are closer to the users.

Autoscaling

First we will add two policies. These policies will result in a change in capacity of +2 nodes or -2 nodes.

We can then use two alarms to trigger the autoscaling policy.

When the CPU Utilization exceeds 80% on average across all our nodes we will add 2 additional nodes.  When the CPU Utilization falls below 25% on average we will remove 2 nodes from the pool.

We can test that this works by using a utility called stress.  SSH into both of your webnodes and run

This will bring the cpu on both nodes to 90%.

Logging back into the Amazon console you should see the additional nodes added

Once you terminate the stress program the instances will terminate next time CloudWatch triggers the scale down policy.

CloudFront

Lastly we will bring CloudFront into the picture.  CloudFront is a Content Delivery Network.  The idea is that static content such as images can be serviced from a web endpoint that is closer to the user.  This reduces latency to load the asset as well as relieves the pressure off of your web server. The CDN will load the images directly from your origin if they are not currently present in the cache.

The CDN api requires a lot of values that I haven’t listed here.  Please see the code on GitHub for a complete example.

The CDN will pass a header X_CDN every time it makes a request. This is to prevent an endless loop when we redirect calls for images to the CDN. When the header is present the webserver will return the image to the CDN rather than a redirect.

The above rewrite rule added to the .htaccess will force all calls to the wp-content/uploads directory to be redirected.

The above line was added to the bootstrap.tpl and will add the rewrite rule to the .htaccess file if it doesn’t exist.  We need to check to see if its present as the .htaccess file lives on the EFS and only needs to be updated the first time its created.

Running terraform apply will update the website with this new configuration.  Since the launch configuration has changed go ahead and terminate any running EC2 instances via the console.  The autoscaling group will restart them running the new bootstrap. Once the CDN is provisioned you should see the images that you have uploaded to the Media Library start appearing.  In most cases the CDN does take about 15 minutes to fully setup so be patient.

An astute reader may notice that this approach of using URL rewrites results an additional round trip for the client.  The client must retrieve the original request from the server and then be redirected to the CDN.  Its beyond what I planned to cover in these blog posts but it is possible in Word press to use a plugin to force the URLs to be rewritten as the page is rendered so as to avoid the extra round trip.

Code for this section can be found at https://github.com/jeffellin/ellin.com/tree/master/wpdemo/ellin6

 Continued in: Building a scalable web site using AWS/Docker/Terraform: odd and ends

You may also like...