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

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Application Auto Scaling 範例使用 AWS CLI

下列程式碼範例說明如何 AWS Command Line Interface 搭配 Application Auto Scaling 使用 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 delete-scaling-policy

AWS CLI

若要刪除擴展政策

此範例會刪除在預設叢集中執行之 Amazon ECS服務 Web 應用程式的擴展政策。

命令:

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

此命令不會產生輸出。

如需詳細資訊,請參閱《應用程式自動擴展使用者指南》中的排程擴展

下列程式碼範例示範如何使用 deregister-scalable-target

AWS CLI

若要取消註冊可擴展的目標

此範例會取消註冊在預設叢集中執行的名為 Web-app 的 Amazon ECS服務可擴展目標。

命令:

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 的字串,對於自訂資源,該字串是透過您的 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 AWS Auto Scaling 使用者指南中的可與 Application Auto Scaling 搭配使用的服務 Auto Scaling

下列程式碼範例示範如何使用 describe-scaling-activities

AWS CLI

範例 1:描述指定 Amazon ECS服務的擴展活動

下列describe-scaling-activities範例說明default叢集中執行web-app的名為 的 Amazon ECS服務擴展活動。輸出會顯示由擴展政策啟動的擴展活動。

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 擴展活動 Auto Scaling

範例 2:描述指定 DynamoDB 資料表的擴展活動

下列describe-scaling-activities範例說明名為 的 DynamoDB 資料表的擴展活動TestTable。輸出顯示由兩個不同的排程動作啟動的擴展活動。

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 擴展活動 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" } ] }

如需詳細資訊,請參閱《應用程式自動擴展使用者指南》中的排程擴展

下列程式碼範例示範如何使用 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 警示的 ARNs和名稱。

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 應用程式的 Amazon ECS服務。此政策會將服務的平均使用率保持在 75%,橫向擴展和橫向擴展冷卻期為 60 秒。輸出包含代表您建立的兩個 CloudWatch 警示的 ARNs和名稱。

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服務。當來自 Application Load Balancer 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" } ] }

如需詳細資訊,請參閱 Application Auto Scaling 使用者指南中的 Application Auto Scaling 的目標追蹤擴展政策AWS Auto Scaling

  • 如需API詳細資訊,請參閱 命令參考 PutScalingPolicy中的 。 AWS CLI

下列程式碼範例示範如何使用 put-scheduled-action

AWS CLI

若要將排程動作新增至 DynamoDB 資料表

此範例會將排程動作新增至名為 的 DynamoDB 資料表, TestTable 以依週期性排程縮減規模。在指定的排程 (每天中午 12:15 UTC) 上,如果目前的容量低於 指定的值 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 使用者指南中的排程擴展

下列程式碼範例示範如何使用 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 AWS Auto Scaling 使用者指南 中的可與 Application Auto Scaling 搭配使用之服務中的主題。 Auto Scaling

範例 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詳細資訊,請參閱 命令參考 TagResource中的 。 AWS CLI

下列程式碼範例示範如何使用 untag-resource

AWS CLI

從可擴展目標中移除標籤

下列untag-resource範例會從其 指定的environment可擴展目標中移除標籤對與金鑰名稱的標籤對ARN。

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詳細資訊,請參閱 命令參考 UntagResource中的 。 AWS CLI