使用 AWS CLI 的 Application Auto Scaling 示例 - AWS Command Line Interface

使用 AWS CLI 的 Application Auto Scaling 示例

以下代码示例演示了如何通过将 AWS Command Line Interface与 Application Auto Scaling 结合使用,来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示了如何使用 delete-scaling-policy

AWS CLI

删除扩展策略

此示例删除在默认集群中运行的 Amazon ECS 服务 web-app 的扩展策略。

命令:

aws application-autoscaling delete-scaling-policy --policy-name web-app-cpu-lt-25 --scalable-dimension ecs:service:DesiredCount --resource-id service/default/web-app --service-namespace ecs

以下代码示例演示了如何使用 delete-scheduled-action

AWS CLI

删除计划操作

以下 delete-scheduled-action 示例从指定的 Amazon AppStream 2.0 实例集中删除指定的计划操作:

aws application-autoscaling delete-scheduled-action \ --service-namespace appstream \ --scalable-dimension appstream:fleet:DesiredCapacity \ --resource-id fleet/sample-fleet \ --scheduled-action-name my-recurring-action

此命令不生成任何输出。

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的计划扩展

以下代码示例演示了如何使用 deregister-scalable-target

AWS CLI

取消注册可扩展目标

此示例取消注册在默认集群中运行的 Amazon ECS 服务(名为 web-app)的可扩展目标。

命令:

aws application-autoscaling deregister-scalable-target --service-namespace ecs --scalable-dimension ecs:service:DesiredCount --resource-id service/default/web-app

此示例取消注册自定义资源的可扩展目标。custom-resource-id.txt 文件包含一个标识资源 ID 的字符串,对于自定义资源,该 ID 是通过 Amazon API Gateway 端点指向自定义资源的路径。

命令:

aws application-autoscaling deregister-scalable-target --service-namespace custom-resource --scalable-dimension custom-resource:ResourceType:Property --resource-id file://~/custom-resource-id.txt

custom-resource-id.txt 文件的内容:

https://example.execute-api.us-west-2.amazonaws.com/prod/scalableTargetDimensions/1-23456789

以下代码示例演示了如何使用 describe-scalable-targets

AWS CLI

描述可扩展目标

以下 describe-scalable-targets 示例描述 ecs 服务命名空间的可扩展目标。

aws application-autoscaling describe-scalable-targets \ --service-namespace ecs

输出:

{ "ScalableTargets": [ { "ServiceNamespace": "ecs", "ScalableDimension": "ecs:service:DesiredCount", "ResourceId": "service/default/web-app", "MinCapacity": 1, "MaxCapacity": 10, "RoleARN": "arn:aws:iam::123456789012:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService", "CreationTime": 1462558906.199, "SuspendedState": { "DynamicScalingOutSuspended": false, "ScheduledScalingSuspended": false, "DynamicScalingInSuspended": false }, "ScalableTargetARN": "arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123" } ] }

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的可与 Application Auto Scaling 一起使用的 AWS 服务

以下代码示例演示了如何使用 describe-scaling-activities

AWS CLI

示例 1:描述指定 Amazon ECS 服务的扩展活动

以下 describe-scaling-activities 示例描述在 default 集群中运行的 Amazon ECS 服务(名为 web-app)的扩展活动。输出显示了由扩展策略启动的扩展活动。

aws application-autoscaling describe-scaling-activities \ --service-namespace ecs \ --resource-id service/default/web-app

输出:

{ "ScalingActivities": [ { "ScalableDimension": "ecs:service:DesiredCount", "Description": "Setting desired count to 1.", "ResourceId": "service/default/web-app", "ActivityId": "e6c5f7d1-dbbb-4a3f-89b2-51f33e766399", "StartTime": 1462575838.171, "ServiceNamespace": "ecs", "EndTime": 1462575872.111, "Cause": "monitor alarm web-app-cpu-lt-25 in state ALARM triggered policy web-app-cpu-lt-25", "StatusMessage": "Successfully set desired count to 1. Change successfully fulfilled by ecs.", "StatusCode": "Successful" } ] }

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的 Application Auto Scaling 的扩展活动

