

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

# 使用终端节点自动扩缩


您可以使用自动扩缩自动设置终端节点配置以满足您的容量需求，而无需手动调整为文档分类终端节点和实体识别器终端节点配置的推理单元数量。

使用自动扩缩调整为终端节点预置的推理单元数量有两种方法：
+ [目标跟踪](targettracking.md)：设置自动扩缩以根据使用情况调整终端节点配置以满足容量需求。
+ [计划扩展](ScheduledScaling.md)：设置自动扩缩以按照指定计划调整终端节点配置以满足容量需求。

只能使用 AWS Command Line Interface (AWS CLI) 设置自动缩放。有关自动扩缩的更多信息，请参阅[什么是应用程序自动扩缩？](https://docs.aws.amazon.com/autoscaling/application/userguide/what-is-application-auto-scaling.html)

# 目标跟踪


通过目标跟踪，您可以根据使用情况调整终端节点配置，以满足您的容量需求。推理单元的数量会自动调整，使已利用容量保持在预配置容量的目标百分比之内。您可以使用目标跟踪来适应文档分类终端节点和实体识别器终端节点的临时使用激增。有关更多信息，请参阅 [Application Auto Scaling 的目标跟踪扩缩策略](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-target-tracking.html)。

**注意**  
以下示例的格式适用于 Unix、Linux 和 macOS。对于 Windows，请将每行末尾的反斜杠 (\$1) Unix 行继续符替换为脱字号 (^)。

## 设置目标跟踪


要为终端节点设置目标跟踪，您可以使用 AWS CLI 命令注册可扩展目标，然后创建扩展策略。可扩展目标将推理单元定义为用于调整终端节点配置的资源，扩缩策略定义了控制预配置容量自动扩缩的指标。

**设置目标跟踪**

1. 注册可扩展目标。以下示例注册了一个可扩展的目标，以调整终端节点配置，其最小容量为 1 个推理单元，最大容量为 2 个推理单元。

   对于文档分类端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling register-scalable-target \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:document-classifier-endpoint/name \
       --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits \
       --min-capacity 1 \
       --max-capacity 2
   ```

   对于实体识别器端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling register-scalable-target \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name \
       --scalable-dimension comprehend:entity-recognizer-endpoint:DesiredInferenceUnits \
       --min-capacity 1 \
       --max-capacity 2
   ```

1. 要验证可扩展目标的注册情况，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling describe-scalable-targets \
       --service-namespace comprehend \
       --resource-id endpoint ARN
   ```

1. 为扩缩策略创建目标跟踪配置，并将该配置保存在名为 `config.json` 的文件中。以下是文档分类端点的目标跟踪配置示例，其目标是将 InferenceUtilization 指标保持在 70%。

   ```
   {
     "TargetValue": 70,
     "CustomizedMetricSpecification": {
       "MetricName": "InferenceUtilization",
       "Namespace": "MyNamespace",
       "Dimensions": [
         {
           "Name": "EndpointArn",
           "Value": "arn:aws:comprehend:region:account-id:document-classifier-endpoint/name"
         }
       ],
       "Statistic": "Sum",
       "Unit": "Percent"
     }
   }
   ```

   以下是实体识别器端点的示例：

   ```
   {
     "TargetValue": 70,
     "CustomizedMetricSpecification": {
       "MetricName": "InferenceUtilization",
       "Namespace": "MyNamespace",
       "Dimensions": [
         {
           "Name": "EndpointArn",
           "Value": "arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name"
         }
       ],
       "Statistic": "Sum",
       "Unit": "Percent"
     }
   }
   ```

1. 创建扩缩策略。以下示例根据 `config.json` 文件中定义的目标跟踪配置创建扩缩策略。

   对于文档分类端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling put-scaling-policy \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:document-classifier-endpoint/name \
       --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits \
       --policy-name TestPolicy \
       --policy-type TargetTrackingScaling \
       --target-tracking-scaling-policy-configuration file://config.json
   ```

   对于实体识别器端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling put-scaling-policy \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name \
       --scalable-dimension comprehend:entity-recognizer-endpoint:DesiredInferenceUnits \
       --policy-name TestPolicy \
       --policy-type TargetTrackingScaling \
       --target-tracking-scaling-policy-configuration file://config.json
   ```

## 注意事项


在 Comprehend 端点上使用目标跟踪时，需要考虑以下注意事项：
+ 只有成功请求时才会发出终端节点指标。对于因内部服务器错误或客户错误而受到限制或失败的请求，则不会显示指标。
+ 当缺少数据点时，后备 CloudWatch 警报状态将更改为`INSUFFICIENT_DATA `。发生这种情况时，Application Auto Scaling 将无法扩展您的终端节点。
+ 公制数学可能有助于解决此限制。例如，要在未报告任何指标时使用值 0，请使用`FILL(m1,0)`函数 where is the met `m1` rics。请务必测试您的配置，以确保其行为符合预期。有关更多选项，请参阅[使用指标数学创建目标跟踪策略](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-target-tracking-metric-math.html)。

## 正在删除目标跟踪


要删除终端节点的目标跟踪，可以使用 AWS CLI 命令删除扩展策略，然后取消注册可扩展目标。

**删除目标跟踪**

1. 删除扩缩策略。以下示例删除了指定扩缩策略。

   对于文档分类端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling delete-scaling-policy \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:document-classifier-endpoint/name \
       --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits \
       --policy-name TestPolicy \
   ```

   对于实体识别器端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling delete-scaling-policy \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name \
       --scalable-dimension comprehend:entity-recognizer-endpoint:DesiredInferenceUnits \
       --policy-name TestPolicy
   ```

1. 取消注册可扩展目标。以下示例将注销指定的可扩展目标。

   对于文档分类端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling deregister-scalable-target \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:document-classifier-endpoint/name \
       --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits
   ```

   对于实体识别器端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling deregister-scalable-target \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name \
       --scalable-dimension comprehend:entity-recognizer-endpoint:DesiredInferenceUnits
   ```

# 计划扩展


通过计划扩缩，您可以调整终端节点的配置以适应指定计划的容量需求。计划扩缩会自动调整推理单元的数量，以适应特定时间的使用量激增。您可以对文档分类终端节点和实体识别器终端节点使用计划扩缩。有关计划扩缩的更多信息，请参阅[应用程序自动扩缩中的计划扩缩](https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-scheduled-scaling.html)。

**注意**  
以下示例的格式适用于 Unix、Linux 和 macOS。对于 Windows，请将每行末尾的反斜杠 (\$1) Unix 行继续符替换为脱字号 (^)。

## 设置计划扩缩


要为终端节点设置定时扩展，可以使用 AWS CLI 命令注册可扩展目标，然后创建计划操作。可扩展目标将推理单元定义为用于调整终端节点配置的资源，而计划操作控制了预配置容量在特定时间的自动扩缩。

**设置计划扩缩**

1. 注册可扩展目标。以下示例注册了一个可扩展的目标，以调整终端节点配置，其最小容量为 1 个推理单元，最大容量为 2 个推理单元。

   对于文档分类端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling register-scalable-target \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:document-classifier-endpoint/name \
       --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits \
       --min-capacity 1 \
       --max-capacity 2
   ```

   对于实体识别器端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling register-scalable-target \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name \
       --scalable-dimension comprehend:entity-recognizer-endpoint:DesiredInferenceUnits \
       --min-capacity 1 \
       --max-capacity 2
   ```

1. 创建计划操作。以下示例创建了一个计划操作，以在每天 12:00 UTC 自动调整预配置容量，最小为 2 个推理单元，最大为 5 个推理单元。有关时间表达式和计划扩缩的更多信息，请参阅[计划表达式](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html)。

   对于文档分类端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling put-scheduled-action \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:document-classifier-endpoint/name \
       --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits \
       --scheduled-action-name TestScheduledAction \
       --schedule "cron(0 12 * * ? *)" \
       --scalable-target-action MinCapacity=2,MaxCapacity=5
   ```

   对于实体识别器端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling put-scheduled-action \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name \
       --scalable-dimension comprehend:entity-recognizer-endpoint:DesiredInferenceUnits \
       --scheduled-action-name TestScheduledAction \
       --schedule "cron(0 12 * * ? *)" \
       --scalable-target-action MinCapacity=2,MaxCapacity=5
   ```

## 删除计划扩缩


要删除终端节点的计划扩展，可以使用 AWS CLI 命令删除计划操作，然后取消注册可扩展目标。

**删除计划扩缩**

1. 删除计划操作。以下示例将删除指定的计划操作。

   对于文档分类端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling delete-scheduled-action \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:document-classifier-endpoint/name \
       --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits \
       --scheduled-action-name TestScheduledAction
   ```

   对于实体识别器端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling delete-scheduled-action \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name \
       --scalable-dimension comprehend:entity-recognizer-endpoint:DesiredInferenceUnits \
       --scheduled-action-name TestScheduledAction
   ```

1. 取消注册可扩展目标。以下示例将注销指定的可扩展目标。

   对于文档分类端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling deregister-scalable-target \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:document-classifier-endpoint/name \
       --scalable-dimension comprehend:document-classifier-endpoint:DesiredInferenceUnits
   ```

   对于实体识别器端点，请使用以下 AWS CLI 命令：

   ```
   aws application-autoscaling deregister-scalable-target \
       --service-namespace comprehend \
       --resource-id arn:aws:comprehend:region:account-id:entity-recognizer-endpoint/name \
       --scalable-dimension comprehend:entity-recognizer-endpoint:DesiredInferenceUnits
   ```