

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

# 使用适用于 Amazon WorkSpaces 应用程序的 AWS CLI 管理队列扩展
<a name="autoscaling-cli"></a>

您可以使用 AWS Command Line Interface (AWS CLI) 设置和管理队列扩展。要获得更高级的功能，例如设置多个扩展策略或设置缩减和横向扩展冷却时间，请使用 CLI。 AWS 在运行扩展策略命令之前，您必须将实例集注册为可扩展目标。为此，请使用以下[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>

此 AWS CLI 示例设置了一个扩展策略，如果利用率 >= 75%，则将队列扩展 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>

此 AWS CLI 示例设置了一个扩展策略，如果队列返回`InsufficientCapacityError`错误，则将队列扩展 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 命令。

在更改舰队容量之前，您可以使用 A WorkSpaces pplication [s describe-f](https://docs.aws.amazon.com/cli/latest/reference/appstream/describe-fleets.html) AWS leets CLI 命令列出当前的舰队容量。

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

当前实例集容量类似于以下输出 (用 JSON 格式显示)：

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

然后，使用 `put-scheduled-action` 命令创建计划操作以更改实例集容量。例如，以下命令在 UTC 时间每天上午 9 点将最小容量更改为 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"
        }
    ]
}
```

有关更多信息，请参阅《Application Auto Scaling 用户指南》**中的[计划扩展](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-scheduled-scaling.html)。

## 示例 5：应用目标跟踪扩展策略
<a name="autoscaling-target-tracking"></a>

通过目标跟踪扩展，您可以为实例集指定容量利用率级别。

创建目标跟踪扩展策略时，Application Auto Scaling 会自动创建和管理触发扩展策略的 CloudWatch 警报。扩缩策略根据需要增加或减少容量，将容量利用率保持在指定的目标值或接近指定的目标值。为了确保应用程序可用性，实例集针对此指标尽快按比例向外扩展，但会逐渐向内扩展。

以下[put-scaling-policy](https://docs.aws.amazon.com/cli/latest/reference/application-autoscaling/put-scaling-policy.html)命令定义了目标跟踪扩展策略，该策略尝试保持 75% 的 WorkSpaces 应用程序队列的容量利用率。

```
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"
        }
    ]
}
```

有关更多信息，请参阅《Application Auto Scaling 用户指南》**中的[目标跟踪扩缩策略](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-target-tracking.html)。