Auto Scaling Group of EC2 Instances

Sheniell Sinclair
6 min readSep 1, 2022

--

In this article I’ll build a cloud environment of Auto Scaling EC2 Instances which host Apache web servers.

What is a Virtual Private Cloud? A VPC is a virtual network that resembles an on-premises data center, except it operates on AWS global infrastructure. It allows users to define their own scalable network by creating subnets (segments of the virtual network) to house and deploy virtual resources. I’ll create my very own VPC to run multiple Apache Web Servers on Elastic Compute Cloud.

What is an Application Load Balancer? According to Amazon Web Services, “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.” The load balancer will listen for incoming HTTP traffic on port 80 to distribute request across the network subnets.

What is an Auto Scaling group? One of the many services that makes running applications in the cloud cost effective. Instead of having to provision expensive resources on-premises for unpredictable workloads, you can have an Auto Scaling group monitor and maintain the amount of Elastic Cloud Compute needed for your applications. Auto Scaling groups improve the overall availability of applications.

[Before starting, Log into AWS Management Console with IAM User credentials that have the appropriate permissions.]

LET’S GET STARTED!

Phase 1: Create the VPCs

Step 1: Navigate to the VPC dashboard and click Create VPC. In VPC settings click VPC only. Type in the IPv4 CIDR block notation-10.10.0.0/16 (This CIDR block has a scope of 65,536 IP addresses). Then Click Create VPC.

Step 2: In VPC menu navigate to Subnets > Create Subnet. Choose the VPC we just created. Then input the IP address range of the 3 new subnets, along with the Availability Zone and CIDR block. Then click Create subnet.

  1. Public1–10.10.1.0/24 us-east-1a
  2. Public2- 10.10.2.0/24 us-east-1b
  3. Public3- 10.10.3.0/24 us-east-1c

Step 3: Select one of the newly created subnets, then click Action > Edit subnet settings. Check the box to “Enable auto-assign public IPv4 address” and Save. Do this for each subnet.

Step 4: In VPC menu navigate to Internet Gateways and click Create internet gateway. Give it a name the click Create internet gateway.

Step 5: Attach new internet gateway to VPC.

Step 6: Edit route table to accept traffic from the new internet gateway.

Phase 2: Create a Launch template for the Autoscaling group.

Step 1: Navigate to the EC2 dashboard. In the dropdown menu for Instances choose Launch template click Create a launch template. Give the template a name and click the box that states “Provide guidance to help me set up a template that I can use with EC2 Auto Scaling”.

Step 2: Choose the AMI (Amazon Linux 2 Kernel 5.10 AMI 2.0.20220719.0 x86_64 HVM gp2, ami-090fa75af13c156b4) and the Instance type (t2.micro).

Step 3: Under Network settings select a security group. In Advance network configuration select Enable in Auto-assign public IP.

Step : In the Advanced details drop down menu scroll all the way to the bottom until you reach User data. Input this script 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/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

Now view the summary and click Create launch template.

Step 5: Give your Auto Scaling group a name and click Next. (NOTE: Launch template we created is already listed throughout the configuration steps.)

Step 6: For the Network configuration menu choose the VPC that was created earlier and all of the associated subnets. Click Next and Next again until you arrive at Configure group size and scaling policies menu.

Step: Input the Auto Scale group sizes. Click Next all the way to the Review page. Then click Create Auto Scaling group.

Select the autoscaling group then check out the activity.

Autoscaling group activity
Instances launched from Auto Scaling group

Last step: Grab the public IP address of one of the newly created Instances. Open a web browser and paste the IP address in the address bar.

Successful Apache webserver Instillation

Phase 3: Create an Application Load Balancer for Auto Scaling group

Step 1: Navigate to the Load Balancers menu located in the EC2 menu, then click Create Load Balancer. Click Create for Application Load Balancer. Give the Load Balancer a name. Select Internet-facing for the Scheme and IPv4 for IP address type.

Step 2: For Network mapping select VPC and for Mappings choose all Availability Zones.

Step : Select the Security group then in Listeners and routing click Create target group. For basic configurations Choose Instances as target type, Give the Target group a name, Choose VPC and protocol version. Click Next then Create target group.

Step : Head back to the tab that has Load Balancer configurations open then add the target group. Scroll down then click Create load balancer.

Step: Go to Auto Scaling groups and click the Auto Scaling group that was created. Under Details click Edit for Load balancing. Add the Load balancer to the Auto Scaling group.

Finally: Copy the DNS name of the load balancer and put it in the address bar of a browser.

(NOTE: If you reload the web page it will bounce back and forth between the Instance that is housed in the other Availability Zone. This action is made possible by the Application Load Balancer distributing the HTTP traffic between the subnets in different AZs.)

THANK YOU FOR TAKING TIME TO READ MY ARTICLE!

Connect with me on LinkedIn:

www.linkedin.com/in/sheniell-sinclair

--

--

Sheniell Sinclair

DevOps/Cloud Engineer. Here I’ll document my learning and projects in the Cloud. www.linkedin.com/in/sheniell-sinclair