Target Tracking Policy for Auto Scaling

Ershad Kunnakkadan

By Ershad Kunnakkadan

on January 15, 2019

In July 2017, AWS introduced Target Tracking Policy for Auto Scaling in EC2. It helps to autoscale based on the metrics like Average CPU Utilization, Load balancer request per target, and so on. Simply stated it scales up and down the resources to keep the metric at a fixed value. For example, if the configured metric is Average CPU Utilization and the value is 60%, the Target Tracking Policy will launch more instances if the Average CPU Utilization goes beyond 60%. It will automatically scale down when the usage decreases. Target Tracking Policy works using a set of CloudWatch alarms which are automatically set when the policy is configured.

It can be configured in EC2 -> Auto Scaling Groups -> Scaling Policies.

EC2 Target Tracking Policy

We can also configure a warm-up period so that it would wait before it launches more instances to keep the metric at the configured value.

Internally, we use terraform to manage AWS resources. We can configure Target Tracking Policy using terraform as follows.

1resource "aws_launch_configuration" "web_cluster" {
2name_prefix = "staging-web-cluster"
3image_id = "<image ID>"
4instance_type = "<instance type>"
5key_name = "<ssh key name>"
6security_groups = ["<security group>"]
7user_data = "<user_data script>"
8
9root_block_device {
10volume_size = "<volume size>"
11}
12
13lifecycle {
14create_before_destroy = true
15}
16}
17
18resource "aws_autoscaling_group" "web_cluster" {
19name = "staging-web-cluster-asg"
20min_size = "<min ASG size>"
21max_size = "<max ASG size>"
22default_cooldown = "300"
23launch_configuration = "\${ aws_launch_configuration.web_cluster.name }"
24vpc_zone_identifier = ["<subnet ID>"]
25health_check_type = "EC2"
26health_check_grace_period = 300
27
28target_group_arns = ["<target group arn>"]
29}
30
31resource "aws_autoscaling_policy" "web_cluster_target_tracking_policy" {
32name = "staging-web-cluster-target-tracking-policy"
33policy_type = "TargetTrackingScaling"
34autoscaling_group_name = "\${aws_autoscaling_group.web_cluster.name}"
35estimated_instance_warmup = 200
36
37target_tracking_configuration {
38predefined_metric_specification {
39predefined_metric_type = "ASGAverageCPUUtilization"
40}
41
42    target_value = "60"
43
44}
45}

Target Tracking Policy allows us to easily configure and manage autoscaling in EC2. It's particularly helpful while running services like web servers.

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.