Implementing Auto Scaling on AWS: A Step-by-Step Guide
Tags: aws, devops, awsautoscaling, cloud
Published: 2023-06-18 20:17:47

## Introduction:
In this article, you will learn the process of implementing auto scaling on AWS, which allows you to automatically adjust the number of instances in your application based on demand. You will be guided through each step, accompanied by explanations and helpful screenshots, making it easier for you to follow along. Additionally, we will cover the essential concepts of launch templates and load balancing, ensuring a comprehensive understanding of the topic. Join us as you delve into the world of auto scaling and empower your AWS infrastructure for optimal performance.

## Prerequisite:
Before proceeding with this tutorial, it is assumed that you have the following prerequisites in place:

1. AWS Account: You should have an active AWS account that allows you to access and manage AWS services.

2. Basic Understanding of EC2 Instances: It is recommended to have a basic understanding of Amazon EC2 (Elastic Compute Cloud) instances, including how to launch and manage them.

3. Familiarity with Availability Zones: It is beneficial to have knowledge about AWS Availability Zones, which are distinct physical locations within an AWS Region designed to provide high availability and fault tolerance.

Alright let's jump in...

**Step 1: Sign into the AWS console**
To begin, sign into the [AWS console](https://aws.amazon.com/console/) using your credentials.

**Step 2: Search and select EC2**
In the search bar, type "EC2" and select it from the dropdown menu.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyc7z52dpm88kkwv7uxg.png)

**Step 3: Create an Auto Scaling Group**
Scroll down on the left-hand side and click on "Create Auto Scaling Group".

**Step 4: Configure Auto Scaling Group**
Provide a name for the Auto Scaling Group and create a launch template. In the diagram below, we have chosen the name 'demo-autoscaling-group'.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9xg5r9d177okp5ryila.png)



**What is a launch template?** 
Think of a launch template as a blueprint for creating EC2 instances. Instead of manually specifying configurations such as AMIs, instance types, and security groups each time you create an instance, a launch template allows you to define these configurations in advance. This saves you time and provides flexibility, as you can have multiple versions of the template to suit different needs. With a launch template, you can easily create instances with pre-defined configurations, eliminating the need for repetitive manual configuration.

**Step 5: Create a Launch Template**
Click on "Create Launch Template" to open a new tab. Enter a template name and description.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wzwyic2ndfkjwx7nfcj.png)

**Step 6: Customize the Launch Template**
Select an AMI image for your EC2 instance and choose an instance type. Create a security group to control inbound and outbound traffic.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/584fz2uza1mrxya22a9u.png)

In the provided image, I have personally selected the Ubuntu AMI


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5cnh14nmiuvrk1wa63d.png)

In the image above, I have selected the t2.micro instance type, which is suitable for this demo and falls under the free tier usage.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztk6lecszib3t05qfz8r.png)


In the image above, we click on "Create a key pair". A key pair is essential as it enables SSH access into our instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bne4tn2yoo50ynbdxzw4.png)

Enter a name for your key pair, and click "Create". This action will prompt the download of a .pem file. It is recommended to download the file to a directory that you can easily access.

Next, scroll down to network settings:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2k666487zyvh038fxab.png)

We have opted to create a new security group, which acts as a firewall, controlling the network access to our server/instance.

In the image above, we have provided a name for our security group, entered a description, and selected the VPC where the server/instance will be created.

Next, we add the rules for our security group

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzv4mbuvjw6a4wchs2ub.png)

In our security group configuration:

Rule 1 allows SSH access from our specific IP address, using the default port 22.

Rule 2 permits internet access over the HTTP protocol (port 80) from any source.

Rule 3 does the same as Rule 2 but for HTTPS (port 443).

Now, let's proceed to the next steps:

Scroll down to the "Advanced details" section. 

Refer to the image (Image 1) for visual guidance.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/op8cus25lyi8yqya6vgu.png)


Click on the dropdown arrow to expand and reveal more options.

In the expanded section, locate the "User data" field. 

Copy and paste the following script into the "User data" field

```javascript
#!/bin/bash
# Update package repository
apt-get update

# Install Apache web server
apt-get install -y apache2

# Retrieve instance ID
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

# Create the HTML file with custom content
cat <<EOF > /var/www/html/index.html
<!DOCTYPE html>
<html>
<head>
  <title>Welcome to my website</title>
</head>
<body>
  <h1>Hello, world!</h1>
  <p>This is my awesome demo launch template website</p>
  <p>Instance ID: $INSTANCE_ID</p>
</body>
</html>
EOF

# Start the Apache service
service apache2 start
```

**Note:** The script provided in the user data section is optional and serves as an example. It is designed to configure the launched instances from your template with a basic Apache web server and a custom HTML page. Feel free to modify this script to meet your specific requirements. You can install additional software, configure different settings, or even add your own custom content. The purpose of including this script is to demonstrate the capabilities of user data in customizing instances launched from your template.

