How To Create An Auto-Scaling Group of EC2 Instances For High Availability

Sharon Johnson
7 min readFeb 25, 2023

--

AWS Diagram for this project: Created by Sharon Johnson

Objective:

  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.

Let go over what is:

VPC-A virtual private cloud (VPC) is a private cloud computing environment contained within a public cloud. Essentially, a VPC provisions logically isolated sections of a public cloud in order to provide a virtual private environment.

Public Subnets-A public subnet is a subnet that’s associated with a route table that has a route to an internet gateway. A private subnet with a size /24 IPv4 CIDR block (example: 10.0. 1.0/24). This provides 256 private IPv4 addresses. An internet gateway.

Internet Gateway-An internet gateway is a horizontally scaled, redundant, and highly available VPC component that allows communication between your VPC and the internet. It supports IPv4 and IPv6 traffic.

Auto Scaling-monitors your applications and automatically adjusts capacity to maintain steady, predictable performance at the lowest possible cost.

t2.micro Instances-T2 instances are a low-cost, general purpose instance type that provides a baseline level of CPU performance with the ability to burst above the baseline when needed.

Application Load Balancer-A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer.

Lets head into your AWS Console:

Step 1: Create your VPC with cidr 10.10.0.0/16

In your AWS console search VPC and create VPC

Select VPC only, name your Name Tags, everything else leave at default. At tags is optional if you want to add the tag name value.

Click on the Create VPC and you should get successfully created.

Step 2: Create your public subnets with 10.10.1.0/24, 10.10.2.0/24 and 10.10.3.0/24

Under VPC dashboard select subnets and create the subnets

Here you will need to select your VPC you just created and now name your first subnet, the Availability Zone (I’m using US East N Virginia since this is closet to me) select us-east-1a, IPv4 cidr block will be 10.10.1.0/24

Let’s continue with adding the other two subnets, click on add subnet, name the next one and use the same Availability Zone but use us-east-1b, the IPv4 cidr block 10.10.2.0/24

Create the last subnet, name it, select same Availability Zone but us-east-1c, IPv4 cidr block will be 10.10.3.0/24. Now that's done click on Create Subnet. Here you will see your created subnets

Now we need to into the Internet Gateway located in the VPC dashboard, click on Internet Gateway, name your gateway click create internet gateway.

You will get the message your was created and it will show your internet gateway ID.

Lets now attach it to your VPC your created, find the VPC your working with and attach.

Now we need go to Route Table on the VPC dashboard

Make sure to associate your IGW ID to the 0.0.0.0/0 and the 10.10.0.0/10 is local.

Now let create a security group

Click on Create Security Group

Name your security group, go down to Inbound Rules and click on add rule, the first rule drop down under type add in SSH and your source choose MY IP and now add another rule drop down and add in HTTP and source it to ANYWHERE IPV4.

Under the Outbound make sure the type is ALL TRAFFIC and the source is ANYWHERE IPv4.

Step 3: We are now going to create an auto scaling group using t2.micro instances, so let go to EC2 on the dashboard and click on launch template.

Name your launch template, click on the Auto Scaling guidance

Click on the drop down for the AMI (you can choose whatever you are comfortable using) I’m going to use Amazon Linux.

On Instance Type use the t2.micro (free tier eligible)

Create new key pair or use an existing key pair

Under Networking Settings click on “select existing security group” and make sure below you are using the correct VPC.

Now let go to the bottom and click on Advance Detail, User Data and use this script to install Apache on every instance this template launches.

Now let launch this instance

Step 4: Autoscaling, under the EC2 dashboard, click on Auto Scaling and create.

Create your Auto Scaling group name, under launch template select your launch template name, click on next.

Scroll to Network, click on your VPC you created, then under the Availability Zones and Subnets click on all three of the subnets you created.

View and click on Next

Step 5: Create a Application Load Balancer to distribute traffic to the auto scaling group.

Still working in the auto scaling group, under the configure advance options click on Attach to a new load balancer, make sure under the attach to new load balancer type is Application Load Balancer (HTTP, HTTPS) is selected, under the load balancer scheme click on Internet- Facing.

Verify that the VPC you are using is correct, under the Availability Zones and Subnets, select a single subnet for each Zone. Under the Listeners and Routing “Default Routing needs to be set at Create a target group and an instance target group with default settings will be created.

From here click on Next

Here is the configure group size and scaling polices, Desired capacity set at 2, Minimum capacity set at 2 and the Maximum capacity set at 5.

Click on Next

Now let go back to your EC2 dashboard and look for the two running instance and grab the public ip address and open a browser and paste

First IP address should show you this

Second IP address should show you this

You have now complete the objective. This will be a good time to terminate and stop anything that will cost you to continue to run.

--

--

No responses yet