Create an AWS Auto Scaling Group of EC2 Instances

Charles Black II
8 min readDec 26, 2022

--

A Foundational Guide on How to Design your Cloud Architecture for High Availability

Today, having a fault tolerant environment is considered a must! In the traditional IT infrastructure, there are a limited number of servers that can handle the application loads of a business operation in high-demand. Businesses now run their servers and applications 24/7. In order to meet customer demand, these systems need to be highly available, secure, and scalable based on consumer needs.

The AWS cloud service provides a ubiquitous cloud offering with more than 200 fully-featured services like AWS EC2, IAM, and VPC. With a click of button, you can easily launch compute resources as needed. Auto-scaling is a function that can detect if hardware failure may arise, and automatically launch new instances. Essentially, auto-scaling is a way to monitor business applications and increase or decreased services based on demand. In this article, will show you how to setup your cloud environment for high availability. Let’s go ahead and get started.

Prerequisites:

  • AWS Account with IAM
  • Basic Knowledge of AWS Management Console
  • Basic Knowledge of EC2, VPC, Auto-Scaling

Step 1: Create a Virtual Private Cloud (VPC)

In your AWS management console, navigate over to VPC. Easiest way is to just type VPC in the search menu. NOTE: Ensure you are in a region that has at least 3 availability zones for this tutorial (us-east-1 N Virginia or us-west-2 Oregon).

NOTE: For commonly used services, select the “star” icon to save to Home Dashboard

For the next set of instructions, it might be easier if we list out the steps.

  • Click on the orange “Create VPC” button.
  • Choose “VPC only” and enter an optional name tag for tracking.
  • Choose your IPv4 CIDR block. For the purpose of this tutorial, I will be manually entering CIDR block: (10.10.0.0/16). Adjusts Tenacy
  • Name your Tags (Optional)
  • Click “Create VPC.”
Ensure Tenancy is set to “Default” and not dedicated.
Successful creation of VPC

Step 2: Create Public Subnets

Now it’s time to create three public subnets that will eventually be separated into three different availability zones. See following CIDR blocks:

  • 10.10.1.0/24
  • 10.10.2.0/24
  • 10.10.3.0/24

Navigate over to the left dashboard and select “Subnets,” then click the orange “Create subnet” button. There will be a drop-down menu for VPCs. You want to select the one you just created.

Choose the VPC you created

Next, setup your subnet settings. I will be using the three subnets listed above. Enter names your new subnets, select a different availability zone for each subnet.

Be sure to select different availability zone for each subnet
Successful creation of subnets

Step 3: Configure Routing Table

After successful creation of a VPC, it’s automatically configured with a route table (Main) that directs network traffic to and from target/destination. If you click the orange “subnet associations” tab, you will see the three “Subnets without explicit associations” at the bottom.

Default Route Table: 3 subnets automatically associated

Step 4: Create and Configure Internet Gateway

Now we need to create an Internet Gateway to attach to our VPC. The Internet gateway is a highly available VPC component responsible for allowing communication between your VPC and the internet. Navigate over to the left dashboard and select “Internet gateways,” then press the orange “Create internet gateway” button.

Create Internet Gateway

Inside the Internet Gateway menu, click the Actions drop-down, and select “Attach to VPC.”

Select your available VPC and click “Attach Internet gateway.”

If done correctly, you will be directed to Details screen, that says you are successfully attached to VPC ID.

Successfully attached!

If your a visual like me, you trust but verify. If you click back into “Internet gateways tab, you should be able to see your tag you created listed.

“luit-week7-IG”

Step 5: Create a Security Group

A security group is responsible for controlling the incoming and outgoing traffic. Essentially, it’s a virtual firewall, that consists of a set of rules that be used to monitor and filter traffic. We need to configure the security group to allow inbound traffic from HTTP from 0.0.0.0/0 (public internet). Navigate over to the Security tab on the left dashboard, and select “Security groups.” Then click the orange “Create security group.”

Once you have the inbound/outbound rules set to “0.0.0.0/0,” click the “Create Security group” button.

Step 6: Create Launch Template and Auto Scaling Group

A launch template is a prepared Amazon Machine Image (AMI) configuration that contains; instance type, key pair, security group, and user data for bash scripts. Before creating the auto scaling group, we will configure a launch template. Navigate over to EC2 console and click the “Launch Templates” tab.

Choose a launch template name and select an AMI. For tutorial purposes, I will using the Amazon Linux 2 AMI with 64-bit architecture.

T2.micro instance type (Free tier eligible)

Choose your key pair. I created a new one called “luitweek7 key pair.”

Save in secure place

Under Network settings, ensure to select the security group you created under “common security groups.” Hit the “Advanced network configuration” drop-down menu, and make sure your security group is selected.

NOTE: Be sure to select the correct Security Group Associated with Launch Template

Leave the default settings for Storage (volumes) and Resource tags. Scroll down to “Advanced details,” and click the drop-down menu. Scroll all the way to the bottom for “User data” text block. We will entering bash script data here. Data in this text block will be executed upon launch of the EC2 instance.

Script will install and start Apache web server

You should be able to view your launch template

Successful Launch Template

Step 7: Create Autoscaling Group

Now that our launch template is created, navigate over to left dashboard to “Auto Scaling Groups.” Click the Orange “Auto Scaling group” button. Choose a group name and select the launch template you created, then click “Next.”

Under “Choose instance launch options” in the network settings, you will need to select the VPC you created, along with all the subnets associated. Then click “Next.”

Next, move down to Configure advanced options. Load balancer helps distribute incoming traffic across instances, to ensure your services stay available. Select “new load balancer.” Select a name and connect to the right VPC. Select the subnet and availability zones you want to configure wit the load balancer.

Under listeners and routing, select a target group.

Under “Health checks,” check the “Enable group metrics collection within CloudWatch” option. Click the “Next” button.

Next, we are in the “Configure group size and scaling policies.” The autoscaling settings from minimum to maximum should be 2 and 5. This configuration gives Amazon EC2 the range that instances can be adjusted, based on demand.

Select Target tracking scaling policy

The notifications and tags sections you can leave default settings. Scroll all the way to the bottom and select “Create Auto Scaling group.”

After successful creating of auto scaling group, you will have two EC2 instances that were launched. Navigate to the “Instances” tab to see.

All Green…. “Running” = Success

Go to your instances, copy and past the public IPv4 address into your browser. Verify that you see the Apache Web Server test page.

Test Page Successful!

NOTE: Screenshot below is a test page with the script for availability zone is located. You may need to terminate your EC2 Instance, re-create your launch template with AWS AZID, and allow your auto-scaling configuration launch new EC2 instances, with the updated “User data” field.

Availability Zone ID

Your test page will now look like this.

This image is confirmation that your load balancer is created correctly. Also, you can keep refreshing the page to change the availability zone (us-west-2a/us-west-2c)

Thank you again for taking the time to read the article. This was definitely a challenging but cool project. Continue to follow my journey, and don’t hesitate with connect with me on my LinkedIn (Charles Black II). I look forward to sharing more of these medium posts.

--

--

Charles Black II

Founder and CEO of Made It Digital. Business | Technology | Personal Growth