示例 2:描述指定 DynamoDB 表的扩展活动

以下 describe-scaling-activities 示例描述名为 TestTable 的 DynamoDB 表的扩展活动。输出显示了由两个不同的计划操作启动的扩展活动。

aws application-autoscaling describe-scaling-activities \ --service-namespace dynamodb \ --resource-id table/TestTable

输出:

{ "ScalingActivities": [ { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting write capacity units to 10.", "ResourceId": "table/my-table", "ActivityId": "4d1308c0-bbcf-4514-a673-b0220ae38547", "StartTime": 1561574415.086, "ServiceNamespace": "dynamodb", "EndTime": 1561574449.51, "Cause": "maximum capacity was set to 10", "StatusMessage": "Successfully set write capacity units to 10. Change successfully fulfilled by dynamodb.", "StatusCode": "Successful" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting min capacity to 5 and max capacity to 10", "ResourceId": "table/my-table", "ActivityId": "f2b7847b-721d-4e01-8ef0-0c8d3bacc1c7", "StartTime": 1561574414.644, "ServiceNamespace": "dynamodb", "Cause": "scheduled action name my-second-scheduled-action was triggered", "StatusMessage": "Successfully set min capacity to 5 and max capacity to 10", "StatusCode": "Successful" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting write capacity units to 15.", "ResourceId": "table/my-table", "ActivityId": "d8ea4de6-9eaa-499f-b466-2cc5e681ba8b", "StartTime": 1561574108.904, "ServiceNamespace": "dynamodb", "EndTime": 1561574140.255, "Cause": "minimum capacity was set to 15", "StatusMessage": "Successfully set write capacity units to 15. Change successfully fulfilled by dynamodb.", "StatusCode": "Successful" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting min capacity to 15 and max capacity to 20", "ResourceId": "table/my-table", "ActivityId": "3250fd06-6940-4e8e-bb1f-d494db7554d2", "StartTime": 1561574108.512, "ServiceNamespace": "dynamodb", "Cause": "scheduled action name my-first-scheduled-action was triggered", "StatusMessage": "Successfully set min capacity to 15 and max capacity to 20", "StatusCode": "Successful" } ] }

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的 Application Auto Scaling 的扩展活动

以下代码示例演示了如何使用 describe-scaling-policies

AWS CLI

描述扩展策略

此示例命令描述 ecs 服务命名空间的扩展策略。

命令:

aws application-autoscaling describe-scaling-policies --service-namespace ecs

输出:

{ "ScalingPolicies": [ { "PolicyName": "web-app-cpu-gt-75", "ScalableDimension": "ecs:service:DesiredCount", "ResourceId": "service/default/web-app", "CreationTime": 1462561899.23, "StepScalingPolicyConfiguration": { "Cooldown": 60, "StepAdjustments": [ { "ScalingAdjustment": 200, "MetricIntervalLowerBound": 0.0 } ], "AdjustmentType": "PercentChangeInCapacity" }, "PolicyARN": "arn:aws:autoscaling:us-west-2:012345678910:scalingPolicy:6d8972f3-efc8-437c-92d1-6270f29a66e7:resource/ecs/service/default/web-app:policyName/web-app-cpu-gt-75", "PolicyType": "StepScaling", "Alarms": [ { "AlarmName": "web-app-cpu-gt-75", "AlarmARN": "arn:aws:cloudwatch:us-west-2:012345678910:alarm:web-app-cpu-gt-75" } ], "ServiceNamespace": "ecs" }, { "PolicyName": "web-app-cpu-lt-25", "ScalableDimension": "ecs:service:DesiredCount", "ResourceId": "service/default/web-app", "CreationTime": 1462562575.099, "StepScalingPolicyConfiguration": { "Cooldown": 1, "StepAdjustments": [ { "ScalingAdjustment": -50, "MetricIntervalUpperBound": 0.0 } ], "AdjustmentType": "PercentChangeInCapacity" }, "PolicyARN": "arn:aws:autoscaling:us-west-2:012345678910:scalingPolicy:6d8972f3-efc8-437c-92d1-6270f29a66e7:resource/ecs/service/default/web-app:policyName/web-app-cpu-lt-25", "PolicyType": "StepScaling", "Alarms": [ { "AlarmName": "web-app-cpu-lt-25", "AlarmARN": "arn:aws:cloudwatch:us-west-2:012345678910:alarm:web-app-cpu-lt-25" } ], "ServiceNamespace": "ecs" } ] }

