Autoscaling EC2’s in CloudFormation

CloudFormation is an AWS service that lets you model, provision, and manage AWS and third-party resources by treating infrastructure as code.

It’s similar to ordering a gourmet sandwich. You basically have three options:

You can find these sample templates by AWS HERE.

When you create a template (sandwich) in CloudFormation, you are making things called, “Stacks” (The ingredients). Each of these contain the codes for the services and structures you need.

These “Stacks” all work together to execute the services that you need.

In this project we will customize a sample template from AWS. We will do that by creating an Autoscaling Group with EC2 instances in two public Availability Zones. These Sample Templates can be found HERE.

Some of this will be customized by editing the code (you will want an HTML and script editor of some kind — I use BBEdit), but most of this can be done from the console.

Extra Deets:
* We will be using "t2.micro" EC2 instances
* Our EC2’s will use Apache as the webserver (This is preconfigured on the template)
* The instance minimum will be 2, with a max of 5
* We will use a default VPC

Let’s begin by going to the services page (mentioned above), and clicking on “Amazon Virtual Private Cloud”.

As you look through the options, there will be several template options, but we will choose this one (Note the description) and click, “Launch stack”.

Before we actually launch our stack let’s open it up in Designer (This option is on the bottom right side of the page), and make a few edits to the code.

Change this to, “t2.micro”.
Change this to, “2”.
Change these to, “2” & “5”.

Copy all the text in the Designer log, and save it as a .JSON or .YAML file.

Click “back” on your browser and then select, “Template is ready” and “upload a template”.

This is where you will upload your edited file, and begin launching it!

The next few pages should let you name your Stack, and then you can double check the parameters we set in the code. Click “Next”. Leave most things as the default, and launch the Stack.

THIS MAY TAKE A FEW MINUTES, so go make a sandwich and come back…

When the status changes from this…

To that…

Your Stack has been created!

Now, let’s go ahead and make sure the details are configured to our specific needs. The first thing I want to do is check that our Autoscaling Group is running.

Looks Good!

Then lets navigate to our EC2's…

Make sure the security groups are running with the right permissions…

I had to add the last one

Next, lets make sure our EC2’s are running apache…

Everything here looks good!

Finally, let’s make sure our Autoscaling is doing what we want it to.

We are going to make sure our Minimum and Maximum launch settings are where we want them (Min 2, Max 5). We will then terminate an instance and make sure a new one gets created.

Again, looks good!

After a few minutes, and a refresh, a new Instance is running again!

Great job, it looks like it is all working according to our needs! Now, just make sure to go back through a delete any services you don’t plan to use.

Thanks for reading!

#CI/CD #EC2 #Autoscale #Cloud #CloudFormation #Stacks



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store