

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 搭配使用 `PutScalingPolicy` 與 CLI
<a name="application-auto-scaling_example_application-auto-scaling_PutScalingPolicy_section"></a>

下列程式碼範例示範如何使用 `PutScalingPolicy`。

------
#### [ CLI ]

**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 服務。當 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"
        }
    ]
}
```
如需詳細資訊，請參閱《*AWS Application Auto Scaling 使用者指南*》中的 [Application Auto Scaling 目標追蹤擴展政策](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-target-tracking.html)。  
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [PutScalingPolicy](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/application-autoscaling/put-scaling-policy.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [PutScalingPolicy](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 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 參考 (V5)》**中的 [PutScalingPolicy](https://docs.aws.amazon.com/powershell/v5/reference)。

------