EC2 Auto Scaling – Target tracking scaling based on CPU utilization

TOC

EC2 Auto Scaling – Target tracking scaling based on CPU utilization

The following pages cover the basics of EC2 Auto Scaling.

あわせて読みたい
Introduction to EC2 Auto Scaling – No Scaling Policy 【Introduction to EC2 Auto Scaling - No Scaling Policy】 EC2 Auto Scaling allows you to launch any number of EC2 instances to increase the availability of yo...

This page will review the behavior of dynamic scaling.
There are three types of dynamic scaling in EC2 Auto Scaling

  • Simple scaling
  • Step scaling
  • Target Tracking Scaling

In this page, we will check the behavior of target tracking scaling.
It scales the number of instances based on CPU usage.

For more information on simple scaling, please refer to the following page.

あわせて読みたい
EC2 Auto Scaling – Simple Scaling based on CPU utilization 【EC2 Auto Scaling - Simple Scaling based on CPU utilization】 The following pages cover the basics of EC2 Auto Scaling. https://awstut.com/en/2022/10/08/int...

For step scaling, please refer to the following page

あわせて読みたい
EC2 Auto Scaling – step scaling based on CPU utilization 【EC2 Auto Scaling - step scaling based on CPU utilization】 The following pages cover the basics of EC2 Auto Scaling. https://awstut.com/en/2022/10/08/intro...

Environment

Create an ALB and attach EC2 Auto Scaling in private subnets.

Set the number of Auto Scaling instances as follows

  • Minimum number: 2
  • Maximum number: 5
  • Desired number: 2

Set scaling to run based on CPU utilization.
Set the target tracking scaling so that CPU utilization is maintained at around 20%.

The EC2 instance to be started in the Auto Scaling group, but it should be the latest version of Amazon Linux 2.
Install Apache from the yum repository on S3 and configure it to act as a web server.

Use SSM Session Manager to access the launched instance.

CloudFormation template files

Build the above configuration with CloudFormation.
The CloudFormation templates are located at the following URL

https://github.com/awstut-an-r/awstut-fa/tree/main/026

Explanation of key points of the template files

This page focuses on how to configure step scaling in EC2 Auto Scaling.

For information on how to attach resources in a private subnet to an ALB, please refer to the following page

あわせて読みたい
Attaching instances in private subnet to ALB 【Configure instances in private subnets to be attached to ALB】 We will see how to attach an instance located in a private subnet to an ALB. The following m...

For information on how to execute yum on instances in a private subnet, please refer to the following page

あわせて読みたい
yum/dnf on private subnet instances 【Configuration for running yum/dnf on instance in private subnet】 We will check how to run yum/dnf on an instance in a private subnet. In this case, the fo...

(Reference) Launch Template

Resources:
  LaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties:
      LaunchTemplateData:
        IamInstanceProfile:
          Arn: !GetAtt InstanceProfile.Arn
        ImageId: !Ref ImageId
        InstanceType: !Ref InstanceType
        SecurityGroupIds:
          - !Ref InstanceSecurityGroup
        UserData: !Base64 |
          #!/bin/bash -xe
          yum update -y
          yum install -y httpd
          systemctl start httpd
          systemctl enable httpd
          ec2-metadata -i > /var/www/html/index.html
      LaunchTemplateName: !Sub "${Prefix}-LaunchTemplate"
Code language: YAML (yaml)

A Launch template is a resource for configuration information for EC2 instances to be launched within an Auto Scaling group.
You must create a Launch template or launch configuration to configure EC2 Auto Scaling.
However, it is currently deprecated to configure Auto Scaling using Launch configurations.

We strongly recommend that you do not use launch configurations. They do not provide full functionality for Amazon EC2 Auto Scaling or Amazon EC2. We provide information about launch configurations for customers who have not yet migrated from launch configurations to launch templates.

Launch configurations

These are basically the same configuration items as for EC2 instances.
For example, specify the AMI and instance type of the instance to be launched with the ImageId and InstanceType properties.

User data can be set with the UserData property.
This time we will install and activate Apache, write the instance ID in an HTML file, and set it in the Apache root page.

Auto Scaling Group

Resources:
  AutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      AutoScalingGroupName: !Sub "${Prefix}-AutoScalingGroup"
      DesiredCapacity: !Ref DesiredCapacity
      LaunchTemplate:
        LaunchTemplateId: !Ref LaunchTemplate
        Version: !GetAtt LaunchTemplate.LatestVersionNumber
      MaxSize: !Ref MaxSize
      MinSize: !Ref MinSize
      VPCZoneIdentifier:
        - !Ref PrivateSubnet1
        - !Ref PrivateSubnet2
      TargetGroupARNs:
        - !Ref ALBTargetGroup
