

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

# 教學課程：設定自動擴展以處理繁重的工作負載
<a name="application-auto-scaling-tutorial"></a>

在本教學課程中，您將學習如何在應用程式高於正常工作負載的時段水平擴展和縮減。當應用程式可能定期或隨季節而突然訪客大增時，這很有用。

您可以使用目標追蹤擴展政策連同排程擴展功能，以處理額外的負載。排程擴展功能會根據您指定的排程，自動代表您對 `MinCapacity` 和 `MaxCapacity` 起始變更。資源上處於作用中的目標追蹤擴展政策可以根據目前的資源使用率，在新的容量下限和上限範圍內動態擴展。

完成本教學課程後，您將會知道如何：
+ 使用排程擴充功能來事先增加額外容量，以因應繁重的負載，等到不需要額外容量時再移除。
+ 使用目標追蹤擴展政策以根據目前的資源使用率來擴展應用程式。

**Topics**
+ [先決條件](#tutorial-prerequisites)
+ [步驟 1：註冊可擴展的目標](#tutorial-register-scalable-target)
+ [步驟 2：根據您的需求設定排定的動作](#tutorial-create-scheduled-actions)
+ [步驟 3：新增目標追蹤擴展政策](#tutorial-create-target-tracking-policy)
+ [步驟 4：後續步驟](#tutorial-next-steps)
+ [步驟 5：清除](#tutorial-scaling-clean-up)

## 先決條件
<a name="tutorial-prerequisites"></a>

此教學假設您已執行下列作業：
+ 已建立 AWS 帳戶。
+ 安裝並設定 AWS CLI。
+ 授予使用 Application Auto Scaling 將資源註冊和取消註冊為可擴展目標的必要許可。此外， 授予建立擴展政策和排程動作的必要許可。如需詳細資訊，請參閱[適用於 Application Auto Scaling 的 Identity and Access Management](auth-and-access-control.md)。
+ 在可用於本教學課程的非生產環境中建立支援的資源。如果您還沒有，請立即建立一個。如果需要 Application Auto Scaling 可使用的 AWS 服務和資源的相關資訊，請參閱 [AWS 服務 可與 Application Auto Scaling 搭配使用](integrated-services-list.md) 區段。

**注意**  
完成本教學課程時，有兩個步驟可讓您將資源的最小和最大容量值設定為 0，使目前容量重設為 0。視您使用的 Application Auto Scaling 資源而定，在這些步驟期間，您可能無法將目前容量重設為 0。為了協助您解決問題，輸出中的訊息會指出最小容量不能小於指定的值，並提供 AWS 資源可以接受的最小容量值。

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

首先向 Application Auto Scaling 將資源註冊為可擴展的目標。可擴展的目標是 Application Auto Scaling 可橫向擴充和縮減的資源。

**向 Application Auto Scaling 註冊可擴展的目標**
+ 使用以下 [register-scalable-target](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/register-scalable-target.html) 命令註冊可擴展的目標。將 `--min-capacity` 和 `--max-capacity` 值設定為 0，使目前容量重設為 0。

  替換您正在使用的 Application Auto Scaling AWS 服務的命名空間的 `--service-namespace` 範例文本、您正在註冊的資源相關的可擴展維度的 `--scalable-dimension`，和具有資源辨識符的 `--resource-id`。這些值根據使用的資源以及資源 ID 的構建方式而定。如果需要更多相關資訊，請參閱主題內的 [AWS 服務 可與 Application Auto Scaling 搭配使用](integrated-services-list.md) 區段。這些主題包括範例命令，能引導您了解如何使用 Application Auto Scaling 註冊可擴展目標。

  **Linux、macOS 或 Unix**

  ```
  aws application-autoscaling register-scalable-target \
    --service-namespace {{namespace}} \
    --scalable-dimension {{dimension}} \
    --resource-id {{identifier}} \
    --min-capacity 0 --max-capacity 0
  ```

  **Windows**

  ```
  aws application-autoscaling register-scalable-target --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --min-capacity 0 --max-capacity 0
  ```

  如果成功，此命令會傳回可擴展目標的 ARN。

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

## 步驟 2：根據您的需求設定排定的動作
<a name="tutorial-create-scheduled-actions"></a>

您可以使用 [put-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 命令建立排定的動作，並設定為滿足您的業務需求。在本教學課程中，我們著重的組態會在工作時間以外將容量降低至 0，以免耗用資源。

**建立在早晨水平擴展的排定動作**

1. 若要水平擴展可擴展的目標，請使用以下 [put-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 命令。包括 `--schedule` 參數，並使用 Cron 運算式以 UTC 格式指定週期性排程。

   Application Auto Scaling 會依指定的排程 (每天上午 9 點 UTC)，將 `MinCapacity` 和 `MaxCapacity` 值更新為 1-5 個容量單位的所需範圍。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling put-scheduled-action \
     --service-namespace {{namespace}} \
     --scalable-dimension {{dimension}} \
     --resource-id {{identifier}} \
     --scheduled-action-name {{my-first-scheduled-action}} \
     --schedule "cron({{0 9 * * ? *}})" \
     --scalable-target-action MinCapacity={{1}},MaxCapacity={{5}}
   ```

   **Windows**

   ```
   aws application-autoscaling put-scheduled-action --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --scheduled-action-name {{my-first-scheduled-action}} --schedule "cron({{0 9 * * ? *}})" --scalable-target-action MinCapacity={{1}},MaxCapacity={{5}}
   ```

   如果成功，此命令不會傳回任何輸出。

1. 若要確認排定的動作存在，請使用下列 [describe-scheduled-actions](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/describe-scheduled-actions.html) 命令。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling describe-scheduled-actions \
     --service-namespace {{namespace}} \ 
     --query 'ScheduledActions[?ResourceId==`{{identifier}}`]'
   ```

   **Windows**

   ```
   aws application-autoscaling describe-scheduled-actions --service-namespace {{namespace}} --query "ScheduledActions[?ResourceId==`{{identifier}}`]"
   ```

   以下為範例輸出。

   ```
   [
       {
           "ScheduledActionName": "my-first-scheduled-action",
           "ScheduledActionARN": "{{arn}}",
           "Schedule": "cron(0 9 * * ? *)",
           "ScalableTargetAction": {
               "MinCapacity": 1,
               "MaxCapacity": 5
           },
           ...
       }
   ]
   ```

**建立在夜間水平擴展的排定動作**

1. 重複上述程序建立另一個排定的動作，供 Application Auto Scaling 在一天結束時用來縮減。

   Application Auto Scaling 會依指定的排程 (每天下午 8 點 UTC)，將目標的 `MinCapacity` 和 `MaxCapacity` 更新為 0，如以下 [put-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scheduled-action.html) 命令所指示。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling put-scheduled-action \
     --service-namespace {{namespace}} \
     --scalable-dimension {{dimension}} \
     --resource-id {{identifier}} \
     --scheduled-action-name {{my-second-scheduled-action}} \
     --schedule "cron({{0 20 * * ? *}})" \
     --scalable-target-action MinCapacity={{0}},MaxCapacity={{0}}
   ```

   **Windows**

   ```
   aws application-autoscaling put-scheduled-action --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --scheduled-action-name {{my-second-scheduled-action}} --schedule "cron({{0 20 * * ? *}})" --scalable-target-action MinCapacity={{0}},MaxCapacity={{0}}
   ```

1. 若要確認排定的動作存在，請使用下列 [describe-scheduled-actions](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/describe-scheduled-actions.html) 命令。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling describe-scheduled-actions \
     --service-namespace {{namespace}} \ 
     --query 'ScheduledActions[?ResourceId==`{{identifier}}`]'
   ```

   **Windows**

   ```
   aws application-autoscaling describe-scheduled-actions --service-namespace {{namespace}} --query "ScheduledActions[?ResourceId==`{{identifier}}`]"
   ```

   以下為範例輸出。

   ```
   [
       {
           "ScheduledActionName": "my-first-scheduled-action",
           "ScheduledActionARN": "{{arn}}",
           "Schedule": "cron(0 9 * * ? *)",
           "ScalableTargetAction": {
               "MinCapacity": 1,
               "MaxCapacity": 5
           },
           ...
       },
       {
           "ScheduledActionName": "my-second-scheduled-action",
           "ScheduledActionARN": "{{arn}}",
           "Schedule": "cron(0 20 * * ? *)",
           "ScalableTargetAction": {
               "MinCapacity": 0,
               "MaxCapacity": 0
           },
           ...
       }
   ]
   ```

## 步驟 3：新增目標追蹤擴展政策
<a name="tutorial-create-target-tracking-policy"></a>

現在您已備妥基本排程，請新增目標追蹤擴展政策，以根據目前的資源使用率來擴展。

Application Auto Scaling 會透過目標追蹤，比較政策中的目標值與特定指標的目前值。當這些值在一段時間內不相等時，Application Auto Scaling 會增加或移除容量，以維持穩定的效能。當應用程式的負載和指標值上升時，Application Auto Scaling 會盡快增加容量，但不會超過 `MaxCapacity`。當 Application Auto Scaling 因負載極小而刪除容量時，不會低於 `MinCapacity`。根據使用量調整容量，您只須為應用程式所需而付費。

如果因為應用程式沒有任何負載，以致指標沒有足夠的資料，則 Application Auto Scaling 不會增加或移除容量。換句話說，Application Auto Scaling 會在資訊不足的情況下，以可用性為優先。

您可以新增多個擴展政策，但絕不可新增衝突的步驟擴展政策，否則可能造成不良後果。例如，如果步進擴展政策在目標追蹤政策準備好縮減之前即啟動縮減活動，則不會封鎖縮減活動。縮減活動完成之後，目標追蹤政策可以指示 Application Auto Scaling 再次水平擴展。

**建立目標追蹤擴展政策**

1. 使用以下 [put-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scaling-policy.html) 命令建立政策。

   目標追蹤最常用的指標已預先定義，可以直接使用，不需要從 CloudWatch 提供完整的指標規格。關於有哪些預先定義的指標可用，詳情請參閱 [Application Auto Scaling 的目標追蹤擴展政策](application-auto-scaling-target-tracking.md)。

   執行此命令之前，請確定預先定義的指標期待目標值。例如，若要在 CPU 使用率達到 50% 時水平擴展，請指定目標值 50.0。或者，若要在使用率達到 70% 時水平擴展 Lambda 佈建並行，請指定目標值 0.7。關於特定資源的目標值，相關資訊請參閱服務提供的文件，以了解如何設定目標追蹤。如需詳細資訊，請參閱[AWS 服務 可與 Application Auto Scaling 搭配使用](integrated-services-list.md)。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling put-scaling-policy \
     --service-namespace {{namespace}} \
     --scalable-dimension {{dimension}} \
     --resource-id {{identifier}} \
     --policy-name {{my-scaling-policy}} --policy-type TargetTrackingScaling \
     --target-tracking-scaling-policy-configuration '{ "TargetValue": {{50.0}}, "PredefinedMetricSpecification": { "PredefinedMetricType": "{{predefinedmetric}}" }}'
   ```

   **Windows**

   ```
   aws application-autoscaling put-scaling-policy --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --policy-name {{my-scaling-policy}} --policy-type TargetTrackingScaling --target-tracking-scaling-policy-configuration "{ \"TargetValue\": {{50.0}}, \"PredefinedMetricSpecification\": { \"PredefinedMetricType\": \"{{predefinedmetric}}\" }}"
   ```

   如果成功，此命令會傳回替您建立的兩個 CloudWatch 警示的 ARN 和名稱。

1. 若要確認排定的動作存在，請使用下列 [describe-scaling-policies](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/describe-scaling-policies.html) 命令。

   **Linux、macOS 或 Unix**

   ```
   aws application-autoscaling describe-scaling-policies --service-namespace {{namespace}} \
     --query 'ScalingPolicies[?ResourceId==`{{identifier}}`]'
   ```

   **Windows**

   ```
   aws application-autoscaling describe-scaling-policies --service-namespace {{namespace}} --query "ScalingPolicies[?ResourceId==`{{identifier}}`]"
   ```

   以下為範例輸出。

   ```
   [
       {
           "PolicyARN": "{{arn}}",
           "TargetTrackingScalingPolicyConfiguration": {
               "PredefinedMetricSpecification": {
                   "PredefinedMetricType": "{{predefinedmetric}}"
               },
               "TargetValue": 50.0
           },
           "PolicyName": "my-scaling-policy",
           "PolicyType": "TargetTrackingScaling",
           "Alarms": [],
           ...
       }
   ]
   ```

## 步驟 4：後續步驟
<a name="tutorial-next-steps"></a>

當擴展活動發生時，您會在可擴展目標的擴展活動輸出值中看到該活動的記錄，例如：

```
Successfully set desired count to 1. Change successfully fulfilled by ecs.
```

若要使用 Application Auto Scaling 來監控擴展活動，您可以使用以下的 [describe-scaling-activities](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/describe-scaling-activities.html) 命令。

**Linux、macOS 或 Unix**

```
aws application-autoscaling describe-scaling-activities 
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}}
```

**Windows**

```
aws application-autoscaling describe-scaling-activities --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}}
```

## 步驟 5：清除
<a name="tutorial-scaling-clean-up"></a>

若要避免帳戶因為積極擴展時建立的資源而產生費用，您可以按照下列方式清除相關的擴展組態。

刪除擴展組態不會刪除基礎 AWS 資源。也不會恢復到原來的容量。在您建立資源的服務主控台上，您可以刪除資源或調整其容量。

**刪除排程動作**  
以下 [delete-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/delete-scheduled-action.html) 命令會刪除指定的排定動作。如果想要保留您建立的排定動作，您可以略過此步驟。

**Linux、macOS 或 Unix**

```
aws application-autoscaling delete-scheduled-action \
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}} \
  --scheduled-action-name {{my-second-scheduled-action}}
```

**Windows**

```
aws application-autoscaling delete-scheduled-action --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --scheduled-action-name {{my-second-scheduled-action}}
```

**刪除擴展政策**  
以下 [delete-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/delete-scaling-policy.html) 命令會刪除指定的目標追蹤擴展政策。如果想要保留您建立的擴展政策，您可以略過此步驟。

**Linux、macOS 或 Unix**

```
aws application-autoscaling delete-scaling-policy \
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}} \
  --policy-name {{my-scaling-policy}}
```

**Windows**

```
aws application-autoscaling delete-scaling-policy --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}} --policy-name {{my-scaling-policy}}
```

**解除登錄可擴展的目標**  
使用以下 [deregister-scalable-target](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/deregister-scalable-target.html) 命令取消註冊可擴展的目標。如果您有任何由您建立的擴展政策或任何排程動作尚未刪除，此命令會將其全部刪除。如果您想要保留可擴展的目標以供日後使用，您可以略過此步驟。

**Linux、macOS 或 Unix**

```
aws application-autoscaling deregister-scalable-target \
  --service-namespace {{namespace}} \
  --scalable-dimension {{dimension}} \
  --resource-id {{identifier}}
```

**Windows**

```
aws application-autoscaling deregister-scalable-target --service-namespace {{namespace}} --scalable-dimension {{dimension}} --resource-id {{identifier}}
```