There are more AWS SDK examples available in the AWS Doc SDK Examples
Auto Scaling Plans examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with Auto Scaling Plans.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use create-scaling-plan.
- AWS CLI
-
To create a scaling plan
The following
create-scaling-planexample creates a scaling plan namedmy-scaling-planusing an already-created JSON file (named config.json). The structure of the scaling plan includes a scaling instruction for an Auto Scaling group namedmy-asg. It specifies theTagFiltersproperty as the application source and enables predictive scaling and dynamic scaling.aws autoscaling-plans create-scaling-plan \ --scaling-plan-namemy-scaling-plan\ --cli-input-jsonfile://~/config.jsonContents of
config.jsonfile:{ "ApplicationSource": { "TagFilters": [ { "Key": "purpose", "Values": [ "my-application" ] } ] }, "ScalingInstructions": [ { "ServiceNamespace": "autoscaling", "ResourceId": "autoScalingGroup/my-asg", "ScalableDimension": "autoscaling:autoScalingGroup:DesiredCapacity", "ScheduledActionBufferTime": 300, "PredictiveScalingMaxCapacityBehavior": "SetForecastCapacityToMaxCapacity", "PredictiveScalingMode": "ForecastAndScale", "PredefinedLoadMetricSpecification": { "PredefinedLoadMetricType": "ASGTotalCPUUtilization" }, "ScalingPolicyUpdateBehavior": "ReplaceExternalPolicies", "MinCapacity": 1, "MaxCapacity": 4, "TargetTrackingConfigurations": [ { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "ASGAverageCPUUtilization" }, "TargetValue": 50 } ] } ] }Output:
{ "ScalingPlanVersion": 1 }For more information, see the AWS Auto Scaling User Guide.
-
For API details, see CreateScalingPlan
in AWS CLI Command Reference.
-
The following code example shows how to use delete-scaling-plan.
- AWS CLI
-
To delete a scaling plan
The following
delete-scaling-planexample deletes the specified scaling plan.aws autoscaling-plans delete-scaling-plan \ --scaling-plan-namemy-scaling-plan\ --scaling-plan-version1This command produces no output.
For more information, see the AWS Auto Scaling User Guide.
-
For API details, see DeleteScalingPlan
in AWS CLI Command Reference.
-
The following code example shows how to use describe-scaling-plan-resources.
- AWS CLI
-
To describe the scalable resources for a scaling plan
The following
describe-scaling-plan-resourcesexample displays details about the single scalable resource (an Auto Scaling group) that is associated with the specified scaling plan.aws autoscaling-plans describe-scaling-plan-resources \ --scaling-plan-namemy-scaling-plan\ --scaling-plan-version1Output:
{ "ScalingPlanResources": [ { "ScalableDimension": "autoscaling:autoScalingGroup:DesiredCapacity", "ScalingPlanVersion": 1, "ResourceId": "autoScalingGroup/my-asg", "ScalingStatusCode": "Active", "ScalingStatusMessage": "Target tracking scaling policies have been applied to the resource.", "ScalingPolicies": [ { "PolicyName": "AutoScaling-my-asg-b1ab65ae-4be3-4634-bd64-c7471662b251", "PolicyType": "TargetTrackingScaling", "TargetTrackingConfiguration": { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "ALBRequestCountPerTarget", "ResourceLabel": "app/my-alb/f37c06a68c1748aa/targetgroup/my-target-group/6d4ea56ca2d6a18d" }, "TargetValue": 40.0 } } ], "ServiceNamespace": "autoscaling", "ScalingPlanName": "my-scaling-plan" } ] }For more information, see What Is AWS Auto Scaling? in the AWS Auto Scaling User Guide.
-
For API details, see DescribeScalingPlanResources
in AWS CLI Command Reference.
-
The following code example shows how to use describe-scaling-plans.
- AWS CLI
-
To describe a scaling plan
The following
describe-scaling-plansexample displays the details of the specified scaling plan.aws autoscaling-plans describe-scaling-plans \ --scaling-plan-namesscaling-plan-with-asg-and-ddbOutput:
{ "ScalingPlans": [ { "LastMutatingRequestTime": 1565388443.963, "ScalingPlanVersion": 1, "CreationTime": 1565388443.963, "ScalingInstructions": [ { "ScalingPolicyUpdateBehavior": "ReplaceExternalPolicies", "ScalableDimension": "autoscaling:autoScalingGroup:DesiredCapacity", "TargetTrackingConfigurations": [ { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "ASGAverageCPUUtilization" }, "TargetValue": 50.0, "EstimatedInstanceWarmup": 300, "DisableScaleIn": false } ], "ResourceId": "autoScalingGroup/my-asg", "DisableDynamicScaling": false, "MinCapacity": 1, "ServiceNamespace": "autoscaling", "MaxCapacity": 10 }, { "ScalingPolicyUpdateBehavior": "ReplaceExternalPolicies", "ScalableDimension": "dynamodb:table:ReadCapacityUnits", "TargetTrackingConfigurations": [ { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "DynamoDBReadCapacityUtilization" }, "TargetValue": 50.0, "ScaleInCooldown": 60, "DisableScaleIn": false, "ScaleOutCooldown": 60 } ], "ResourceId": "table/my-table", "DisableDynamicScaling": false, "MinCapacity": 5, "ServiceNamespace": "dynamodb", "MaxCapacity": 10000 }, { "ScalingPolicyUpdateBehavior": "ReplaceExternalPolicies", "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "TargetTrackingConfigurations": [ { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "DynamoDBWriteCapacityUtilization" }, "TargetValue": 50.0, "ScaleInCooldown": 60, "DisableScaleIn": false, "ScaleOutCooldown": 60 } ], "ResourceId": "table/my-table", "DisableDynamicScaling": false, "MinCapacity": 5, "ServiceNamespace": "dynamodb", "MaxCapacity": 10000 } ], "ApplicationSource": { "TagFilters": [ { "Values": [ "my-application-id" ], "Key": "application" } ] }, "StatusStartTime": 1565388455.836, "ScalingPlanName": "scaling-plan-with-asg-and-ddb", "StatusMessage": "Scaling plan has been created and applied to all resources.", "StatusCode": "Active" } ] }For more information, see What Is AWS Auto Scaling? in the AWS Auto Scaling User Guide.
-
For API details, see DescribeScalingPlans
in AWS CLI Command Reference.
-
The following code example shows how to use get-scaling-plan-resource-forecast-data.
- AWS CLI
-
To retrieve load forecast data
This example retrieves load forecast data for a scalable resource (an Auto Scaling group) that is associated with the specified scaling plan.
aws autoscaling-plans get-scaling-plan-resource-forecast-data \ --scaling-plan-namemy-scaling-plan\ --scaling-plan-version1\ --service-namespace"autoscaling"\ --resource-idautoScalingGroup/my-asg\ --scalable-dimension"autoscaling:autoScalingGroup:DesiredCapacity"\ --forecast-data-type"LoadForecast"\ --start-time"2019-08-30T00:00:00Z"\ --end-time"2019-09-06T00:00:00Z"Output:
{ "Datapoints": [...] }For more information, see What Is AWS Auto Scaling in the AWS Auto Scaling User Guide.
-
For API details, see GetScalingPlanResourceForecastData
in AWS CLI Command Reference.
-
The following code example shows how to use update-scaling-plan.
- AWS CLI
-
To update a scaling plan
The following
update-scaling-planexample modifies the scaling metric for an Auto Scaling group in the specified scaling plan.aws autoscaling-plans update-scaling-plan \ --scaling-plan-namemy-scaling-plan\ --scaling-plan-version1\ --scaling-instructions '{"ScalableDimension":"autoscaling:autoScalingGroup:DesiredCapacity","ResourceId":"autoScalingGroup/my-asg","ServiceNamespace":"autoscaling","TargetTrackingConfigurations":[{"PredefinedScalingMetricSpecification": {"PredefinedScalingMetricType":"ALBRequestCountPerTarget","ResourceLabel":"app/my-alb/f37c06a68c1748aa/targetgroup/my-target-group/6d4ea56ca2d6a18d"},"TargetValue":40.0}],"MinCapacity": 1,"MaxCapacity": 10}'This command produces no output.
For more information, see What Is AWS Auto Scaling? in the AWS Auto Scaling User Guide.
-
For API details, see UpdateScalingPlan
in AWS CLI Command Reference.
-