Create a target tracking scaling policy using the Amazon CLI
You can create a target tracking scaling policy for Application Auto Scaling by using the Amazon CLI for the following configuration tasks.
-
Register a scalable target.
-
Add a target tracking scaling policy on the scalable target.
For brevity, the examples in this topic illustrate CLI commands for an Amazon EC2 Spot Fleet.
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 a Spot Fleet request with Application Auto Scaling. Application Auto Scaling can scale the number of instances in the Spot Fleet at a minimum of 2 instances and a maximum of 10.
Linux, macOS, or Unix
aws application-autoscaling register-scalable-target --service-namespace
ec2
\ --scalable-dimensionec2:spot-fleet-request:TargetCapacity
\ --resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
\ --min-capacity2
--max-capacity10
Windows
aws application-autoscaling register-scalable-target --service-namespace
ec2
--scalable-dimensionec2:spot-fleet-request:TargetCapacity
--resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
--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 target tracking scaling policy
Example: target tracking configuration file
The following is an example target tracking configuration that keeps the average CPU
utilization at 40 percent. Save this configuration in a file named
config.json
.
{ "TargetValue": 40.0, "PredefinedMetricSpecification": { "PredefinedMetricType": "EC2SpotFleetRequestAverageCPUUtilization" } }
For more information, see PredefinedMetricSpecification in the Application Auto Scaling API Reference.
Alternatively, you can use a custom metric for scaling by creating a customized metric specification and adding values for each parameter from CloudWatch. The following is an example target tracking configuration that keeps the average utilization of the specified metric at 40 percent.
{ "TargetValue":40.0, "CustomizedMetricSpecification":{ "MetricName":"MyUtilizationMetric", "Namespace":"MyNamespace", "Dimensions":[ { "Name":"MyOptionalMetricDimensionName", "Value":"MyOptionalMetricDimensionValue" } ], "Statistic":"Average", "Unit":"Percent" } }
For more information, see CustomizedMetricSpecification in the Application Auto Scaling API Reference.
Example: cpu40-target-tracking-scaling-policy
Use the following put-scaling-policy command, along with the config.json
file you created, to create a scaling policy named
cpu40-target-tracking-scaling-policy
.
Linux, macOS, or Unix
aws application-autoscaling put-scaling-policy --service-namespace
ec2
\ --scalable-dimensionec2:spot-fleet-request:TargetCapacity
\ --resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
\ --policy-namecpu40-target-tracking-scaling-policy
--policy-type TargetTrackingScaling \ --target-tracking-scaling-policy-configurationfile://config.json
Windows
aws application-autoscaling put-scaling-policy --service-namespace
ec2
--scalable-dimensionec2:spot-fleet-request:TargetCapacity
--resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
--policy-namecpu40-target-tracking-scaling-policy
--policy-type TargetTrackingScaling --target-tracking-scaling-policy-configurationfile://config.json
If successful, this command returns the ARNs and names of the two CloudWatch alarms created on your behalf.
{
"PolicyARN": "arn:aws-cn:autoscaling:region:account-id:scalingPolicy:policy-id:resource/ec2/spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE:policyName/cpu40-target-tracking-scaling-policy",
"Alarms": [
{
"AlarmARN": "arn:aws-cn:cloudwatch:region:account-id:alarm:TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca",
"AlarmName": "TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca"
},
{
"AlarmARN": "arn:aws-cn:cloudwatch:region:account-id:alarm:TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d",
"AlarmName": "TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d"
}
]
}
Describe target tracking 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
ec2
You can filter the results to just the target tracking 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
ec2
\ --query 'ScalingPolicies[?PolicyType==`TargetTrackingScaling`]'
Windows
aws application-autoscaling describe-scaling-policies --service-namespace
ec2
--query "ScalingPolicies[?PolicyType==`TargetTrackingScaling`]"
The following is example output.
[
{
"PolicyARN": "PolicyARN",
"TargetTrackingScalingPolicyConfiguration": {
"PredefinedMetricSpecification": {
"PredefinedMetricType": "EC2SpotFleetRequestAverageCPUUtilization"
},
"TargetValue": 40.0
},
"PolicyName": "cpu40-target-tracking-scaling-policy",
"ScalableDimension": "ec2:spot-fleet-request:TargetCapacity",
"ServiceNamespace": "ec2",
"PolicyType": "TargetTrackingScaling",
"ResourceId": "spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE",
"Alarms": [
{
"AlarmARN": "arn:aws-cn:cloudwatch:region:account-id:alarm:TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca",
"AlarmName": "TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca"
},
{
"AlarmARN": "arn:aws-cn:cloudwatch:region:account-id:alarm:TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d",
"AlarmName": "TargetTracking-spot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d"
}
],
"CreationTime": 1515021724.807
}
]
Delete a target tracking scaling policy
When you are finished with a target tracking scaling policy, you can delete it using the delete-scaling-policy command.
The following command deletes the specified target tracking scaling policy for the specified Spot Fleet request. It also deletes the CloudWatch alarms that Application Auto Scaling created on your behalf.
Linux, macOS, or Unix
aws application-autoscaling delete-scaling-policy --service-namespace
ec2
\ --scalable-dimensionec2:spot-fleet-request:TargetCapacity
\ --resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
\ --policy-namecpu40-target-tracking-scaling-policy
Windows
aws application-autoscaling delete-scaling-policy --service-namespace
ec2
--scalable-dimensionec2:spot-fleet-request:TargetCapacity
--resource-idspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE
--policy-namecpu40-target-tracking-scaling-policy