

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

# 使用 建立 Application Auto Scaling 的步驟擴展政策 AWS CLI
<a name="create-step-scaling-policy-cli"></a>

此範例使用 AWS CLI 命令來建立 Amazon ECS 服務的步進擴展政策。對於不同的可擴展性目標，請在 中指定其命名空間`--service-namespace`、在 中指定其可擴展性維度`--scalable-dimension`，以及在 中指定其資源 ID`--resource-id`。

使用 時 AWS CLI，請記住您的命令會在為設定檔 AWS 區域 設定的 中執行。如果您想在不同區域中執行命令，則可變更設定檔的預設區域，或搭配 `--region` 參數使用命令。

**Topics**
+ [步驟 1：註冊可擴展的目標](#step-scaling-register-scalable-target)
+ [步驟 2：建立步驟擴展政策](#create-step-scaling-policy)
+ [步驟 3：建立叫用擴展政策的警示](#step-scaling-create-alarm)

## 步驟 1：註冊可擴展的目標
<a name="step-scaling-register-scalable-target"></a>

如果您尚未註冊可擴展的目標，請註冊。使用 [register-scalable-target](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/register-scalable-target.html) 命令，將目標服務中的特定資源註冊為可擴展的目標。下列範例向 Application Auto Scaling 註冊 Amazon ECS 服務。Application Auto Scaling 可擴展的任務數量下限為 2 個任務，上限為 10 個任務。將每個*使用者輸入預留位置*替換為自己的資訊。

**Linux、macOS 或 Unix**

```
aws application-autoscaling register-scalable-target --service-namespace ecs \
  --scalable-dimension ecs:service:DesiredCount \
  --resource-id service/my-cluster/my-service \
  --min-capacity 2 --max-capacity 10
```

**Windows**

```
aws application-autoscaling register-scalable-target --service-namespace ecs ^
  --scalable-dimension ecs:service:DesiredCount ^
  --resource-id service/my-cluster/my-service ^
  --min-capacity 2 --max-capacity 10
```

**Output**  
如果成功，此命令會傳回可擴展目標的 ARN。以下為範例輸出。

```
{
    "ScalableTargetARN": "arn:aws:application-autoscaling:region:account-id:scalable-target/1234abcd56ab78cd901ef1234567890ab123"
}
```

## 步驟 2：建立步驟擴展政策
<a name="create-step-scaling-policy"></a>

若要為可擴展的目標建立步驟擴展政策，您可以使用下列範例來協助您開始使用。

------
#### [ Scale out ]

**建立橫向擴展的步驟擴展政策 （增加容量）**

1. 使用下列`cat`命令，將步進擴展政策組態儲存在主目錄中名為 `config.json`的 JSON 檔案中。以下是調整類型為 的範例組態`PercentChangeInCapacity`，可根據下列步驟調整 （假設 CloudWatch 警示閾值為 70) 來增加可擴展目標的容量：
   + 當指標的值大於或等於 70 但小於 85 時，將容量增加 10%
   + 當指標的值大於或等於 85 但小於 95 時，將容量增加 20%
   + 當指標的值大於或等於 95 時，將容量增加 30%

   ```
   $ cat ~/config.json
   {
     "AdjustmentType": "PercentChangeInCapacity",
     "MetricAggregationType": "Average",
     "Cooldown": 60,
     "MinAdjustmentMagnitude": 1,
     "StepAdjustments": [ 
       {
         "MetricIntervalLowerBound": 0.0,
         "MetricIntervalUpperBound": 15.0,
         "ScalingAdjustment": 10
       },
       {
         "MetricIntervalLowerBound": 15.0,
         "MetricIntervalUpperBound": 25.0,
         "ScalingAdjustment": 20
       },
       {
         "MetricIntervalLowerBound": 25.0,
         "ScalingAdjustment": 30
       }
     ]
   }
   ```

   如需詳細資訊，請參閱 *Application Auto Scaling * API 參考中的 [StepScalingPolicyConfiguration](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_StepScalingPolicyConfiguration.html)。

1. 使用以下 [put-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scaling-policy.html) 命令並指定您建立的 `config.json` 檔案，建立名為 `my-step-scaling-policy` 的擴展政策。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling put-scaling-policy --service-namespace ecs \
     --scalable-dimension ecs:service:DesiredCount \
     --resource-id service/my-cluster/my-service \
     --policy-name my-step-scaling-policy --policy-type StepScaling \
     --step-scaling-policy-configuration file://config.json
   ```

   **Windows**

   ```
   aws application-autoscaling put-scaling-policy --service-namespace ecs ^
     --scalable-dimension ecs:service:DesiredCount ^
     --resource-id service/my-cluster/my-service ^
     --policy-name my-step-scaling-policy --policy-type StepScaling ^
     --step-scaling-policy-configuration file://config.json
   ```

**Output**  
該輸出包含 ARN，其作用為政策的唯一名稱。您需要它來為您的政策建立 CloudWatch 警示。以下為範例輸出。

   ```
   {
       "PolicyARN": "arn:aws:autoscaling:region:123456789012:scalingPolicy:ac542982-cbeb-4294-891c-a5a941dfa787:resource/ecs/service/my-cluster/my-service:policyName/my-step-scaling-policy"
   }
   ```

------
#### [ Scale in ]

**為縮減建立步進擴展政策 （減少容量）**

1. 使用下列`cat`命令，將步進擴展政策組態儲存在主目錄中名為 `config.json`的 JSON 檔案中。以下是調整類型為 的範例組態`ChangeInCapacity`，可根據下列步驟調整 （假設 CloudWatch 警示閾值為 50) 來減少可擴展目標的容量：
   + 當指標的值小於或等於 50 但大於 40 時，將容量減少 1
   + 當指標的值小於或等於 40 但大於 30 時，將容量減少 2
   + 當指標的值小於或等於 30 時，將容量減少 3

   ```
   $ cat ~/config.json
   {
     "AdjustmentType": "ChangeInCapacity",
     "MetricAggregationType": "Average",
     "Cooldown": 60,
     "StepAdjustments": [ 
       {
         "MetricIntervalUpperBound": 0.0,
         "MetricIntervalLowerBound": -10.0,
         "ScalingAdjustment": -1
       },
       {
         "MetricIntervalUpperBound": -10.0,
         "MetricIntervalLowerBound": -20.0,
         "ScalingAdjustment": -2
       },
       {
         "MetricIntervalUpperBound": -20.0,
         "ScalingAdjustment": -3
       }
     ]
   }
   ```

   如需詳細資訊，請參閱 *Application Auto Scaling * API 參考中的 [StepScalingPolicyConfiguration](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_StepScalingPolicyConfiguration.html)。

1. 使用以下 [put-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scaling-policy.html) 命令並指定您建立的 `config.json` 檔案，建立名為 `my-step-scaling-policy` 的擴展政策。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling put-scaling-policy --service-namespace ecs \
     --scalable-dimension ecs:service:DesiredCount \
     --resource-id service/my-cluster/my-service \
     --policy-name my-step-scaling-policy --policy-type StepScaling \
     --step-scaling-policy-configuration file://config.json
   ```

   **Windows**

   ```
   aws application-autoscaling put-scaling-policy --service-namespace ecs ^
     --scalable-dimension ecs:service:DesiredCount ^
     --resource-id service/my-cluster/my-service ^
     --policy-name my-step-scaling-policy --policy-type StepScaling ^
     --step-scaling-policy-configuration file://config.json
   ```

**Output**  
該輸出包含 ARN，其作用為政策的唯一名稱。您需要此 ARN 才能為您的政策建立 CloudWatch 警示。以下為範例輸出。

   ```
   {
       "PolicyARN": "arn:aws:autoscaling:region:123456789012:scalingPolicy:ac542982-cbeb-4294-891c-a5a941dfa787:resource/ecs/service/my-cluster/my-service:policyName/my-step-scaling-policy"
   }
   ```

------

## 步驟 3：建立叫用擴展政策的警示
<a name="step-scaling-create-alarm"></a>

最後，使用以下 CloudWatch [put-metric-alarm](https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/put-metric-alarm.html) 命令建立警示，以搭配步驟擴展政策使用。此範例將根據平均 CPU 使用率觸發警示。如果該警示至少連續兩次在 60 秒的評估期間達到閾值 70%，其將設定成處於 ALARM 狀態。若要指定不同的 CloudWatch 指標或使用您自己的自訂指標，請以 `--metric-name` 指定名稱，以 `--namespace` 指定命名空間。

**Linux、macOS 或 Unix**

```
aws cloudwatch put-metric-alarm --alarm-name Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service \
  --metric-name CPUUtilization --namespace AWS/ECS --statistic Average \
  --period 60 --evaluation-periods 2 --threshold 70 \
  --comparison-operator GreaterThanOrEqualToThreshold \
  --dimensions Name=ClusterName,Value=default Name=ServiceName,Value=sample-app-service \
  --alarm-actions PolicyARN
```

**Windows**

```
aws cloudwatch put-metric-alarm --alarm-name Step-Scaling-AlarmHigh-ECS:service/my-cluster/my-service ^
  --metric-name CPUUtilization --namespace AWS/ECS --statistic Average ^
  --period 60 --evaluation-periods 2 --threshold 70 ^
  --comparison-operator GreaterThanOrEqualToThreshold ^
  --dimensions Name=ClusterName,Value=default Name=ServiceName,Value=sample-app-service ^
  --alarm-actions PolicyARN
```