A Step-by-Step Guide to AWS EC2 Auto Scaling
Published: 2023-10-31 18:05:16



Amazon Web Services (AWS) Elastic Compute Cloud (EC2) is a powerful and flexible cloud computing service that allows you to run virtual machines in the cloud. However, managing the capacity of your EC2 instances manually can be a daunting and time-consuming task. This is where AWS EC2 Auto Scaling comes into play. In this step-by-step guide, we will explore the concept of auto scaling and show you how to set up and configure AWS EC2 Auto Scaling to ensure that your application can handle varying workloads efficiently.

## What is AWS EC2 Auto Scaling?

AWS EC2 Auto Scaling is a feature that allows you to automatically adjust the number of EC2 instances in your fleet to match your desired capacity. It is designed to help you maintain high availability and cost efficiency by automatically adding or removing instances based on the defined scaling policies and conditions. This ensures that your application can handle varying levels of traffic without manual intervention.


## Prerequisites

Before you begin, make sure you have an AWS account and are logged in to the AWS Management Console.

## Sign in to AWS Console

To get started with AWS EC2 Auto Scaling, sign in to your AWS Management Console. If you don't have an AWS account, you can sign up for one [here](https://aws.amazon.com/).


## Step 1: Create a Virtual Private Cloud (VPC)

1. Go to the AWS Management Console.
2. Navigate to the VPC service.
3. Click on "Create VPC."
4. Provide a name for your VPC and specify the IP address range.
5. Click "Create VPC."

## Step 2: Create Two Subnets

1. In the VPC service, go to "Subnets."
2. Click "Create Subnet."
3. Choose your VPC.
4. Specify a name for the subnet.
5. Select the availability zone (e.g., us-east-1a).
6. Set the IP range for the subnet.
7. Click "Create Subnet."
8. Repeat the process for the second subnet (e.g., us-east-1b).

## Step 3: Create an Internet Gateway

1. In the VPC service, go to "Internet Gateways."
2. Click "Create Internet Gateway."
3. Provide a name for the internet gateway.
4. Click "Create Internet Gateway."
5. Select the newly created internet gateway and attach it to your VPC.

<u>_Step 3.1: Create a Route Table_</u>

1. In the VPC service, go to "Route Tables."
2. Click "Create Route Table."
3. Provide a name for the route table.
4. Choose your VPC.
5. Click "Create Route Table."

<u>_Step 3.2: Associate Subnets with Route Table_</u>

1. Select the newly created route table.
2. In the "Subnet Associations" tab, click "Edit subnet associations."
3. Associate both of your subnets with the route table.

<u>_Step 3.3: Edit Route with Destination_</u>

1. In the "Routes" tab of the route table, click "Edit routes."
2. Add a route with a destination of "0.0.0.0/0" and select the internet gateway you created.
3. Click "Save routes."

## Step 4: Create a Target Group

1. Go to the EC2 service.
2. Under the "Load Balancing" section, select "Target Groups."
3. Click "Create target group."
4. Provide a name for the target group.
5. Specify the protocol and port your application uses.
6. Choose your VPC.
7. Configure health checks as needed.
8. Click "Create."

## Step 5: Create an Application Load Balancer

1. In the EC2 service, under the "Load Balancing" section, select "Load Balancers."
2. Click "Create Load Balancer."
3. Choose "Application Load Balancer."
4. Configure listeners and routing as necessary.
5. Select the two subnets you created.
6. Configure security groups to allow HTTP traffic and open it to the world.
7. Attach the existing target group.
8. Click "Create."

## Step 6: Create Security Groups

1. In the EC2 service, navigate to "Security Groups."
2. Create two security groups: one for the Application Load Balancer and another for the EC2 instances launched by Auto Scaling.
3. Allow HTTP (port 80) for the Application Load Balancer security group and allow it to be accessed from anywhere.
4. For the EC2 instances, allow HTTP (port 80) and SSH (port 22) access, and make them accessible to everyone.

## Step 7: Create a Launch Template

1. In the EC2 service, go to "Launch Templates."
2. Click "Create launch template."
3. Select the Amazon Machine Image (AMI) you want to use (e.g., RHEL 9).
4. Configure the instance type (e.g., t2.micro).
5. Ensure that you enable "Auto-assign Public IP."
6. Attach the security group you created for EC2 instances.
7. Add your SSH key pair.
8. In the user data section, add any necessary startup scripts or configurations.

<u>_Step 7.1: Create an Auto Scaling Group_</u>

1. In the EC2 service, under the "Auto Scaling" section, select "Auto Scaling Groups."
2. Click "Create Auto Scaling group."
3. Choose the launch template you created.
4. Configure network settings by selecting the two subnets you created.
5. Attach the existing load balancer and target group.
6. Enable health checks and monitoring.
7. Set the desired, minimum, and maximum group sizes (e.g., 2, 1, 3).
8. Leave scaling policies as "none" for this basic setup.
9. Click through the remaining options and create the auto scaling group.


## Step 8: Test and Monitor

After configuring auto scaling, it's crucial to thoroughly test it to ensure that it behaves as expected. You can simulate traffic spikes or resource failures to see if the auto scaling group responds correctly.

Additionally, use AWS CloudWatch to monitor your instances and scaling activities. Create custom CloudWatch alarms to trigger actions based on specific metrics.

## Fine-Tune and Optimise

Once your auto scaling group is up and running, periodically review your scaling policies and configurations to optimize cost and performance. Adjust the scaling thresholds, instance types, and other settings as needed based on your application's usage patterns.

"For your convenience, I have prepared a Terraform script that fulfills the aforementioned requirements. This script orchestrates the setup of an AWS infrastructure, incorporating essential components like a VPC, subnets, an internet gateway, security groups, load balancers, launch templates, and an auto-scaling group for EC2 instances."

*Here is Git repo: https://github.com/yadanaresh/aws-ec2-autoscale.git*


## Conclusion

AWS EC2 Auto Scaling is a powerful tool for managing the capacity of your EC2 instances automatically. By following this step-by-step guide, you can set up and configure auto scaling to ensure your application can handle varying workloads efficiently, improve availability, and optimize costs. Remember that the key to successful auto scaling is proper planning and monitoring to align your application's performance with your business needs.