EC2 Auto Scaling Target Tracking Policy with AWS CLI

Let’s setup the Target Tracking auto scale policy with AWS CLI.

1. Dynamic Scaling- scale instances dynamically based on certain criteria.
This three types of policy can be configured for dynamic scaling —

a. Target tracking scaling
This policy helps you to select a scaling metric and set a specific threshold. Amazon EC2 auto scaling group will scale in/out in such a way that it can keep the metric at or close to the target threshold value.

b. Simple scaling
c. Step scaling

2. Predictive Scaling- scale instances based on daily or weekly traffic flow patterns.

3. Scheduled Scaling- scale instances at a scheduled time.

Here I will show how to configure Target Tracking Scaling policy for EC2 Auto Scaling Group-

* Enough IAM permissions to create and manage EC2 auto scaling groups.
* AWS CLI and programmatic access to AWS.
* A VPC with subnets, EC2 Key Pair, Security Group

Let’s begin —

Modify the below parameters and save it in a json file named LaunchTemplateData.json .

"NetworkInterfaces": [{
"DeviceIndex": 0,
"AssociatePublicIpAddress": true,
"Groups": ["sg-035766790e94c0bfb"],
"DeleteOnTermination": true
"ImageId": "ami-02e136e904f3da870",
"InstanceType": "t2.micro",
"KeyName": "EC2AutoScaleKeyPair",
"TagSpecifications": [{
"ResourceType": "instance",
"Tags": [{
"Key": "environment",
"Value": "aws-demo"

This file will be used to create an EC2 Launch template. This is where we provide the basic information required to launch an EC2 instance. Such as AMI ID, Network Interface configuration, Public IP association, Security Groups, EC2 Key Pair details, tags etc. The Image ID we have used is of Amazon Linux 2.

Step 1: Create a Launch Template.

aws ec2 create-launch-template \
--launch-template-name myDemoLaunchTmplate \
--version-description Version_1 \
--launch-template-data file://LaunchTemplateData.json \
--region us-east-1
EC2 Launch template creation

Step 2: Create the Auto Scaling Group.

aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name myDemoAutoScalingGroup \
--launch-template LaunchTemplateId=lt-0e206dda80647818a \
--min-size 1 \
--max-size 3 \
--vpc-zone-identifier "subnet-0e89d7ef8d877451b,subnet-04ea61ef9f93dda63"

Modify the parameters and run the above command. This command does not have any output.

Create auto scaling group

Verify the auto scaling group from the AWS Console -

Auto Scaling Group

Our Auto Scaling group has min capacity 1 and max 3. So, one instance has been launched when we created the Auto Scaling group.

Step 3: Configure Target Tracking Scaling policy

The following predefined metrics are available for target tracking policy:
1. ASGAverageCPUUtilization
2. ASGAverageNetworkIn
3. ASGAverageNetworkOut
4. ALBRequestCountPerTarget

We are going to configure the policy based on Average CPU utilization of the Auto Scaling Group. If the average CPU percentage goes beyond 70% for a specific time period(here it is 5 minute), Cloud Watch alarm will be triggered which will initiate scale out process.

Save the below text in a json file named TargetTrackingConfig.json.

"TargetValue": 70.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ASGAverageCPUUtilization"

If you want to configure the policy with some custom matric then use the below format -


Note: Not all metrics work for target tracking policy. For more info see here.

Run the below command to set the policy with the Auto Scaling Group.

aws autoscaling put-scaling-policy \
--policy-name TargetTrackingAutoScalePolicy \
--auto-scaling-group-name myDemoAutoScalingGroup \
--policy-type TargetTrackingScaling \
--target-tracking-configuration file://TargetTrackingConfig.json
Apply auto scaling policy

The policy has been set. You can see two Cloud Watch alarms are created to track CPU percentage(high, low) metrices. Those alerts will trigger the scale out and scale in process respectively.

Remember the instance that was launched during the creation of the auto scaling group. Find that from the EC2 console, log into it, install the stress tool and stress the CPU.

Use below commands to install Stress tool in Amazon Linux 2 instance -

sudo amazon-linux-extras install epel -y
sudo yum install stress -y

Now stress the CPU for 5 mint -

sudo stress --cpu  8 --timeout 300

After five minutes you will find a new activity in the Auto Scaling Group saying ‘Launching a new EC2 instance’-

PreInService: Scale out in progress
WaitingForInstanceWarmup: Scale out in progress, Instance Count =2, Desired Capacity =2
Scaled out successful

Similarly, wait for few minutes to see scale in process in action. If the average CPU percentage of the Auto Scaling group stays below 70% for a period of time then scale in will be triggered.

Scale in InProgress
Scale in Successful, Instance count, desired capacity will become 1

Thank you so much for following this. Enjoy :)

Cloud Engineer | AWS | Azure