How to Create an Auto-Scaling Group of EC2 Instances for High Availability!

Chanel
Nerd For Tech
Published in
10 min readDec 22, 2022

We are going to use a real world example to help us understand the title and what is really going on. Imagine a big company like Nike has announced that they are releasing a sneaker that everyone has been waiting in anticipation for or a huge artist like Beyonce has announced that her ticket sales are going live on her website on a specific day. Naturally we can infer that traffic on these websites will skyrocket from the normal traffic they see on a daily basis. This is what auto scaling can help with.

If Nike and Beyonce housed their websites within an EC2 instance and attached an auto-scaling group to accommodate a sharp rise in traffic, it could help prevent their website from crashing! This is why auto-scaling is important and I’m going to show you step by step how to create an auto-scaling group for EC2 instances!

*Disclaimer: All examples are fictitious and the author does not represent Nike, Beyonce, the Beyhive or any entities affiliated with such organizations*

What you’ll need to get started:

  • Amazon Web Services account

Step 1: Create a VPC with CIDR 10.10.0.0/16

A VPC or virtual private cloud is a logically isolated portion of the cloud within a region, so basically your own little part of the internet. Each VPC then has its own block of IP addresses called a CIDR block. CIDR stands for Classless Inter-Domain Routing which is the notation for describing blocks of IP addresses. If you don’t know, every device that uses the internet needs an IP address from your iPhone, Apple Watch, your smart refrigerator and your ring door bell system…everything.

First we are going to create a VPC in the AWS management console. Simply search “VPC” in the search bar and click on VPC. Then on the left hand side click “Your VPCs”. You will see a default VPC already there but we are going to create our own by clicking “Create VPC”.

For the VPC, we are going to have the following settings. You can choose any name you’d like but make sure that the IPv4 CIDR is 10.10.0.0/16.

VPC Settings

Lastly, go to the “Actions” tab and drop down and select “Edit VPC Settings” and scroll down and click the check box to enable DNS hostnames and click save.

Step 2: Create 3 Public Subnets

Public subnets are created within availability zones that are accessible to the internet. This means one subnet can’t span across multiple availability zones but you can spread your workload across multiple availability zones using multiple subnets.

For example Beyonce can have a VPC in the US East (N.Virginia region) but within that region she’s going to want to have different subnets in different availability zones so if one is experiencing too much traffic another subnet will be accessed so that’s why we are going to create 3 public subnets.

On the same page click on “Subnets” on the left hand side. There will be default subnets already but we are going to create our own by clicking “Create Subnet” in the upper right hand corner.

Subnet Creation

We have to first attach our VPC to the subnet so we will select our VPC…

We are able to create all three subnet at the same time. Make sure after you enter the information for the first subnet, in the lower left hand corner just click “add new subnet” and not the orange “create subnet” button yet. We will use the following settings for our 3 subnets…

Subnet #1:

  • Subnet name: Public-1A
  • Availability Zone: us-east-1a
  • IPv4 CIDR block: 10.10.1.0/24

Subnet #2:

  • Subnet name: Public-1B
  • Availability Zone: us-east-1b
  • IPv4 CIDR block: 10.10.2.0/24

Subnet #3:

  • Subnet name: Public-1C
  • Availability Zone: us-east-1c
  • IPv4 CIDR block: 10.10.3.0/24

On the creation of the last subnet now we can select “Create subnet

You should get a message that you’ve successfully created 3 subnets.

Lastly we want to go into each of these subnets and click the box that says “enable auto-assign public IPv4 address”. We will do that by clicking the “actions” tab and clicking “Edit subnet settings”. Perform this for all 3 subnets.

We want the resources in our public subnets to be able to reach the internet so we have to create what’s called an Internet Gateway. If we had any private subnets we would us a NAT Gateway to enable our private subnets to reach the internet.

We will select “Internet gateways” on the left hand side and then click “Create Internet gateway” (again there will be a default gateway already populated, create your own).

Creation of Internet Gateway

Name your internet gateway and then select the orange “Create internet gateway”…

Once you create the Internet Gateway it will immediate notify you to attach to a VPC. How is Beyonce’s fans going to get her tickets if her website can’t access the internet! We have to attach BeyoncesIGW to BeyoncesVPC.

In the drop down select the VPC and click “Attach internet gateway”.

Our route table was automatically created but remember the internet gateway is how we reach the internet and we have to add that to the route table.

