top of page

Amazon EC2 Auto Scaling Tutorial for Dynamic Scaling of Instances

Updated: Sep 19, 2022

Dynamic Scaling of EC2 instance using Auto Scaling based on CPU Utilisation

Objectives:

  • Learn to create a launch template

  • Learn to configure Auto Scaling group & create scaling policies for Amazon EC2 Auto Scaling.

  • Learn to configure Amazon SNS for email notification.

  • Learn to configure Amazon CloudWatch for monitoring CPU utilisation.

Step 1:

In EC2 Service, on the left side panel go to Instances -> Launch Template. Click on Create launch template.

ree

Name the template as MyTemplate.

ree

Select Amazon Machine Image (AMI): Amazon Linux 2 AMI (HVM)

Select Instance Type as t2.micro

Select your existing Key Pair

ree

In Network Settings select the Security Group that is created for Linux Server. Keep the rest default values as it is.

ree

Scroll down and click on Create Launch Template. Your Template is successfully created.

ree

Click on View launch templates at the bottom of new page and check whether it is created.

ree

Step 2:

Now on the left side panel go to Auto Scaling -> Auto Scaling Groups. Click on Create an Auto Scaling group.

Name it as MyASG. Select the above created launch template MyTemplate.

Keep the rest default values as it is. Click on Next in bottom right corner.

ree

In Configure Settings, under Network click on the drop down arrow and select two Subnets one at a time.

ree

Keep the rest default values as it is. Click on Next in bottom right corner.

In configure advanced options keep the default values as it is. Click on Next in bottom right corner.

Under Configure group size and scaling policies, go to Group size and configure the values Desired Capacity: 2

Minimum Capacity: 2

Maximum Capacity: 5

ree

In the Scaling policies – optional, select Target tracking scaling policy and configure the Values as:

Scaling Policy Name: MyASG Policy

Metric Type: Average CPU Utilization

Target Value: 50

ree

Click on Next in bottom right corner.

In Add Notifications, click on Add notification.

ree

Click on Create a Topic

ree

Configure the values as:

Send a notification to: MyASG-sns-topic

With these recipients: Enter your personal email address in this value

ree

Click on Next In bottom right corner.

In Add tags, click on Add tag and configure the values as:

Key: Name

Value: EC2forASG

ree

Click on Next in bottom right corner.

Review your auto scaling group. After review, scroll down and click on Create Auto Scaling group.

ree

Your Auto Scaling group is successfully created. Check whether it is

Created.

ree

The status would be initially Updating Capacity. This means that the given capacity is checked. The instances are being initiated or terminated accordingly.

With Auto Scaling Group you can monitor with use of these features. Thus, you can have better control created instances and the system.

ree

Step 3:

On the left side panel go to Instances -> View Instances. Here you can see the working of Auto Scaling group to the specified capacity.

ree

Step 4:

Open you Gmail and check for the email subjected AWS Notification - Subscription Confirmation.

Open that email and click on Confirm Subscription.

ree
ree

After confirming the subscription, close that tab and move back to the AWS console.

Step 5:

Connect to any one of the EC2 instance by selecting the EC2 instance and clicking the Connect button.

ree

Select the EC2 Instance Connect and click on the Connect button in the right bottom corner.

ree

Step 6:

In order to increase the CPU Utilization of the EC2 instance, we need to create a load on the server. This can be achieved by using Stress.

In order to use stress, we must be a root user. Run the following command to become a root user. sudo -s

ree

Now, we will install a utility called stress. This tool is designed to subject your system to a configurable measure of CPU, memory, I/O and disk stress.

Stress tools is not installed by default, you will need to install the package by using the following commands:

amazon-linux-extras install epel -y

yum install stress -y

Once installed, CPU load can be generated using Stress by running the following command:

stress --cpu 1 --timeout 300

The above command will take few minutes to complete.

ree

After the above command is completed, go to Instances in the left side panel.

Refresh the page until you see some more EC2 instances created. It may take some time.

ree

Also, you can check your gmail in which you would have received emails subjected Auto Scaling: launch for group "MyASG"

It is the SNS Notification trigger which we have created while creating the Auto Scaling Group.

ree

Move back to the Instance page. After some time, you will see that some of the EC2 instances are terminated by the Auto Scaling Group as the CPU Utilization of the application has lowered down.

ree

You will also receive email subjected Auto Scaling: termination for group "MyASG"

ree

Step 7:

You can also watch the CPU Utilization graph on the CloudWatch Metrics.

Go to Left side panel and click Auto Scaling Groups.

Select MyASG -> Monitoring -> EC2.

ree
ree

Note:

Delete the Template and the Auto Scaling Group if you do not need it anymore. This will also delete the instances created through the Auto Scaling Group.

Also delete the SNS Topic and Subscription created.

Search for SNS in AWS search bar and select Simple Notification Service.

ree
ree

Select the Topic and click on Delete

ree

Similarily do for Subscriptions.


Note: If you no longer need this instance make sure to terminate the instance. Click on the drop-down menu beside the actions button. Select the Instance State and click on Terminate. This will terminate your instance

Was this document helpful? How can we make this document better? Please provide your insights. You can download PDF version for reference.





We provide the best AWS training from Pune, India. For aws certification contact us now.


24 Comments


Shreya Patne
Shreya Patne
Feb 19, 2024

Well explained

Like

Shruti Bhosekar
Shruti Bhosekar
Feb 19, 2024

Very Informative blog!

Like

Gokulnath
Gokulnath
Feb 19, 2024

Very helpful sir

Like

Gokul M
Gokul M
Feb 19, 2024

well defined

Like

Glastan Jose
Glastan Jose
Feb 19, 2024

Easy to understand the EC2 Auto Scaling concept sir

Like
bottom of page