Nuts and Bolts

Everyone has opinions and thoughts. Here are some of ours.

AutoScaling CloudFormation Template with Lono

Posted by Tung Nguyen
on
May 31, 2017

In the last CloudFormation post, Generating CloudFormation Templates with Lono, the stack was intentionally designed simple to allow focus on learning and understanding CloudFormation basics. The stack was not that useful in real life. Today, I’ll walk through a CloudFormation template that is practical and useful for real life use cases: an AutoScaling App Tier.

Why an AutoScaling App Tier

An AutoScaling App Tier is an extremely useful stack because it is the standard stack to which you can deploy and run application code.

One of my first thoughts of when I first learned of EC2, AutoScaling and CloudFormation was: Why not just launch a fleet of EC2 servers and not bother with AutoScaling? It’s more work to figure out AutoScaling and I just want to get servers up and running fast. I was hesitant to take on learning AutoScaling for time concerns.

It turns out that AutoScaling is amazingly simple and quick to setup with CloudFormation. It is so simple you have little reason not to use this as the default stack to run applications. Apps should always be on an AutoScaling tier. Once you get on Autoscaling you get all of these benefits:

Ensures a highly availability of infrastructure.

Applications will self-heal. Servers will be “Cows not Pets”

If there’s a weird one off instance that is misbehaving, AutoScaling will terminate it and replace it automatically. Robots can do mechanical things like this faster than humans.

Provides infrastructure scaling knobs to adjust if needed.

Build AutoScaling CloudFormation Template

There is little point in starting the CloudFormation template from scratch. So let’s head over to the official AWS CloudFormation sample templates and grab the Load-based auto scaling sample template.

Launch the Stack

Okay, now that we’ve downloaded the template, updated it to use lono and generated the templates to the output folder, we can get ready to launch the stack.

Let’s build up the params file that is required. I’m using the default VPC and default subnets to keep things simple, but you can use any VPC and subnet you would like. Here’s the params/asg.txt parameter file:

Note these the VpcId and Subnets values need to be change for your AWS account.

Let’s create the stack now!

bundle exec lono cfn create asg

You can check the status of the stack on the CloudFormation console. You should see something like this:

The stack with all the resources: AutoScalingGroup, LaunchConfiguration, SecurityGroup, Listener, LoadBalancer, TargetGroup, SNS::Topic, 2 ScalingPolicies and 2 CloudWatch::Alarms, took only 4 minutes to create.

Confirm CloudFormation Stack Working

Check the ELB endpoint to see if the stack is up and running. Note that I had to open port 80 to the world on the ELB’s security group. You should see a simple html page like this:

Congratulations, you have successfully create an extremely valuable CloudFormation App AutoScaling Tier stack. I have found that this is one of the most commonly used stacks in real life.