以下代码示例演示了如何使用 describe-scheduled-actions

AWS CLI

描述计划的操作

以下 describe-scheduled-actions 示例显示指定服务命名空间的计划操作的详细信息:

aws application-autoscaling describe-scheduled-actions \ --service-namespace dynamodb

输出:

{ "ScheduledActions": [ { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Schedule": "at(2019-05-20T18:35:00)", "ResourceId": "table/my-table", "CreationTime": 1561571888.361, "ScheduledActionARN": "arn:aws:autoscaling:us-west-2:123456789012:scheduledAction:2d36aa3b-cdf9-4565-b290-81db519b227d:resource/dynamodb/table/my-table:scheduledActionName/my-first-scheduled-action", "ScalableTargetAction": { "MinCapacity": 15, "MaxCapacity": 20 }, "ScheduledActionName": "my-first-scheduled-action", "ServiceNamespace": "dynamodb" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Schedule": "at(2019-05-20T18:40:00)", "ResourceId": "table/my-table", "CreationTime": 1561571946.021, "ScheduledActionARN": "arn:aws:autoscaling:us-west-2:123456789012:scheduledAction:2d36aa3b-cdf9-4565-b290-81db519b227d:resource/dynamodb/table/my-table:scheduledActionName/my-second-scheduled-action", "ScalableTargetAction": { "MinCapacity": 5, "MaxCapacity": 10 }, "ScheduledActionName": "my-second-scheduled-action", "ServiceNamespace": "dynamodb" } ] }

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的计划扩展

以下代码示例演示了如何使用 list-tags-for-resource

AWS CLI

列出可扩展目标的标签

以下 list-tags-for-resource 示例列出附加到由其 ARN 指定的可扩展目标的标签键名称和值。

aws application-autoscaling list-tags-for-resource \ --resource-arn arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123

输出:

{ "Tags": { "environment": "production" } }

有关更多信息,请参阅 Application Auto Scaling 用户指南中的 Application Auto Scaling 的标签支持

以下代码示例演示了如何使用 put-scaling-policy

AWS CLI

示例 1:应用具有预定义指标规范的目标跟踪扩展策略

以下 put-scaling-policy 示例将具有预定义指标规范的目标跟踪扩展策略应用于默认集群中名为 web-app 的 Amazon ECS 服务。该策略将服务的平均 CPU 利用率保持在 75%,横向扩展和横向缩减冷却时间为 60 秒。输出包含代表您创建的两个 CloudWatch 警报的 ARN 和名称。

aws application-autoscaling put-scaling-policy --service-namespace ecs \ --scalable-dimension ecs:service:DesiredCount \ --resource-id service/default/web-app \ --policy-name cpu75-target-tracking-scaling-policy --policy-type TargetTrackingScaling \ --target-tracking-scaling-policy-configuration file://config.json

此示例假设您在当前目录中有一个 config.json 文件,其中包含以下内容:

{ "TargetValue": 75.0, "PredefinedMetricSpecification": { "PredefinedMetricType": "ECSServiceAverageCPUUtilization" }, "ScaleOutCooldown": 60, "ScaleInCooldown": 60 }

输出:

{ "PolicyARN": "arn:aws:autoscaling:us-west-2:012345678910:scalingPolicy:6d8972f3-efc8-437c-92d1-6270f29a66e7:resource/ecs/service/default/web-app:policyName/cpu75-target-tracking-scaling-policy", "Alarms": [ { "AlarmARN": "arn:aws:cloudwatch:us-west-2:012345678910:alarm:TargetTracking-service/default/web-app-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca", "AlarmName": "TargetTracking-service/default/web-app-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca" }, { "AlarmARN": "arn:aws:cloudwatch:us-west-2:012345678910:alarm:TargetTracking-service/default/web-app-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d", "AlarmName": "TargetTracking-service/default/web-app-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d" } ] }