Click the "Create launch template" button on the right side. Upon successful creation, you will see a confirmation screen with relevant details.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj5tswjjzdlllovu7938.png)


Congratulations on creating the launch template! Now, let's continue creating the auto-scaling group using the recently created launch template.

**Step 7: Return to Auto Scaling Group Creation**
Go back to the previous tab and click the refresh button. Select the launch template you just created. Then click next.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15obyjlme38b5oaz8w04.png)

**Step 8: Configure Instance Launch Options**
Choose the VPC in which you want to launch your resources. Select the desired availability zones.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndo4rfcq247z4w58dsy7.png)

In the image above, we have selected the default VPC. Normally, you would choose the VPC where you are launching your resources. Additionally, we have selected the availability zones US-east-1a and US-east-1b. Our instance will span across these zones, enhancing redundancy.

Click the "Next" button to move to the next step.




**Step 9: Configure Advanced Options**
In this step, we will attach a new load balancer that distributes traffic across your EC2 instances. Provide a name for the load balancer and select the "Internet-facing" option to receive traffic from the internet. Refer to the image below for visual guidance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5p0wca58k6vsnai9yeyi.png)


Now, scroll down on the same page to the network mapping area, where you will find the pre-selected availability zones (AZs). Choose the subnets within these AZs. Additionally, in the Listeners and routing section, select the "Create a target group" option to define a group of instances that will receive traffic from the load balancer. 

Refer to the image below for reference.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40i8kgn1xgncbz7afm72.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5beenjgyapazw4tv2ne6.png)


Lastly, scroll to the health check section, enable health checks and set a grace period of 300 seconds (you can adjust the duration as per your preference). The grace period allows for ignoring any health check failures immediately after instance launch, giving the instances time to stabilize before being marked as unhealthy.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55gro250rrnrgpdt5p29.png)

Click the "Next" button to move to the next step.

**Step 10: Configure Group Size and Scaling Policies**
In this step, you will specify the number of instances you want in your Auto Scaling Group. Set the desired capacity, which represents the number of instances you want to maintain at any given time. Define the minimum and maximum capacity, ensuring your Auto Scaling Group operates within your desired bounds. See the image below: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvx3w7fflfh8zrilq6rl.png)

Furthermore, we would create scaling policies based on metrics such as CPU usage. These policies determine when to add or remove instances, ensuring optimal performance and resource utilization

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00fhgeqksm5oxg4vdw59.png)

Click the "Next" button to move to the next step.

**Step 11: Add Notifications:**
This step is optional, so we would skip it for now.

Click the "Next" button to move to the next step.

**Step 12: Add Tags:**
In this step, we include tags to our Auto Scaling Group for better organization and management.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjm8nkogfwcdmmbc2zr6.png)

Click the "Next" button to move to the next step.

**Step 13: Review and Confirm:**
Before proceeding, please take a moment to review the options you have selected in the previous steps and ensure their accuracy. Once you have confirmed everything, click the "Create Auto Scaling Group" button to finalize the process.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n535kw059qgpl43pqym7.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olns6f7nd4j3y7kw2zcs.png)

If everything is set up correctly, you should see a result similar to the image below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xv5pngrkv9l5dzprm5y.png)


**Step 14: Testing the Setup**
Navigate back to the EC2 dashboard. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hb4rbsey6wthntx4xt3b.png)

Click on the instance ID to access the instance details. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gu6t76n4jd2zqywxjzyc.png)

Copy the instance's public IP address and test it in your browser. You should see the following;

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1clat7p7g8ykn6aonb06.png)

Note: Remember to use `http://public-ip` instead of `https://public-ip` when accessing the website.

Test the other instance the same way.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uccmmgxybi17i44jarma.png)


**Step 15: Testing the Load Balancer**
Access the Load Balancer section from the left-hand side menu. Copy the Load Balancer DNS and test it in your browser.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shfgq00o9pwihl396plr.png)

To observe the load balancer in action, refresh your browser multiple times. You will notice that the instance ID changes periodically as the load balancer dynamically distributes traffic between the instances created by the Auto Scaling Group.

Conclusion:
Congratulations! You have successfully implemented auto scaling on AWS. This article has provided a comprehensive guide, taking you through each step, from creating an Auto Scaling Group to testing your setup. To further explore the capabilities, you can conduct stress testing on one of your instances using the "stress" tool in Ubuntu. Refer to this link (https://manpages.ubuntu.com/manpages/lunar/en/man1/stress.1.html) for more information on how to use the tool. By doing so, you will witness the Auto Scaling Group automatically responding to increased load. Thank you for reading, and happy scaling!