EC2 Auto Scaling Group Simple Scaling policy with AWS CLI

Sourav Karmakar
4 min readOct 28, 2021

Here I will show you how to configure Simple Scaling Policy for EC2 Auto Scaling Group with AWS CLI.

Simple Scaling Policy with CloudWatch alarms

Amazon EC2 Auto Scaling Group supports 3 types of dynamic scaling --
1. Target Tracking Scaling
2. Simple Scaling
3. Step Scaling

Simple Scaling:

Simple scaling policy allows you to define CloudWatch alarms based on which scale in/out is triggered. Instances can be added or removed from the Auto Scaling Group or can be set to achieve a desired capacity with each alarms action.

Here I will configure the scale out and scale in for high CPU usage alert and low CPU usage alert respectively.

Let’s start -

Step 1: Create an EC2 Launch Template

#Please modify the parameters before executionaws ec2 create-launch-template \
--launch-template-name AutoScaleLaunchTemplate \
--version-description Version_1 \
--region us-east-1 \
--launch-template-data '{ "NetworkInterfaces": [{ "DeviceIndex": 0, "Groups": ["sg-05e7a69c323bdd40f"] }], "ImageId": "ami-02e136e904f3da870", "InstanceType": "t2.micro", "KeyName": "EC2KeyPair" }'

Step 2: Deploy the Auto Scaling Group

#Please modify the parameters before executionaws autoscaling create-auto-scaling-group \
--auto-scaling-group-name DemoAutoScalingGroup \
--launch-template LaunchTemplateId=lt-0e321233cce2818d2 \
--min-size 1 \
--max-size 4\
--vpc-zone-identifier "subnet-00db35de81dff856b,subnet-0c4c6c468be60a37f,subnet-08b515f135d99437e"
EC2 Auto Scaling Group created

Step 3: Configure the Simple Scaling policies

Execute the below script to set a scale out policy. It will increase the capacity of the auto scaling group by 1.

#Scale out policy 
aws autoscaling put-scaling-policy \
--policy-name demoSimpleScaleOutPolicy \
--auto-scaling-group-name DemoAutoScalingGroup \
--scaling-adjustment +1 \
--adjustment-type ChangeInCapacity

Execute the below script to set a scale in policy. It will decrease the capacity of the auto scaling group by 1.

#Scale in policy
aws autoscaling put-scaling-policy \
--policy-name demoSimpleScaleInPolicy \
--auto-scaling-group-name DemoAutoScalingGroup \
--scaling-adjustment -1 \
--adjustment-type ChangeInCapacity \
--cooldown 180

Copy the both Policy ARN, will be used in the next steps.

Step 4: Create and associate CloudWatch alarms with the policies

Below script will create a CloudWatch alarm which will be triggered if the Average CPU utilization of the Auto Scaling Group goes above or at 70% for at least 3 consecutive evaluation periods of 3 minutes. Scale out policy will be in action whenever this alarm gets triggered.

#CloudWatch Alarm for scale out
aws cloudwatch put-metric-alarm \
--alarm-name SimpleScalingAlarmHighAddCapacity \
--metric-name CPUUtilization \
--namespace AWS/EC2 --statistic Average \
--period 180 --evaluation-periods 3 --threshold 70 \
--comparison-operator GreaterThanOrEqualToThreshold \
--dimensions "Name=AutoScalingGroupName, Value=DemoAutoScalingGroup" \
--alarm-actions "arn:aws:autoscaling:us-east-1:XXXXYYYYZZZZZ:scalingPolicy:43ed4879-a778-41d5-8e51-53740bc7c7c7:autoScalingGroupName/DemoAutoScalingGroup:policyName/demoSimpleScaleOutPolicy"

Below script will create a CloudWatch alarm which will be triggered if the Average CPU utilization of the Auto Scaling Group goes below or at 50% for at least 3 consecutive evaluation periods of 3 minutes. Scale in policy will be in action whenever this alarm gets triggered.

#CloudWatch Alarm for scale in
aws cloudwatch put-metric-alarm \
--alarm-name SimpleScalingAlarmHighRemoveCapacity \
--metric-name CPUUtilization \
--namespace AWS/EC2 --statistic Average \
--period 180 --evaluation-periods 3 --threshold 50 \
--comparison-operator LessThanOrEqualToThreshold \
--dimensions "Name=AutoScalingGroupName, Value=DemoAutoScalingGroup" \
--alarm-actions "arn:aws:autoscaling:us-east-1:XXXXYYYYZZZZ:scalingPolicy:3ea5a0cf-14e5-40d3-82b3-b02159464bae:autoScalingGroupName/DemoAutoScalingGroup:policyName/demoSimpleScaleInPolicy"
Simple Scaling Policies configured

--

--