AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭PutScalingPolicy
配 AWS SDK或使用 CLI
下列程式碼範例會示範如何使用PutScalingPolicy
。
- CLI
-
- AWS CLI
-
範例 1:套用具備預先定義指標規格的目標追蹤擴展政策
下列範
put-scaling-policy
例會將含有預先定義指標規格的目標追蹤擴展政策套用至預設叢集中稱為 Web 應用程式的 Amazon ECS 服務。該政策將服務的平均CPU使用率保持在 75%,擴展和縮放冷卻時間為 60 秒。輸出包含代表您建立的兩個 CloudWatch 警報的ARNs和名稱。aws application-autoscaling put-scaling-policy --service-namespace
ecs
\ --scalable-dimensionecs:service:DesiredCount
\ --resource-idservice/default/web-app
\ --policy-namecpu75-target-tracking-scaling-policy
--policy-typeTargetTrackingScaling
\ --target-tracking-scaling-policy-configurationfile://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-dimensionecs:service:DesiredCount
\ --resource-idservice/default/web-app
\ --policy-namecms75-target-tracking-scaling-policy
--policy-typeTargetTrackingScaling
\ --target-tracking-scaling-policy-configurationfile://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-dimensionecs:service:DesiredCount
\ --resource-idservice/default/web-app
\ --policy-namealb-scale-out-target-tracking-scaling-policy
\ --policy-typeTargetTrackingScaling
\ --target-tracking-scaling-policy-configurationfile://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" } ] }
如需詳細資訊,請參閱應用程式 Auto Scaling 使用者指南中的應用 Ap AWS plication Auto Scaling 的目標追蹤調整
-
如需詳API細資訊,請參閱AWS CLI 指令參考PutScalingPolicy
中的。
-
- PowerShell
-
- 適用的工具 PowerShell
-
範例 1:此指令程式會建立或更新「應用程式自動調整規模」可調整目標的原則。每個可擴展的目標都由服務命名空間、資源 ID 和可擴展維度識別。
Set-AASScalingPolicy -ServiceNamespace AppStream -PolicyName ASFleetScaleInPolicy -PolicyType StepScaling -ResourceId fleet/MyFleet -ScalableDimension appstream:fleet:DesiredCapacity -StepScalingPolicyConfiguration_AdjustmentType ChangeInCapacity -StepScalingPolicyConfiguration_Cooldown 360 -StepScalingPolicyConfiguration_MetricAggregationType Average -StepScalingPolicyConfiguration_StepAdjustments @{ScalingAdjustment = -1; MetricIntervalUpperBound = 0}
輸出:
Alarms PolicyARN ------ --------- {} arn:aws:autoscaling:us-west-2:012345678912:scalingPolicy:4897ca24-3caa-4bf1-8484-851a089b243c:resource/appstream/fleet/MyFleet:policyName/ASFleetScaleInPolicy
-
如需詳API細資訊,請參閱AWS Tools for PowerShell 指令程PutScalingPolicy式參考中的。
-