0
0
AwsHow-ToBeginner · 4 min read

How to Use Target Tracking Scaling in AWS for Auto Scaling

Use target tracking scaling policies in AWS Auto Scaling to automatically adjust your resources by specifying a target metric value, such as CPU utilization. AWS monitors the metric and scales your resources up or down to keep the metric close to the target.
📐

Syntax

The target tracking scaling policy requires these main parts:

  • Predefined Metric Specification: The metric AWS monitors, like ASGAverageCPUUtilization.
  • Target Value: The desired value for the metric, e.g., 50 for 50% CPU.
  • Resource ID: The Auto Scaling group to apply the policy.
  • Policy Name: A unique name for the scaling policy.
bash
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name my-asg \
  --policy-name my-target-tracking-policy \
  --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{"PredefinedMetricSpecification": {"PredefinedMetricType": "ASGAverageCPUUtilization"}, "TargetValue": 50.0}'
💻

Example

This example creates a target tracking scaling policy that keeps the average CPU utilization of an Auto Scaling group at 60%. AWS will add or remove instances to maintain this target.

bash
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name example-asg \
  --policy-name cpu-target-tracking \
  --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{
    "PredefinedMetricSpecification": {
      "PredefinedMetricType": "ASGAverageCPUUtilization"
    },
    "TargetValue": 60.0
  }'
Output
{ "PolicyARN": "arn:aws:autoscaling:region:account-id:scalingPolicy:policy-id:autoScalingGroupName/example-asg:policyName/cpu-target-tracking" }
⚠️

Common Pitfalls

Common mistakes when using target tracking scaling include:

  • Setting a target value too low or too high, causing frequent scaling or no scaling.
  • Not specifying the correct Auto Scaling group name.
  • Using unsupported metrics or incorrect metric types.
  • Forgetting to attach the scaling policy to the Auto Scaling group.

Always verify your metric and target value fit your workload.

bash
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name wrong-asg-name \
  --policy-name bad-policy \
  --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{"PredefinedMetricSpecification": {"PredefinedMetricType": "ASGAverageCPUUtilization"}, "TargetValue": 10.0}'

# Correct usage:
aws autoscaling put-scaling-policy \
  --auto-scaling-group-name correct-asg-name \
  --policy-name good-policy \
  --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{"PredefinedMetricSpecification": {"PredefinedMetricType": "ASGAverageCPUUtilization"}, "TargetValue": 50.0}'
📊

Quick Reference

ParameterDescriptionExample
--auto-scaling-group-nameName of your Auto Scaling groupmy-asg
--policy-nameUnique name for the scaling policycpu-target-tracking
--policy-typeType of policy, use TargetTrackingScalingTargetTrackingScaling
--target-tracking-configurationJSON with metric and target value{"PredefinedMetricSpecification":{"PredefinedMetricType":"ASGAverageCPUUtilization"},"TargetValue":60.0}

Key Takeaways

Target tracking scaling automatically adjusts resources to keep a metric near your target value.
Use predefined metrics like ASGAverageCPUUtilization for common scaling needs.
Set realistic target values to avoid too frequent or no scaling.
Always specify the correct Auto Scaling group name when creating the policy.
Test your scaling policy to ensure it behaves as expected under load.