0
0
AWScloud~5 mins

Scaling policies (target tracking, step, simple) in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Scaling policies help your cloud servers adjust automatically to changes in demand. They make sure your app has enough power when many people use it and save money when fewer people use it.
When your website traffic changes a lot during the day and you want to add or remove servers automatically.
When you want to keep your app fast by adding servers if CPU usage goes above a certain level.
When you want to reduce costs by removing servers when they are not needed.
When you want to keep a specific performance level, like a target CPU usage, without manual checks.
When you want to react in steps, adding or removing a fixed number of servers based on load changes.
Config File - scaling-policy.json
scaling-policy.json
{
  "AutoScalingGroupName": "example-asg",
  "PolicyName": "cpu-target-tracking-policy",
  "PolicyType": "TargetTrackingScaling",
  "TargetTrackingConfiguration": {
    "PredefinedMetricSpecification": {
      "PredefinedMetricType": "ASGAverageCPUUtilization"
    },
    "TargetValue": 50.0,
    "DisableScaleIn": false
  }
}

This JSON file defines a target tracking scaling policy for an Auto Scaling group named example-asg. It keeps the average CPU usage at 50%. The policy automatically adds or removes servers to maintain this target.

Commands
This command creates or updates the target tracking scaling policy using the JSON file. It tells AWS how to adjust the number of servers based on CPU usage.
Terminal
aws autoscaling put-scaling-policy --cli-input-json file://scaling-policy.json
Expected OutputExpected
{"PolicyARN": "arn:aws:autoscaling:us-east-1:123456789012:scalingPolicy:abcdef12-3456-7890-abcd-ef1234567890:autoScalingGroupName/example-asg:policyName/cpu-target-tracking-policy"}
--cli-input-json - Specifies the JSON file with the scaling policy details
This command shows the scaling policies attached to the Auto Scaling group to verify the policy was created.
Terminal
aws autoscaling describe-policies --auto-scaling-group-name example-asg
Expected OutputExpected
{ "ScalingPolicies": [ { "AutoScalingGroupName": "example-asg", "PolicyName": "cpu-target-tracking-policy", "PolicyType": "TargetTrackingScaling", "TargetTrackingConfiguration": { "PredefinedMetricSpecification": { "PredefinedMetricType": "ASGAverageCPUUtilization" }, "TargetValue": 50.0, "DisableScaleIn": false } } ] }
This command creates a step scaling policy that adds 2 servers when the metric goes above the lower bound (0). It reacts in steps to changes in load.
Terminal
aws autoscaling put-scaling-policy --auto-scaling-group-name example-asg --policy-name step-scale-out --policy-type StepScaling --adjustment-type ChangeInCapacity --step-adjustments MetricIntervalLowerBound=0,ScalingAdjustment=2
Expected OutputExpected
{"PolicyARN": "arn:aws:autoscaling:us-east-1:123456789012:scalingPolicy:1234abcd-5678-ef90-ab12-34567890cdef:autoScalingGroupName/example-asg:policyName/step-scale-out"}
--policy-type - Specifies the policy is step scaling
--adjustment-type - Defines how the scaling adjustment is interpreted
--step-adjustments - Defines the step change in capacity based on metric intervals
This command creates a simple scaling policy that removes 1 server when triggered. The cooldown period prevents rapid repeated scaling.
Terminal
aws autoscaling put-scaling-policy --auto-scaling-group-name example-asg --policy-name simple-scale-in --policy-type SimpleScaling --adjustment-type ChangeInCapacity --scaling-adjustment -1 --cooldown 300
Expected OutputExpected
{"PolicyARN": "arn:aws:autoscaling:us-east-1:123456789012:scalingPolicy:abcd1234-5678-90ef-ab12-34567890cdef:autoScalingGroupName/example-asg:policyName/simple-scale-in"}
--cooldown - Sets wait time before another scaling action can happen
Key Concept

If you remember nothing else from this pattern, remember: scaling policies automatically adjust your servers to keep your app fast and cost-effective without manual work.

Common Mistakes
Using incorrect JSON syntax in the scaling policy file
AWS CLI will fail to create the policy and show an error
Validate JSON syntax before running the command and use proper quotes and commas
Not specifying the Auto Scaling group name correctly
The policy will not attach to the right group and won't work
Double-check the Auto Scaling group name matches exactly the existing group
Setting scaling adjustments too high or too low without testing
Can cause too many or too few servers, leading to cost waste or poor performance
Start with small adjustments and monitor behavior before increasing
Summary
Create scaling policies using JSON files or CLI flags to automate server adjustments.
Use target tracking to keep a metric like CPU usage at a set value automatically.
Use step scaling to add or remove servers in fixed steps based on metric thresholds.
Use simple scaling for basic add or remove actions with cooldown periods to avoid rapid changes.
Verify policies with describe commands to ensure they are attached and configured correctly.