Example scaling policies for the AWS CLI - Amazon EC2 Auto Scaling

Example scaling policies for the AWS CLI

You can create scaling policies for Amazon EC2 Auto Scaling through the AWS Management Console, AWS Command Line Interface (AWS CLI), or SDKs.

The following examples show how you can create scaling policies for Amazon EC2 Auto Scaling with the AWS CLI put-scaling-policy command. Replace each user input placeholder with your own information.

To get started with writing scaling policies using the AWS CLI, see the introductory exercises in Target tracking scaling policies and Step and simple scaling policies.

Example 1: To apply a target tracking scaling policy with a predefined metric specification

aws autoscaling put-scaling-policy --policy-name cpu50-target-tracking-scaling-policy \ --auto-scaling-group-name my-asg --policy-type TargetTrackingScaling \ --target-tracking-configuration file://config.json { "TargetValue": 50.0, "PredefinedMetricSpecification": { "PredefinedMetricType": "ASGAverageCPUUtilization" } }

For more information, see PredefinedMetricSpecification in the Amazon EC2 Auto Scaling API Reference.

Note

If the file is not in the current directory, type the full path to file. For more information about reading AWS CLI parameter values from a file, see Loading AWS CLI parameters from a file in the AWS Command Line Interface User Guide.

Example 2: To apply a target tracking scaling policy with a customized metric specification

aws autoscaling put-scaling-policy --policy-name sqs100-target-tracking-scaling-policy \ --auto-scaling-group-name my-asg --policy-type TargetTrackingScaling \ --target-tracking-configuration file://config.json { "TargetValue": 100.0, "CustomizedMetricSpecification": { "MetricName": "MyBacklogPerInstance", "Namespace": "MyNamespace", "Dimensions": [{ "Name": "MyOptionalMetricDimensionName", "Value": "MyOptionalMetricDimensionValue" }], "Statistic": "Average", "Unit": "None" } }

For more information, see CustomizedMetricSpecification in the Amazon EC2 Auto Scaling API Reference.

Example 3: To apply a target tracking scaling policy for scale out only

aws autoscaling put-scaling-policy --policy-name alb1000-target-tracking-scaling-policy \ --auto-scaling-group-name my-asg --policy-type TargetTrackingScaling \ --target-tracking-configuration file://config.json { "TargetValue": 1000.0, "PredefinedMetricSpecification": { "PredefinedMetricType": "ALBRequestCountPerTarget", "ResourceLabel": "app/my-alb/778d41231b141a0f/targetgroup/my-alb-target-group/943f017f100becff" }, "DisableScaleIn": true }

Example 4: To apply a step scaling policy for scale out

aws autoscaling put-scaling-policy \ --auto-scaling-group-name my-asg \ --policy-name my-step-scale-out-policy \ --policy-type StepScaling \ --adjustment-type PercentChangeInCapacity \ --metric-aggregation-type Average \ --step-adjustments MetricIntervalLowerBound=10.0,MetricIntervalUpperBound=20.0,ScalingAdjustment=10 \ MetricIntervalLowerBound=20.0,MetricIntervalUpperBound=30.0,ScalingAdjustment=20 \ MetricIntervalLowerBound=30.0,ScalingAdjustment=30 \ --min-adjustment-magnitude 1

Record the policy's Amazon Resource Name (ARN). You need the ARN when you create the CloudWatch alarm.

Example 5: To apply a step scaling policy for scale in

aws autoscaling put-scaling-policy \ --auto-scaling-group-name my-asg \ --policy-name my-step-scale-in-policy \ --policy-type StepScaling \ --adjustment-type ChangeInCapacity \ --step-adjustments MetricIntervalUpperBound=0.0,ScalingAdjustment=-2

Record the policy's Amazon Resource Name (ARN). You need the ARN when you create the CloudWatch alarm.

Example 6: To apply a simple scaling policy for scale out

aws autoscaling put-scaling-policy --policy-name my-simple-scale-out-policy \ --auto-scaling-group-name my-asg --scaling-adjustment 30 \ --adjustment-type PercentChangeInCapacity --min-adjustment-magnitude 2

Record the policy's Amazon Resource Name (ARN). You need the ARN when you create the CloudWatch alarm.

Example 7: To apply a simple scaling policy for scale in

aws autoscaling put-scaling-policy --policy-name my-simple-scale-in-policy \ --auto-scaling-group-name my-asg --scaling-adjustment -1 \ --adjustment-type ChangeInCapacity --cooldown 180

Record the policy's Amazon Resource Name (ARN). You need the ARN when you create the CloudWatch alarm.