High Availability With Auto Scaling

Matthew Mendez
DevOps Engineer Documentation
7 min readMay 13, 2021
  1. What is Auto Scaling?
  2. Getting started with AWS Auto scaling.
  3. Create an Auto Scaling Group.
  4. Create a Scaling Policy.
  5. Test our Auto Scaling Group
  6. Conclusion.

1. What is Auto Scaling?

Amazon EC2 Auto Scaling helps you ensure that you have the correct number of Amazon EC2 instances available to handle the load for your application. You create collections of EC2 instances, called Auto Scaling groups. You can specify the minimum number of instances in each Auto Scaling group, and Amazon EC2 Auto Scaling ensures that your group never goes below this size. You can specify the maximum number of instances in each Auto Scaling group, and Amazon EC2 Auto Scaling ensures that your group never goes above this size. If you specify the desired capacity, either when you create the group or at any time thereafter, Amazon EC2 Auto Scaling ensures that your group has this many instances. If you specify scaling policies, then Amazon EC2 Auto Scaling can launch or terminate instances as demand on your application increases or decreases.

2. Getting started with AWS Auto scaling.

Note: This demonstration assumes that you are familar with AWS EC2 instances. For information on how to set up EC2 instances, click on this link https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html

Navigate to the EC2 Management Console. On the left hand side, scroll down to Auto Scaling. Choose Auto Scaling Groups.

We can now begin to configure our Auto Scaling Group.

3. Create an Auto Scaling Group.

We can begin by clicking on Create Auto Scaling Group.

Before naming our Auto Scaling Group, we’ll have to choose Create a Launch Template first. Click create a launch template.

From here, we can name our Launch Template and give it a description. See example below.

Next we’ll need to choose our AMI. Choose Amazon Linux 2. Next select the instance type. I recommend choosing t2.micro for free tier purposes but for this demo I chose t2.nano.

Next you’ll want to create a New Key pair. Do not lose this key pair. You will need it in order to SSH into your instances. After naming your key pair, click Create Key Pair.

For Network settings, choose the default VPC. For Security Groups, choose launch-wizard-1. This is the security group associated with the default VPC. For storage setting, choose the default storage setting General Purpose SSD.

Scroll down to advanced details and click the right arrow.

Scroll down to User Data and enter this bootstrap script to start the apache webserver.

Click Create Lauch Template.

We can now go back to the Auto Scaling Group console. We can now name our Auto Scaling Group and choose the template that we just created. Click next after naming group and choosing template.

For instance purchase options, choose Adhere to launch template.

For Network, choose the default VPC.

For Subnets, choose subnets three.

Click next.

For advanced settings, I will not choose a load balancer for this demo. Click next.

Next we can configure our group size.

For this demo I chose my desired capacity to be 3, my minimum capacity at 2, and my maximum capacity at 5.

We will add a scaling policy after we configure our scaling group. Scroll down and select next.

We can select next on notifications.

We can select next on tags.

We can scroll down on review and select Create Auto Scaling Group.

We can click on our auto scaling group and confirm that we had successful launches of our instances by reviewing our activity history.

4. Create a Scaling Policy.

Let’s begin by defining an Auto Scaling Policy. A scaling policy instructs Amazon EC2 Auto Scaling to track a specific CloudWatch metric, and it defines what action to take when the associated CloudWatch alarm is in ALARM. The metrics that are used to trigger an alarm are an aggregation of metrics coming from all of the instances in the Auto Scaling group.

Lets’s now create a scaling policy.

Click Automatic scaling and click Add policy.

For this demo, we are going to choose a Step Scaling policy. We will add two policies.

One for scaling out when the CPU utilization reaches 80%.

The second for scaling in when the CPU utilization reaches 40%.

Let’s create the first one.

Choose step scaling.

Name this one scale_out.

Next click on Create a cloudwatch alarm.

You will need to select a metric.

Scroll down and choose EC2.

Next select by auto scaling group.

Choose CPUUtilization.

Click Select Metric.

Next we’ll specify the conditions.

Choose Greater/Equal threshold.

Define the threshold value of 80.

Click next.

Next we’ll need select on SNS topic.

Select new topic.

Name the topic scale_out.

Enter in your email address.

Select create topic.

Select next.

Next add an alarm name.

Name it High CPU Utilization.

Click next.

Review and click create alarm.

We can now go back to our scaling policy page and choose the alarm that we created. (NOTE: The name of the alarm should be high cpu but my Mac auto-corrected the name of my alarm.)

Under take action.

Add two capacity units when the CPU utilization reaches 80.

Scroll down and select create.

Let’s add a policy for scaling in.

Select add policy as shown below.

For policy type, choose step scaling.

Name your scaling policy “scaling in”.

Click create a cloudwatch alarm.

Select metric.

Scroll down and choose EC2.

Choose by auto scaling group.

Click CPUUtilization.

Click select metric.

Scroll down and choose Whenever CPUUtilization is lower/equal.

Define the value as 40.

Select next.

For our SNS topic, we’ll create a new topic and name it “low cpu”.

Enter your email address.

Hit create topic and select next.

Name the alarm name “lowcpu” and click next.

Preview and hit Create Alarm.

We can now go back to our scaling policy page and select the low cpu alarm that we created.

We can now add the action to remove 2 capacity units when the CPUUtilization reaches 40% or below.

5. Test our Auto Scaling Group.

Let's test out our Auto Scaling Group by stressing out.

Let’s connect to one of our EC2 instances by selecting one and clicking connect. Select ec2 instance connect and click connect. Let’s run the command sudo amazon-linux-extras install epel -y as seen below.

Next we need to install the stress command by running the command sudo yum install stress -y as seen below.

After a successful install we can now stress out our instance by running the stress command as seen below. (NOTE: running stress — — cpu 60 wasn’t enough, I ran 100 to stress it out.)

After running stress — — cpu 100 , notice the cpu utilization going way up in the graph below. This triggered an alarm in cloudwatch.

In the Auto Scaling Group console, you can click on activity and activity history and watch your Auto scaling Group launch more instances to meet the demand as seen below.

After some time, the low cpu alarm triggered and terminated the additional EC2 instances.

6. Conclusion

As demonstrated, Amazon EC2 Auto Scaling has better availability. Amazon EC2 Auto Scaling helps ensure that your application always has the right amount of capacity to handle the current traffic demand and has better cost management. Amazon EC2 Auto Scaling can dynamically increase and decrease capacity as needed.

We can delete our Auto Scaling group along with our cloudwatch alarms and any other resources that were created during this demo.

--

--

Matthew Mendez
DevOps Engineer Documentation

Documenting my journey from bartender to a career as a devops engineer