Code language: YAML (yaml)

No special settings are required for the Auto Scaling group in configuring step scaling.

Set the number of instances to be created in the group as follows
Set the desired number to 2 in the DesiredCapacity property.
Set the maximum number to 5 in the MaxSize property.
Set the minimum number to 2 in the MinSize property.

Scaling Policy

Resources:
  ScalingPolicy:
    Type: AWS::AutoScaling::ScalingPolicy
    Properties:
      AutoScalingGroupName: !Ref AutoScalingGroup
      PolicyType: TargetTrackingScaling
      TargetTrackingConfiguration:
        PredefinedMetricSpecification:
          PredefinedMetricType: ASGAverageCPUUtilization
        TargetValue: !Ref TargetTrackingConfigurationTargetValue
Code language: YAML (yaml)

To configure step scaling, create one policy for target tracking scaling.

Set the scaling policy type in the PolicyType property.
In this case, it is target tracking scaling, so specify “TargetTrackingScaling”.

Configure the target tracking scaling in the TargetTrackingConfiguration property.

Specify the target metric to be tracked in the PredefinedMetricSpecification property.
In this case, scaling is based on CPU utilization, so specify “ASGAverageCPUUtilization” for the PredefinedMetricType property inside this property.

Set the threshold of the metric to be tracked in the TargetValue property.
In this case, the scaling will be performed so that the CPU utilization of the entire Auto Scaling group remains below 20%, so specify “20”.

Architecting

Using CloudFormation, we will build this environment and check the actual behavior.

Create CloudFormation stacks and check resources in stacks

Create a CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page

あわせて読みたい
CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

After checking the resources in each stack, information on the main resources created this time is as follows

  • ALB: fa-026-ALB
  • DNS name of ALB: fa-026-ALB-957819670.ap-northeast-1.elb.amazonaws.com
  • ALB target group: fa-026-ALBTargetGroup
  • Launch template: fa-026-LaunchTemplate
  • EC2 Auto Scaling group: fa-026-AutoScalingGroup

Confirm the created resource from the AWS Management Console.
Confirm the ALB.

You can check the DNS name of the ALB.

Confirm the Auto Scaling group.

The desired minimum number is 2 and the maximum number is 5. In other words, within the Auto Scaling group, 2 instances will be launched during normal operation, and up to 5 instances during scale-out.

Check the scaling policy.

You can see that one target tracking scaling policy has been created.

Looking at the activity history of the Auto Scaling group, we see that two instances were created in the group that was empty.

Checking the instances in the group, we see that one instance is indeed activated.

Check Operation

Normal

Now that everything is ready, we access the ALB.

We were able to access two instances.
You can see that the Auto Scaling group is indeed attached to the ALB.

Scale-out

Check the behavior during scale-out.
Use SSM Session Manager to access the instances in the Auto Scaling group.

% aws ssm start-session --target i-00772e6923e792985

Starting session with SessionId: root-0fa311b43f4b8e6ba
sh-4.2$
Code language: Bash (bash)

For more information on SSM Session Manager, please refer to the following page

あわせて読みたい
Accessing Linux instance via SSM Session Manager 【Configure Linux instances to be accessed via SSM Session Manager】 We will check a configuration in which an EC2 instance is accessed via SSM Session Manag...

Use the yes command to increase the CPU utilization of the instance.

sh-4.2$ yes > /dev/null &
Code language: Bash (bash)

Check the CPU utilization.

CPU utilization has exceeded 20%.
Scale out should begin to maintain 20%.

The activity history of the Auto Scaling group shows that a new instance has been started.

If you check the instances in the group, you will see that two instances have indeed been added.
The CPU utilization for the entire Auto Scaling group should be kept within 20%.

Indeed, the CPU utilization has settled at about 12%.
We have thus confirmed that by using Target Tracking, the EC2 instances are automatically scaled out so that the target metric is within the specified value.

Scale-in

Manipulate the instance again and stop the yes command.

sh-4.2$ kill %1
[1]+ Terminated yes > /dev/null
Code language: Bash (bash)

Wait for a while and the CPU utilization of the entire group will decrease.

Now that the utilization has decreased, the number of unneeded instances should be scaled in.

Indeed, scale-in has started and the number of instances has been changed from 4 to 2.

Indeed, the number of instances in the group is 2.
Thus, by using target tracking, we have confirmed that the EC2 instances are automatically scaled in when the metric value of the target falls well below the threshold value.

Summary

We have confirmed the behavior of target tracking scaling, a type of dynamic scaling in EC2 Auto Scaling.

TOC