Create a step scaling policy using the Amazon CLI
You can create a step scaling policy for Application Auto Scaling by using the Amazon CLI for the following configuration tasks.
-
Register a scalable target.
-
Add a step scaling policy on the scalable target.
-
Create a CloudWatch alarm for the policy.
For brevity, the examples in this topic illustrate CLI commands for an Amazon ECS service. To
specify a different scalable target, specify its namespace in --service-namespace
,
its scalable dimension in --scalable-dimension
, and its resource ID in
--resource-id
. For more information and examples for each service, see the topics
in Amazon services that you can use with Application Auto Scaling.
When using the Amazon CLI, remember that your commands run in the Amazon Web Services Region configured for
your profile. If you want to run the commands in a different Region, either change the default
Region for your profile, or use the --region
parameter with the command.
Contents
Register a scalable target
If you haven't already done so, register the scalable target. Use the register-scalable-target command to register a specific resource in the target service
as a scalable target. The following example registers an Amazon ECS service with Application Auto Scaling. Application Auto Scaling
can scale the number of tasks at a minimum of 2 tasks and a maximum of 10. Replace each
user input placeholder
with your own information.
Linux, macOS, or Unix
aws application-autoscaling register-scalable-target --service-namespace
ecs
\ --scalable-dimensionecs:service:DesiredCount
\ --resource-idservice/my-cluster/my-service
\ --min-capacity2
--max-capacity10
Windows
aws application-autoscaling register-scalable-target --service-namespace
ecs
--scalable-dimensionecs:service:DesiredCount
--resource-idservice/my-cluster/my-service
--min-capacity2
--max-capacity10
If successful, this command returns the ARN of the scalable target.
{
"ScalableTargetARN": "arn:aws-cn:application-autoscaling:region
:account-id
:scalable-target/1234abcd56ab78cd901ef1234567890ab123"
}
Create a step scaling policy
To create a step scaling policy for your scalable target, you can use the following examples to help you get started.
Create an alarm that invokes the scaling policy
Finally, use the following CloudWatch put-metric-alarm command to create an alarm to use with your step scaling policy. In
this example, you have an alarm based on average CPU utilization. The alarm is configured to be
in an ALARM state if it reaches a threshold of 70 percent for at least two consecutive
evaluation periods of 60 seconds. To specify a different CloudWatch metric or use your own custom
metric, specify its name in --metric-name
and its namespace in
--namespace
.
Linux, macOS, or Unix
aws cloudwatch put-metric-alarm --alarm-name
Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service
\ --metric-nameCPUUtilization
--namespaceAWS/ECS
--statistic Average \ --period60
--evaluation-periods2
--threshold70
\ --comparison-operator GreaterThanOrEqualToThreshold \ --dimensions Name=ClusterName,Value=default
Name=ServiceName,Value=sample-app-service
\ --alarm-actionsPolicyARN
Windows
aws cloudwatch put-metric-alarm --alarm-name
Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service
--metric-nameCPUUtilization
--namespaceAWS/ECS
--statistic Average --period60
--evaluation-periods2
--threshold70
--comparison-operator GreaterThanOrEqualToThreshold --dimensions Name=ClusterName,Value=default
Name=ServiceName,Value=sample-app-service
--alarm-actionsPolicyARN
Describe step scaling policies
You can describe all scaling policies for the specified service namespace using the following describe-scaling-policies command.
aws application-autoscaling describe-scaling-policies --service-namespace
ecs
You can filter the results to just the step scaling policies using the --query
parameter. For more information about the syntax for query
, see Controlling command output from the Amazon CLI in
the Amazon Command Line Interface User Guide.
Linux, macOS, or Unix
aws application-autoscaling describe-scaling-policies --service-namespace
ecs
\ --query 'ScalingPolicies[?PolicyType==`StepScaling`]'
Windows
aws application-autoscaling describe-scaling-policies --service-namespace
ecs
--query "ScalingPolicies[?PolicyType==`StepScaling`]"
The following is example output.
[
{
"PolicyARN": "PolicyARN",
"StepScalingPolicyConfiguration": {
"MetricAggregationType": "Average",
"Cooldown": 60,
"StepAdjustments": [
{
"MetricIntervalLowerBound": 0.0,
"MetricIntervalUpperBound": 15.0,
"ScalingAdjustment": 1
},
{
"MetricIntervalLowerBound": 15.0,
"MetricIntervalUpperBound": 25.0,
"ScalingAdjustment": 2
},
{
"MetricIntervalLowerBound": 25.0,
"ScalingAdjustment": 3
}
],
"AdjustmentType": "ChangeInCapacity"
},
"PolicyType": "StepScaling",
"ResourceId": "service/my-cluster/my-service",
"ServiceNamespace": "ecs",
"Alarms": [
{
"AlarmName": "Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service",
"AlarmARN": "arn:aws-cn:cloudwatch:region:012345678910:alarm:Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service"
}
],
"PolicyName": "my-step-scaling-policy",
"ScalableDimension": "ecs:service:DesiredCount",
"CreationTime": 1515024099.901
}
]
Delete a step scaling policy
When you no longer need a step scaling policy, you can delete it. To delete both the scaling policy and the CloudWatch alarm, complete the following tasks.
To delete your scaling policy
Use the following delete-scaling-policy command.
Linux, macOS, or Unix
aws application-autoscaling delete-scaling-policy --service-namespace
ecs
\ --scalable-dimensionecs:service:DesiredCount
\ --resource-idservice/my-cluster/my-service
\ --policy-namemy-step-scaling-policy
Windows
aws application-autoscaling delete-scaling-policy --service-namespace
ecs
--scalable-dimensionecs:service:DesiredCount
--resource-idservice/my-cluster/my-service
--policy-namemy-step-scaling-policy
To delete the CloudWatch alarm
Use the delete-alarms
command. You can delete one or more alarms at a time. For example, use the following command to
delete the Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service
and
Step-Scaling-AlarmLow-ECS:service/my-cluster/my-service
alarms.
aws cloudwatch delete-alarms --alarm-name
Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service
Step-Scaling-AlarmLow-ECS:service/my-cluster/my-service