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

William
9 min readFeb 28, 2023

--

Amazon EC2 Auto Scaling helps you maintain application availability and lets you automatically add or remove EC2 instances using scaling policies that you define.

AWS auto scaling of EC2 instances has many benefits, mainly better fault tolerance, availability and cost management. This lesson will cover configuring many AWS resources, including:

  • VPC’s
  • Launch Templates
  • security groups
  • key pairs
  • Auto Scaling Groups (ASG)
  • Target Groups
  • Load Balancers, and more

The steps for this will be to:

  1. Create a VPC with cidr 10.10.0.0/16
  2. Create three public subnets with 10.10.1.0/24 & 10.10.2.0/24 & 10.10.3.0/24
  3. Create an autoscaling group using t2.micro instances. All instances should have apache installed on each instance with the ability to check any random IP address and be able to produce a test page. Ensure the autoscaling group is using the public subnets from #2.
  4. The autoscaling min and max should be 2 and 5.
  5. Create an Application Load Balancer to distribute traffic to the autoscaling group.
  6. Create web server security group that allows inbound traffic from HTTP from your Application Load Balancer.
  7. Create a load balancer security group that allows inbound traffic from HTTP from 0.0.0.0/0.

Create VPC

Our first step will be to create a VPC. In the AWS dashboard, search for and open VPC.

On EC2 dashboard, select Create VPC

Select the option for VPC and more. Give the VCP a name and use a CIDR block of 10.0.0.0/16

Select 3 availability zones. In the dropdown menu, select availability zones us-east-1a, us-east-1b, and us-east-1c.

Select 3 public subnets and 0 private subnets. For the 3 public CIDR Blocks, use 10.10.1.0/24, 10.10.2.0/24, and 10.10.3.0/24.

Keep all other defaults and select Create VPC.

Our VPC has been created successfully. Select View VPC. On the VPC dashboard, select actions, then Edit VPC settings.

Under DNS settings ensure Enable DNS hostnames is checked and click save.

On the VPC console, select Subnets. Select one of our three recently created subnets. Click the Actions menu and then Edit subnet settings.

Under Auto-assign IP settings, ensure Enable auto-assign public IPv4 addresses is checked. Repeat for all three subnets.

Create A Launch Template

Next we create a launch template that contains the configuration information to launch an EC2 instance. This saves time by storing parameters so that we don't have to specify them every time an instance is created.

In the EC2 dashboard, select Launch Templates.

Select Create launch template.

Give the template a name and (optional) description.

Select Quick Start under Application and OS Images. Select a free tier eligible Amazon Linux AMI. Ensure Architecture is set to 64-bit.

Under Instance type select t2.micro.

Select a previously created key pair or create a new one for this template. Amazon documentation on creating new key pairs can be found here.

Under Network settings select Create security group. Give the group a name and description. Ensure your recently created VPC is selected from the dropdown menu.

Select Add security group rule

Create a rule allowing HTTP from Anywhere. This will allow internet traffic to the website. Also allow SSH from My IP. This will allow you to connect to the instances remotely.

Under Advanced network configuration select Add network interface.

Select the option to Enable Auto-assign public IP.

Open Advanced details and scroll to the bottom of the menu.

In the User data field we will enter data giving instructions to the instance upon creation. This code will install Apache web server as well as notify us of the local IP address of the instance when we visit the website. This will come in handy when testing our load balancer later.

Input the following in the User data field.

#!/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/local-hostname)
echo '<center><h1>The local IP Address of this Amazon EC2 instance is: AZID </h1></center>' > /var/www/html/index.txt
sed "s/AZID/$EC2AZ/" /var/www/html/index.txt > /var/www/html/index.html

Select Create launch template and the template will be successfully created.

Create Auto Scaling Group (ASG)

Next we will create an auto scaling group. An auto scaling group contains a collection of EC2 instances that are treated as a logical grouping for the purposes of automatic scaling and management. You can use scaling policies to increase or decrease the number of instances in your group dynamically to meet changing conditions.

On the EC2 dashboard, scroll to the bottom and select Auto Scaling Groups.

Give the group a name and select our previously created launch template from the dropdown menu. Click Next.

Select our previously created VPC. Under Availability Zones and Subnets, select our three public subnets from the dropdown menu. Click next.

Keep the defaults on the advanced options page and click next.

On the Configure group size menu. select a desired and minimum capacity of 2. Select a maximum capacity of 5.

Click next on add notifications.

Click next on add tags.

Finally click Create Auto Scaling Group on the review page. Auto Scaling group should now be created successfully.

Create A Target Group

Now we will create a target group for our load balancer. Target groups route requests to one or more registered targets, such as EC2 instances, using the protocol and port number that you specify.

The load balancer continually monitors the health of all targets registered with the target group and routesrequests to the registered targets that are healthy.

At the bottom of the EC2 dashboard menu, select Target Group.

Select Create target group.

Ensure Instances is selected for target type. Give the target group a name.

Select a protocol version of HTTP and port 80. Ensure the correct VPC is selected and click next.

Leave the rest default and click Create target group.

Create Load Balancer

Now we will create our load balancer.

Our load balancer will serve as the single point of contact for clients. It will distribute incoming application traffic across multiple EC2 instances, in multiple Availability Zones increases the availability of our website or application.

On the EC2 dashboard select Load Balancers.

Select Create load balancer.

Select Create under Application Load Balancer.

Give the load balancer a name. Select Internet-facing schema.

Under network mapping, ensure our VPC is selected, and check the boxes for our public subnets (us-east-1a,1b, and 1c).

Under Security groups, remove the default and select our previously created security group which allows http traffic.

Under Listeners and routing, create a listener using protocol http on port 80. In the Forward To box, select our previously created target group.

Scroll to the bottom and select Create load balancer.

Load balancer successfully created.

Now we need to ensure any instances created in the future are added to the load balancer. For that we will go to Auto Scaling Groups on the EC2 dashboard.

Select our Auto Scaling group.

Scroll to load balancing and click edit.

Check the box for Application, Network, or Gateway Load Balancer target groups, then select our target group. Click Update.

Verify ASG and Load Balancer Working Properly

Now, anytime a new instance is launched into the auto scaling group, it will be picked up by the load balancer. With everything completed, lets verify that the autoscaling group and load balancer are working properly.

On the EC2 dashboard, go back to Auto Scaling Groups.

Select our Group and click on the instance management tab.

As we configured earlier in the Auto Scaling Group settings, our ASG has automatically created two instances and their status shows healthy.

With ASG verified, lets now check our load balancer. To verify it is functioning, we will go to it’s public dns address from our browser. If working correctly, it will show the webpage for one of our servers. Once we refresh the page, we should see it sent us to a different webpage from a different server showing that the balancer is controlling the traffic.

Select load balancer from the EC2 dashboard.

Select our load balancer.

Copy the DNS name.

Visit the site in your browser, ensure you are using http and not https.

When configuring our launch template, our script was set to show the local IP address of the EC2 instance created. Take note of the address on the page.

Refresh the page a few times, the load balancer will distribute the load and take you to a different EC2 instance.

Success! Our load balancer is distributing traffic to our multiple instances!

--

--