示例 2:应用具有自定义指标规范的目标跟踪扩展策略

以下 put-scaling-policy 示例将具有自定义指标规范的目标跟踪扩展策略应用于默认集群中名为 web-app 的 Amazon ECS 服务。该策略将服务的平均利用率保持在 75%,横向扩展和横向缩减冷却时间为 60 秒。输出包含代表您创建的两个 CloudWatch 警报的 ARN 和名称。

aws application-autoscaling put-scaling-policy --service-namespace ecs \ --scalable-dimension ecs:service:DesiredCount \ --resource-id service/default/web-app \ --policy-name cms75-target-tracking-scaling-policy --policy-type TargetTrackingScaling \ --target-tracking-scaling-policy-configuration file://config.json

此示例假设您在当前目录中有一个 config.json 文件,其中包含以下内容:

{ "TargetValue":75.0, "CustomizedMetricSpecification":{ "MetricName":"MyUtilizationMetric", "Namespace":"MyNamespace", "Dimensions": [ { "Name":"MyOptionalMetricDimensionName", "Value":"MyOptionalMetricDimensionValue" } ], "Statistic":"Average", "Unit":"Percent" }, "ScaleOutCooldown": 60, "ScaleInCooldown": 60 }

输出:

{ "PolicyARN": "arn:aws:autoscaling:us-west-2:012345678910:scalingPolicy: 8784a896-b2ba-47a1-b08c-27301cc499a1:resource/ecs/service/default/web-app:policyName/cms75-target-tracking-scaling-policy", "Alarms": [ { "AlarmARN": "arn:aws:cloudwatch:us-west-2:012345678910:alarm:TargetTracking-service/default/web-app-AlarmHigh-9bc77b56-0571-4276-ba0f-d4178882e0a0", "AlarmName": "TargetTracking-service/default/web-app-AlarmHigh-9bc77b56-0571-4276-ba0f-d4178882e0a0" }, { "AlarmARN": "arn:aws:cloudwatch:us-west-2:012345678910:alarm:TargetTracking-service/default/web-app-AlarmLow-9b6ad934-6d37-438e-9e05-02836ddcbdc4", "AlarmName": "TargetTracking-service/default/web-app-AlarmLow-9b6ad934-6d37-438e-9e05-02836ddcbdc4" } ] }

示例 3:为仅向外扩展应用目标跟踪扩展策略

以下 put-scaling-policy 示例将目标跟踪扩展策略应用于默认集群中名为 web-app 的 Amazon ECS 服务。当来自应用程序负载均衡器的 RequestCountPerTarget 指标超过阈值时,该策略用于横向扩展 ECS 服务。输出包含代表您创建的 CloudWatch 警报的 ARN 和名称。

aws application-autoscaling put-scaling-policy \ --service-namespace ecs \ --scalable-dimension ecs:service:DesiredCount \ --resource-id service/default/web-app \ --policy-name alb-scale-out-target-tracking-scaling-policy \ --policy-type TargetTrackingScaling \ --target-tracking-scaling-policy-configuration file://config.json

config.json 的内容:

{ "TargetValue": 1000.0, "PredefinedMetricSpecification": { "PredefinedMetricType": "ALBRequestCountPerTarget", "ResourceLabel": "app/EC2Co-EcsEl-1TKLTMITMM0EO/f37c06a68c1748aa/targetgroup/EC2Co-Defau-LDNM7Q3ZH1ZN/6d4ea56ca2d6a18d" }, "ScaleOutCooldown": 60, "ScaleInCooldown": 60, "DisableScaleIn": true }

输出:

