

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

# 使用 AWS CLI for Amazon WorkSpaces 應用程式管理機群擴展
<a name="autoscaling-cli"></a>

您可以使用 AWS Command Line Interface (AWS CLI) 來設定和管理機群擴展。如需設定多個擴展政策或設定向內擴展和向外擴展冷卻時間等更進階的功能，請使用 AWS CLI。在執行調整規模政策命令前，您必須先將您的機群註冊為可調整規模的目標。若要這樣做，請使用以下 [register-scalable-target](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/register-scalable-target.html) 命令：

```
aws application-autoscaling register-scalable-target
  --service-namespace appstream \
  --resource-id fleet/fleetname \
  --scalable-dimension appstream:fleet:DesiredCapacity \
  --min-capacity 1 --max-capacity 5
```

**Topics**
+ [範例 1：根據容量使用率套用調整規模政策](#autoscaling-cli-utilization)
+ [範例 2：根據容量不足錯誤套用調整規模政策](#autoscaling-cli-capacity)
+ [範例 3：根據低容量使用率套用調整規模政策](#autoscaling-cli-scale-in)
+ [範例 4：根據排程變更機群容量](#autoscaling-cli-schedule)
+ [範例 5：套用目標追蹤調整規模政策](#autoscaling-target-tracking)

## 範例 1：根據容量使用率套用調整規模政策
<a name="autoscaling-cli-utilization"></a>

如果使用率 >= 75%，此 AWS CLI 範例會設定擴展政策，將機群擴展 25%。

以下 [put-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scaling-policy.html) 命令會定義使用率類型的調整規模政策：

```
aws application-autoscaling put-scaling-policy --cli-input-json file://scale-out-utilization.json
```

`scale-out-utilization.json` 檔案的內容如下：

```
{
    "PolicyName": "policyname",
    "ServiceNamespace": "appstream",
    "ResourceId": "fleet/fleetname",
    "ScalableDimension": "appstream:fleet:DesiredCapacity",
    "PolicyType": "StepScaling",
    "StepScalingPolicyConfiguration": {
        "AdjustmentType": "PercentChangeInCapacity",
        "StepAdjustments": [
            {
                "MetricIntervalLowerBound": 0,
                "ScalingAdjustment": 25
            }
        ],
        "Cooldown": 120
    }
}
```

若命令成功，則輸出類似以下內容，雖然有些詳細資訊會因您的帳戶和區域而有所不同。在此範例中，政策識別符是 `e3425d21-16f0-d701-89fb-12f98dac64af`。

```
{"PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:e3425d21-16f0-d701-89fb-12f98dac64af:resource/appstream/fleet/SampleFleetName:policyName/scale-out-utilization-policy"}
```

現在，為此政策設定 CloudWatch 警示。使用適用於您的名稱、區域、帳號和政策識別符。您可以使用先前命令針對 `--alarm-actions` 參數所傳回的政策 ARN。

```
aws cloudwatch put-metric-alarm 
--alarm-name alarmname \
--alarm-description "Alarm when Capacity Utilization exceeds 75 percent" \
--metric-name CapacityUtilization \
--namespace AWS/AppStream \
--statistic Average \
--period 300 \
--threshold 75 \
--comparison-operator GreaterThanOrEqualToThreshold \
--dimensions "Name=Fleet,Value=fleetname" \
--evaluation-periods 1 --unit Percent \
--alarm-actions "arn:aws:autoscaling:your-region-code:account-number-without-hyphens:scalingPolicy:policyid:resource/appstream/fleet/fleetname:policyName/policyname"
```

## 範例 2：根據容量不足錯誤套用調整規模政策
<a name="autoscaling-cli-capacity"></a>

如果機群傳回`InsufficientCapacityError`錯誤，此 AWS CLI 範例會設定擴展政策，將機群向外擴展 1。

以下命令會定義容量不足類型的調整規模政策：

```
aws application-autoscaling put-scaling-policy --cli-input-json file://scale-out-capacity.json
```

`scale-out-capacity.json` 檔案的內容如下：

```
{
    "PolicyName": "policyname",
    "ServiceNamespace": "appstream",
    "ResourceId": "fleet/fleetname",
    "ScalableDimension": "appstream:fleet:DesiredCapacity",
    "PolicyType": "StepScaling",
    "StepScalingPolicyConfiguration": {
        "AdjustmentType": "ChangeInCapacity",
        "StepAdjustments": [
            {
                "MetricIntervalLowerBound": 0,
                "ScalingAdjustment": 1
            }
        ],
        "Cooldown": 120
    }
}
```

若命令成功，則輸出類似以下內容，雖然有些詳細資訊會因您的帳戶和區域而有所不同。在此範例中，政策識別符是 `f4495f21-0650-470c-88e6-0f393adb64fc`。

```
{"PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:f4495f21-0650-470c-88e6-0f393adb64fc:resource/appstream/fleet/SampleFleetName:policyName/scale-out-insufficient-capacity-policy"}
```

現在，為此政策設定 CloudWatch 警示。使用適用於您的名稱、區域、帳號和政策識別符。您可以使用先前命令針對 `--alarm-actions` 參數所傳回的政策 ARN。

```
aws cloudwatch put-metric-alarm 
--alarm-name alarmname \
--alarm-description "Alarm when out of capacity is > 0" \
--metric-name InsufficientCapacityError \
--namespace AWS/AppStream \
--statistic Maximum \
--period 300 \
--threshold 0 \
--comparison-operator GreaterThanThreshold \
--dimensions "Name=Fleet,Value=fleetname" \
--evaluation-periods 1 --unit Count \
--alarm-actions "arn:aws:autoscaling:your-region-code:account-number-without-hyphens:scalingPolicy:policyid:resource/appstream/fleet/fleetname:policyName/policyname"
```

## 範例 3：根據低容量使用率套用調整規模政策
<a name="autoscaling-cli-scale-in"></a>

此 AWS CLI 範例會設定在機群中擴展的擴展政策，以在 `CapacityUtilization`低時減少實際容量。

以下命令會定義容量超過類型的調整規模政策：

```
aws application-autoscaling put-scaling-policy --cli-input-json file://scale-in-capacity.json
```

`scale-in-capacity.json` 檔案的內容如下：

```
{
    "PolicyName": "policyname",
    "ServiceNamespace": "appstream",
    "ResourceId": "fleet/fleetname",
    "ScalableDimension": "appstream:fleet:DesiredCapacity",
    "PolicyType": "StepScaling",
    "StepScalingPolicyConfiguration": {
        "AdjustmentType": "PercentChangeInCapacity",
        "StepAdjustments": [
            {
                "MetricIntervalUpperBound": 0,
                "ScalingAdjustment": -25
            }
        ],
        "Cooldown": 360
    }
}
```

若命令成功，則輸出類似以下內容，雖然有些詳細資訊會因您的帳戶和區域而有所不同。在此範例中，政策識別符是 `12ab3c4d-56789-0ef1-2345-6ghi7jk8lm90`。

```
{"PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:12ab3c4d-56789-0ef1-2345-6ghi7jk8lm90:resource/appstream/fleet/SampleFleetName:policyName/scale-in-utilization-policy"}
```

現在，為此政策設定 CloudWatch 警示。使用適用於您的名稱、區域、帳號和政策識別符。您可以使用先前命令針對 `--alarm-actions` 參數所傳回的政策 ARN。

```
aws cloudwatch put-metric-alarm 
--alarm-name alarmname \
--alarm-description "Alarm when Capacity Utilization is less than or equal to 25 percent" \
--metric-name CapacityUtilization \
--namespace AWS/AppStream \
--statistic Average \
--period 120 \
--threshold 25 \
--comparison-operator LessThanOrEqualToThreshold \
--dimensions "Name=Fleet,Value=fleetname" \
--evaluation-periods 10 --unit Percent \
--alarm-actions "arn:aws:autoscaling:your-region-code:account-number-without-hyphens:scalingPolicy:policyid:resource/appstream/fleet/fleetname:policyName/policyname"
```

## 範例 4：根據排程變更機群容量
<a name="autoscaling-cli-schedule"></a>

根據排程變更您的機群容量，可讓您針對可預期的需求變更調整機群容量規模以進行回應。例如，在工作天的一開始，您可能預期會有特定數量的使用者同時請求串流連線。若要根據排程變更機群容量，您可以使用 Application Auto Scaling [PutScheduledAction](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PutScheduledAction.html) API 動作或 [put-scheduled-action](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scheduled-action.html) AWS CLI 命令。

在變更機群容量之前，您可以使用 WorkSpaces 應用程式 [describe-fleets](https://docs.aws.amazon.com/cli/latest/reference/appstream/describe-fleets.html) AWS CLI 命令列出目前的機群容量。

```
aws appstream describe-fleets --name fleetname
```

目前的機群容量會顯示與以下內容相似的輸出 (以 JSON 格式顯示)：

```
{
    {
            "ComputeCapacityStatus": {
                "Available": 1,
                "Desired": 1,
                "Running": 1,
                "InUse": 0
            },
}
```

然後，使用 `put-scheduled-action` 命令建立排程動作來變更您的機群容量。例如，以下命令會在每天早上 9 點 (UTC) 將容量下限變更為 3，並將容量上限變更為 5。

**注意**  
對於 cron 表達式，請指定在 UTC 的何時執行動作。如需詳細資訊，請參閱 [Cron 表達式](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions)。

```
aws application-autoscaling put-scheduled-action --service-namespace appstream \
--resource-id fleet/fleetname \
--schedule="cron(0 9 * * ? *)" \
--scalable-target-action MinCapacity=3,MaxCapacity=5 \
--scheduled-action-name ExampleScheduledAction \
--scalable-dimension appstream:fleet:DesiredCapacity
```

若要確認已成功建立變更您機群容量的排程動作，請執行 [describe-scheduled-actions](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/describe-scheduled-actions.html) 命令。

```
aws application-autoscaling describe-scheduled-actions --service-namespace appstream --resource-id fleet/fleetname
```

若排程動作已成功建立，則會顯示與以下內容相似的輸出。

```
{
    "ScheduledActions": [
        {
            "ScalableDimension": "appstream:fleet:DesiredCapacity",
            "Schedule": "cron(0 9 * * ? *)",
            "ResourceId": "fleet/ExampleFleet",
            "CreationTime": 1518651232.886,
            "ScheduledActionARN": "<arn>",
            "ScalableTargetAction": {
                "MinCapacity": 3,
                "MaxCapacity": 5
            },
            "ScheduledActionName": "ExampleScheduledAction",
            "ServiceNamespace": "appstream"
        }
    ]
}
```

如需詳細資訊，請參閱《應用程式自動擴展使用者指南》**中的[排程擴展](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-scheduled-scaling.html)。

## 範例 5：套用目標追蹤調整規模政策
<a name="autoscaling-target-tracking"></a>

透過目標追蹤調整規模，您可以為機群指定容量使用率層級。

當您建立目標追蹤擴展政策時，應用程式自動擴展會自動建立和管理觸發擴展政策的 CloudWatch 警示。調整規模政策會視需要新增或移除容量，以讓容量使用率保持在等於或接近指定目標值。為了確保應用程式的可用性，您的機群可以配合指標按比例快速地擴展規模，但是以更漸進的方式縮減規模。

下列 [put-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scaling-policy.html) 命令會定義目標追蹤擴展政策，嘗試維持 WorkSpaces 應用程式機群 75% 的容量使用率。

```
aws application-autoscaling put-scaling-policy --cli-input-json file://config.json
```

`config.json` 檔案的內容如下：

```
{
  "PolicyName":"target-tracking-scaling-policy",
  "ServiceNamespace":"appstream",
  "ResourceId":"fleet/fleetname",
  "ScalableDimension":"appstream:fleet:DesiredCapacity",
  "PolicyType":"TargetTrackingScaling",
  "TargetTrackingScalingPolicyConfiguration":{
    "TargetValue":75.0,
    "PredefinedMetricSpecification":{
      "PredefinedMetricType":"AppStreamAverageCapacityUtilization"
    },
    "ScaleOutCooldown":300,
    "ScaleInCooldown":300
  }
}
```

若命令成功，則輸出類似以下內容，雖然有些詳細資訊會因您的帳戶和區域而有所不同。在此範例中，政策識別符為 6d8972f3-efc8-437c-92d1-6270f29a66e7。

```
{
    "PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:6d8972f3-efc8-437c-92d1-6270f29a66e7:resource/appstream/fleet/fleetname:policyName/target-tracking-scaling-policy",
    "Alarms": [
        {
            "AlarmARN": "arn:aws:cloudwatch:us-west-2:123456789012:alarm:TargetTracking-fleet/fleetname-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca",
            "AlarmName": "TargetTracking-fleet/fleetname-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca"
        },
        {
            "AlarmARN": "arn:aws:cloudwatch:us-west-2:123456789012:alarm:TargetTracking-fleet/fleetname-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d",
            "AlarmName": "TargetTracking-fleet/fleetname-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d"
        }
    ]
}
```

如需詳細資訊，請參閱《應用程式自動擴展使用者指南》**中的[目標追蹤擴展政策](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-target-tracking.html)。