It’s almost like having a laptop (device to reach the internet) and a wireless router in your home. You can have both but until you connect your device to the router you can’t access the internet.

We will add the internet gateway to the route table by clicking “Route tables” and then selecting our route table. You can find it by looking for your VPC name. If you can’t see the whole name just select it and look at the details of the whole name.

With our route table selected click on “Routes” and select “Edit routes

Select “Add route” then Input 0.0.0.0/0 (all IP addresses), then choose your IGW and save the changes.

Adding IGW to Route Table

Step 3: Create an Autoscaling group

First we are going to create a launch template. In the EC2 dashboard under Instances select “Launch Templates” then click the orange button “Create Launch Template

Leave all settings default and change the following:

  1. Launch Template name: Your Template Name
  2. Application and OS Images (Amazon Machine Image): Quick Start > Amazon Linux
  3. Instance type: t2.micro (free tier eligible)
  4. Key Pair: Generate a new key pair (RSA/.pem)
  5. Network Settings: Create a Security Group by attaching your VPC and adding inbound rules for HTTP as follows…
Inbound Security Group Settings
Under Advanced network configuration select enable

6. Advance Details > User Data:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
EC2AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
echo '<center><h1>This Amazon EC2 instance is located in Availability Zone: AZID </h1></center>' > /var/www/html/index.txt
sed "s/AZID/$EC2AZ/" /var/www/html/index.txt > /var/www/html/index.html

7. Click orange “Create launch template

Successful Launch Template

On the EC2 dashboard we are going to select “Auto Scaling Groups” all the way on the bottom on the left side and then click the orange “Create Auto Scaling Group”. Name your auto scaling group, select the template you just made and click “next”.

The default network may be selected so make sure to select your VPC and then select the 3 public subnets we created earlier and click “next”.

For the next screen leave everything default and click “next”.

Since Beyonce is very popular we want to make sure when she puts her tickets on sale, the EC2 instances that house her website can handle the traffic so the minimum will be set at 2 instances and when traffic increases the maximum it will scale up to as much as 5.

Click next until you reach the last page which is the review page and click “Create Auto Scaling group” and you should get a confirmation page and if you check the EC2 dashboard you will see that we have 2 instances provisioned because that was the minimum number we wanted.

Our instance state is showing as running and our status check is in the green as well but you can grab the public IP’s from each instance just to make sure the information populates correctly.

Step 4: Create an Application Load Balancer

Before we create the Application Load Balancer we need to create a target group. In the EC2 dashboard on the left hand side we are going to scroll all the way down and under “Load Balancing” select “Target Groups” and click the orange button “Create target group”.

Leave the target type as “Instances” choose a target group name, Protocol and Port will be HTTP & 80, select your VPC, and then click “next”.

Don’t select the instances just select “Create target group” in the lower right hand corner. Go back to the left hand side under “Load Balancing” and select “Load Balancers” and choose “create” under application load balancer (ALB) with the following settings…

Scroll down and make sure to select your VPC, under mappings select all 3 of your public subnets, remove the default if applicable and select your security group that you made.

Select target group which is also on port 80

After selecting the default action for the listeners and routing section, scroll to the bottom and select “Create Load Balancer” in the lower right corner.

We now need to connect the Load Balancer to the auto scaling group. Click “Auto Scaling Groups” and click the name of your auto scaling group.

Scroll down to Load Balancing and click “edit” and choose the following and select update…

If we click Target groups and click on our target group name we should see two healthy instances running and if they’re not healthy there is a configuration problem.

Healthy Instances

Now I know you’re wondering but how does this actually work. We are going to simulate and EC2 instance going down to see what happens. Going over to the EC2 dashboard I am going to purposely terminate one of the instances.

Go back and select “Auto Scaling Groups” and select “Activity”.

In the activity tab it not only tells you that an EC2 instance was taken out of service after it was discovered to be terminated or stopped during a health check but a healthy one was launched to replace it. Why because our minimum instances we put in the settings was 2 Instances running at all times!

We can also go back and check our target group. It states that we have 2 healthy instances again. Remember our zones were in 1b and 1c. Once us-east-1c was terminated, the auto-scaling group provision another EC2 instance but in the 1a region.

If you’ve made it this far thank you so much for sticking with me and if you’d like to follow more of my journey please connect with me on LinkedIn!

Thank you, Chanel

--

--

Chanel
Nerd For Tech

Cloud/Devops Engineer | AWS | Terraform | Linux 🐧