{ "PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:6d8972f3-efc8-437c-92d1-6270f29a66e7:resource/ecs/service/default/web-app:policyName/alb-scale-out-target-tracking-scaling-policy", "Alarms": [ { "AlarmName": "TargetTracking-service/default/web-app-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca", "AlarmARN": "arn:aws:cloudwatch:us-west-2:123456789012:alarm:TargetTracking-service/default/web-app-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca" } ] }

有关更多信息,请参阅《AWS Application Auto Scaling 用户指南》中的 Application Auto Scaling 的目标跟踪扩展策略

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 PutScalingPolicy

以下代码示例演示了如何使用 put-scheduled-action

AWS CLI

向 DynamoDB 表添加计划操作

此示例向名为 TestTable 的 DynamoDB 表添加一个计划操作,以便按周期性计划进行横向扩展。按照指定的计划(UTC 时间每天中午 12:15),如果当前容量低于为 MinCapacity 指定的值,则 Application Auto Scaling 将横向扩展到由 MinCapacity 指定的值。

命令:

aws application-autoscaling put-scheduled-action --service-namespace dynamodb --scheduled-action-name my-recurring-action --schedule "cron(15 12 * * ? *)" --resource-id table/TestTable --scalable-dimension dynamodb:table:WriteCapacityUnits --scalable-target-action MinCapacity=6

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的“计划扩展”。

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 PutScheduledAction

以下代码示例演示了如何使用 register-scalable-target

AWS CLI

示例 1:将 ECS 服务注册为可扩展目标

以下 register-scalable-target 示例向 Application Auto Scaling 注册 Amazon ECS 服务。它还向可扩展目标添加一个带有键名称 environment 和值 production 的标签。

aws application-autoscaling register-scalable-target \ --service-namespace ecs \ --scalable-dimension ecs:service:DesiredCount \ --resource-id service/default/web-app \ --min-capacity 1 --max-capacity 10 \ --tags environment=production

输出:

{ "ScalableTargetARN": "arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123" }

有关其它 AWS 服务和自定义资源的示例,请参阅《Application Auto Scaling 用户指南》中的可与 Application Auto Scaling 结合使用的 AWS 服务中的主题。

示例 2:暂停可扩展目标的扩展活动

以下 register-scalable-target 示例暂停现有可扩展目标的扩展活动。

aws application-autoscaling register-scalable-target \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:ReadCapacityUnits \ --resource-id table/my-table \ --suspended-state DynamicScalingInSuspended=true,DynamicScalingOutSuspended=true,ScheduledScalingSuspended=true

输出:

{ "ScalableTargetARN": "arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123" }

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的暂停和恢复 Application Auto Scaling 扩展

示例 3:恢复可扩展目标的扩展活动

以下 register-scalable-target 示例恢复现有可扩展目标的扩展活动。

aws application-autoscaling register-scalable-target \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:ReadCapacityUnits \ --resource-id table/my-table \ --suspended-state DynamicScalingInSuspended=false,DynamicScalingOutSuspended=false,ScheduledScalingSuspended=false

输出:

{ "ScalableTargetARN": "arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123" }

有关更多信息,请参阅《Application Auto Scaling 用户指南》中的暂停和恢复 Application Auto Scaling 扩展

以下代码示例演示了如何使用 tag-resource

AWS CLI

向可扩展目标添加标签

以下 tag-resource 示例将带有键名称 environment 和值 production 的标签添加到由其 ARN 指定的可扩展目标。

aws application-autoscaling tag-resource \ --resource-arn arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123 \ --tags environment=production

此命令不生成任何输出。

有关更多信息,请参阅 Application Auto Scaling 用户指南中的 Application Auto Scaling 的标签支持

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 TagResource

以下代码示例演示了如何使用 untag-resource

AWS CLI

从可扩展目标中移除标签

以下 untag-resource 示例从由其 ARN 指定的可扩展目标中移除带有键名称 environment 的标签对。

aws application-autoscaling untag-resource \ --resource-arn arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123 \ --tag-keys "environment"

此命令不生成任何输出。

有关更多信息,请参阅 Application Auto Scaling 用户指南中的 Application Auto Scaling 的标签支持

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 UntagResource