AWS — Enable Auto-scaling for High Availability

Olabode Olugbolagun
7 min readMay 24, 2021

Case Scenario

Your organization, a retail-chain store is prepping for its biggest holiday sale, everything 50% OFF on its online website. It’s anticipating high traffic on the website and needs to make sure it will always be available with zero percent downtime.

Objective

1. Create a Launch Template

- it specifies instance configuration information. Included are the ID of the Amazon Machine Image (AMI), the instance type, a key pair, security groups, and the other parameters that you use to launch EC2 instances.

Note: Launch configuration is another feature option that can be used; however, it does not provide the full functionality for Amazon EC2 Auto Scaling such as creating version templates.

2. Create an Autoscaling Group –

- it contains a collection of Amazon EC2 instances that are treated as a logical grouping for the purposes of automatic scaling and management.

3. Create Scaling Policies

- the ability to increase or decrease the compute capacity of your application.

Prerequisites

  • A VPC with a minimum of two subnets (Multi-Availability)
  • Security Group that allows SSH and HTTP traffic

Let’s get started…..

Create a Launch Template

  1. Navigate to Services and select on EC2. Select on Launch Templates within the Instances section.

2. Create launch template

3. Enter template details

  • Add template name and description
  • Check the Auto Scaling guidance — we do plan on using the EC2 Auto Scaling feature for this template

4. Choose your Amazon machine image (AMI) and Instance type — mandatory

I selected the AMI 2 SSD option and t3.nano as my instance type. To avoid being charged for this project, I recommend just choosing one of the free instance types. Then Next.

5. Although optional, create a key pair. We will need it for when we connect to our EC2 instance.

Follow the instructions and provide it a Name: YnetKP — my example. Choose a file format, select “pem”. Once created, please save your key pair in a secure location.

Next….

6. Networking settings> Select your networking platform which will be Virtual Private Cloud (VPC) as your platform and associate your Security group.

7. In the Advanced details section, we have the option of inputting User Data. We can use it to perform common automated configuration tasks and even run scripts after the instance starts. We’ll enter a script that script creates and configures our web server.

Enter User Data details:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd

8. Once completed, Create launch template.

Create an Autoscaling group

  1. EC2 Service> Within the Auto Scaling section, select on Auto Scaling Groups.

2. Select on Create an Auto Scaling Group

3. Enter template details

  • Add a Group name

4. Select a launch template. The one we just created should be an option. And the Next.

5. Configure settings> Define your Instance purchase option. We’ll be selecting on Adhere to launch template.

6. Configure network details> Select your VPC and associate your subnets. In my example below, I’ve only added two subnets. (FYI- recommend two or more subnets to establish multi-availability). Then Next.

7. For this exercise, we will not be including a Load Balancer to be apart of our group. Leave as is, No load balancer. Also, leave the Health check selections as is.

8. In the additional settings section, check the Monitoring box, to enable group metrics collection within CloudWatch. And then Next.

9. We’ll configure the group size, specifying the desired, minimum, and maximum capacity.

Enter the following details:

  • Desired capacity — 2
  • Minimum capacity — 2
  • Maximum capacity — 5

Having a minimum of two protects us against downtime if one fails. In addition, we can scale in and out based upon needs up to the maximum

10. For Scaling policies and Instance scale-in protection, we will not enable those options. Then select Next.

11. No notifications to be added at this time. Then Next.

12. No tags to be added at this time. Then Next.

13. Perform your final review then click on Create Auto Scaling group.

Automatic scaling

Still within our Auto-scaling group section, we need to select on our Auto-scaling group we just created and create scaling policies for it. We’ll define how to scale the capacity of your Auto Scaling group in response to changing demand. This will include tracking specific CloudWatch metric.

We will add two policies for this project. One for scaling out when the CPU utilization reaches 80% and the second scaling in when the CPU utilization reaches 40%.

  1. We’ll start with the Scaling Out policy. Select your Auto-scaling group. Click on Automatic Scaling and Create dynamic scaling policy.

2. For Policy type, select Simple scaling. Give it a policy name and let’s call it “ScaleOut-AddCapacity”. And now, we need to create a CloudWatch alarm for our policy.

3. Click on Create a CloudWatch alarm. Then Next.

4. Select on metric and choose EC2 as your option followed by Auto Scaling Group

5. Using the search option, identify your Auto scaling group and select the metric.

6. On the metric and conditions page, we’ll leave everything as is except for:

  • Whenever CPUUtilization is… select on Greater/Equal
  • than… lets input 80 as our threshold value

Then Next.

7. On the configure actions page, by default a Notification will automatically be added. It is not need for the project, please remove it. Everything else is not needed. Leave as is and Next.

8. Give the alarm a name and description. I used “Simple-Scaling-AddCapcity-AlarmHigh” for both.

9. Once completed, select Next and Create alarm.

10. We now have to go back to page we were on Step 2 and add the newly created alarm.

11. In addition, let’s update the Take the action and insert 1 for capacity unit. So, when the alarm triggers based on the CPUUtilization, we require 1 unit to be added. And then select on Create.

Repeats the same steps above for adding for adding scaling in policy with some minor updates for these steps:

2. Select Simple scaling. Give it a policy name and call it “ScaleIn-SubtractCapacity”.

6. On the metric and conditions page, we’ll leave everything as is except for:

  • Whenever CPUUtilization is… select on Lower/Equal
  • than… lets input 40 as our threshold value

Then Next.

8. Give the alarm a name and description. I used “Simple-Scaling-SubtractCapcity-AlarmHigh” for both.

9. Once completed, let’s go to our EC2 instances and actually terminate and stop two instances to see if new instances will be created.

As you can see, new instances are created upon termination or being stopped.

Testing out our Scaling Policies

Now that we have completed our Launch template and associated it to our Auto-scaling group, lets go ahead and test out our scaling policy by stressing out. We’ll connect to one of our instances by SSH.

  1. Navigate to EC2 services and select on one of the available instances. Click on connect.

2. Using your window terminal of choice ( I’ll be using Windows 10 CLI terminal), enter the following commands one after another:

  • sudo amazon-linux-extras install epel -y
  • sudo yum install -y stress
  • stress -- cpu 12 --timeout 600

3. Go back to the AWS terminal and select on our Autoscaling group Activity history. We will notice our Alarm has been triggered, updating our desired capacity.

Conclusion

We have now successfully created a highly available server with zero percent downtime.

Now that you’ve completed the project, begin to breakdown, deleting everything we’ve created if you no longer have use for it.

--

--