与 AWS SDK或PutScalingPolicy一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或PutScalingPolicy一起使用 CLI

以下代码示例演示如何使用 PutScalingPolicy

CLI
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-app 的 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" } ] }

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

PowerShell
用于 PowerShell

示例 1:此 cmdlet 为 Application Auto Scaling 可扩展目标创建或更新策略。每个可扩展目标都由服务命名空间、资源 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 Cmdlet 参考PutScalingPolicy中的。