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.
Name the template as MyTemplate.
Select Amazon Machine Image (AMI): Amazon Linux 2 AMI (HVM)
Select Instance Type as t2.micro
Select your existing Key Pair
In Network Settings select the Security Group that is created for Linux Server. Keep the rest default values as it is.
Scroll down and click on Create Launch Template. Your Template is successfully created.
Click on View launch templates at the bottom of new page and check whether it is created.
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.
In Configure Settings, under Network click on the drop down arrow and select two Subnets one at a time.
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
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
Click on Next in bottom right corner.
In Add Notifications, click on Add notification.
Click on Create a Topic
Configure the values as:
Send a notification to: MyASG-sns-topic
With these recipients: Enter your personal email address in this value
Click on Next In bottom right corner.
In Add tags, click on Add tag and configure the values as:
Key: Name
Value: EC2forASG
Click on Next in bottom right corner.
Review your auto scaling group. After review, scroll down and click on Create Auto Scaling group.
Your Auto Scaling group is successfully created. Check whether it is
Created.
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.
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.
Step 4:
Open you Gmail and check for the email subjected AWS Notification - Subscription Confirmation.
Open that email and click on Confirm Subscription.
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.
Select the EC2 Instance Connect and click on the Connect button in the right bottom corner.
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
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.
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.
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.
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.
You will also receive email subjected Auto Scaling: termination for group "MyASG"
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.
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.
Select the Topic and click on Delete
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.
Well explained
Very Informative blog!
Very helpful sir
well defined
Easy to understand the EC2 Auto Scaling concept sir