

• AWS Systems Manager CloudWatch 控制面板在 2026 年 4 月 30 日之后将不再可用。客户可以像现在一样继续使用 Amazon CloudWatch 控制台来查看、创建和管理其 Amazon CloudWatch 控制面板。有关更多信息，请参阅 [Amazon CloudWatch 控制面板文档](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

# 大规模运行自动化操作
<a name="running-automations-scale"></a>

借助 AWS Systems Manager Automation，您可以使用*目标*在 AWS 资源的实例集上运行自动化。此外，您还可以通过指定并发值和错误阈值来控制自动化在整个队列上的部署。并发和错误阈值功能统称为*速率控制*。并发值决定允许同时运行自动化的资源数量。Automation 还提供了一种可以选择加入的自适应并发模式。自适应并发会自动将自动化配额从 100 个同时运行的自动化配额扩展到 500 个。错误阈值决定在 Systems Manager 停止将自动化发送到其他资源之前允许自动化执行失败的次数。

有关并发性和错误阈值的更多信息，请参阅 [大规模控制自动化](running-automations-scale-controls.md)。有关目标的更多信息，请参阅 [映射自动化目标](running-automations-map-targets.md)。

以下程序介绍了如何开启自适应并发性，以及如何使用 Systems Manager 控制台和 AWS Command Line Interface (AWS CLI) 运行具有目标和速率控制的自动化。

## 运行具有目标和速率控制自动化（控制台）
<a name="scale-console"></a>

以下过程介绍了如何使用 Systems Manager 控制台运行具有目标和速率控制的自动化。

**运行具有目标和速率控制的自动化**

1. 访问 [https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/)，打开 AWS Systems Manager 控制台。

1. 在导航窗格中，选择**自动化**，然后选择**执行自动化**。

1. 在**自动化文档**列表中，请选择运行手册。在**文档类别**窗格中选择一个或多个选项，以便根据 SSM 文档的用途对其进行筛选。要查看您拥有的运行手册，请选择**我拥有的**选项卡。要查看与您的账户共享的运行手册，请选择**与我共享**选项卡。要查看所有运行手册，请选择**所有文档**选项卡。
**注意**  
您可以通过选择运行手册名称来查看有关该手册的信息。

1. 在**文档详细信息**部分中，验证**文档版本**已设置为要运行的版本。系统包括以下版本选项：
   + **运行时的默认版本** – 如果定期更新自动化运行手册并分配新的默认版本，请选择此选项。
   + **运行时的最新版本** – 如果定期更新自动化运行手册并且想要运行最新更新的版本，请选择此选项。
   + **1（默认）** – 选择此选项可执行文档的第一个版本，即默认版本。

1. 选择**下一步**。

1. 在**执行模式**部分中，选择**速率控制**。如果要使用目标和速率控制，则必须使用此模式或**多账户和多区域**。

1. 在**目标**部分中，选择希望如何设置要在其中运行自动化的 AWS 资源。这些选项是必需的。

   1. 使用**参数**列表选择一个参数。**参数**列表中的项目由此过程开始时选择的自动化运行手册中的参数确定。通过选择参数，可以定义在其上运行自动化工作流的资源类型。

   1. 使用**目标**列表选择设置目标资源的方式。

      1. 如果选择使用参数值将资源设置为目标，请输入您在**输入参数**部分为参数选择的参数值。

      1. 如果选择使用 AWS Resource Groups 将资源设置为目标，请从**资源组**列表中选择组的名称。

      1. 如果选择使用标签将资源设置为目标，请在提供的字段中输入标签键和（可选）标签值。选择**添加**。

      1. 如果要在当前 AWS 账户 和 AWS 区域 中的所有实例上运行自动化运行手册，则选择**所有实例**。

1. 在**输入参数** 部分中，指定所需的输入。或者，您也可以从 **AutomationAssumeRole** 列表中选择一个 IAM 服务角色。
**注意**  
您可能不需要选择**输入参数**部分中的某些选项。原因是您使用标签或资源组将多个资源设置为目标。例如，如果选择了 `AWS-RestartEC2Instance` 运行手册，则无需在**输入参数** 部分中指定或选择实例 ID。自动化执行过程使用您指定的标签资源组查找要重新启动的实例。

1. 使用**速率控制**部分中的选项限制可在每个账户-区域对中运行自动化的 AWS 资源的数量。

   在**并发**部分中，选择一个选项：
   + 选择**目标**，以输入可同时运行自动化工作流目标的绝对数量。
   + 选择**百分比**，以输入可同时运行自动化工作流的目标集的百分比。

1. 在**错误阈值**部分中，选择一个选项：
   + 选择**错误**，以输入自动化停止将工作流程发送到其他资源之前允许的错误的绝对数量。
   + 选择**百分比**，以输入自动化停止将工作流程发送到其他资源之前允许的错误的百分比。

1. （可选）选择一个 CloudWatch 警报以应用于您的自动化进行监控。要将 CloudWatch 警报附加到自动化，启动自动化的 IAM 主体必须具有 `iam:createServiceLinkedRole` 操作的权限。有关 CloudWatch 警报的更多信息，请参阅[使用 Amazon CloudWatch 警报](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。请注意，如果您的警报激活，自动化将停止。如果使用 AWS CloudTrail，您将在跟踪中看到 API 调用。

1. 选择**执行**。

要查看由速率控制自动化启动的自动化程序，请在导航窗格中，选择自动化，然后选择**显示子自动化程序**。

自动化执行完成后，您可以使用相同或修改后的参数重新运行该执行。有关更多信息，请参阅 [重新运行自动化执行](automation-rerun-executions.md)。

## 运行具有目标和速率控制的自动化（命令行）
<a name="scale-cli"></a>

以下过程介绍了如何使用 AWS CLI（在 Linux 或 Windows 上）或 AWS Tools for PowerShell 运行具有目标和速率控制的自动化。

**运行具有目标和速率控制的自动化**

1. 安装并配置 AWS CLI 或 AWS Tools for PowerShell（如果尚未执行该操作）。

   有关信息，请参阅[安装或更新 AWS CLI 的最新版本](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)以及[安装 AWS Tools for PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up.html)。

1. 运行以下命令以查看文档列表。

------
#### [ Linux & macOS ]

   ```
   aws ssm list-documents
   ```

------
#### [ Windows ]

   ```
   aws ssm list-documents
   ```

------
#### [ PowerShell ]

   ```
   Get-SSMDocumentList
   ```

------

   记下要使用的运行手册的名称。

1. 运行以下命令以查看有关运行手册的详细信息。用您想要查看其详细信息的运行手册的名称替换 *runbook name*。同时，记下要用于 `--target-parameter-name` 选项的参数名称（例如 `InstanceId`）。此参数确定在其上运行自动化的资源的类型。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-document \
       --name runbook name
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-document ^
       --name runbook name
   ```

------
#### [ PowerShell ]

   ```
   Get-SSMDocumentDescription `
       -Name runbook name
   ```

------

1. 创建一个要运行的使用目标和速率控制选项的命令。将每个*示例资源占位符*替换为您自己的信息。

   *使用标签设置目标*

------
#### [ Linux & macOS ]

   ```
   aws ssm start-automation-execution \
       --document-name runbook name \
       --targets Key=tag:key name,Values=value \
       --target-parameter-name parameter name \
       --parameters "input parameter name=input parameter value,input parameter 2 name=input parameter 2 value" \
       --max-concurrency 10 \
       --max-errors 25%
   ```

------
#### [ Windows ]

   ```
   aws ssm start-automation-execution ^
       --document-name runbook name ^
       --targets Key=tag:key name,Values=value ^
       --target-parameter-name parameter name ^
       --parameters "input parameter name=input parameter value,input parameter 2 name=input parameter 2 value" ^
       --max-concurrency 10 ^
       --max-errors 25%
   ```

------
#### [ PowerShell ]

   ```
   $Targets = New-Object Amazon.SimpleSystemsManagement.Model.Target
   $Targets.Key = "tag:key name"
   $Targets.Values = "value"
   
   Start-SSMAutomationExecution `
       DocumentName "runbook name" `
       -Targets $Targets `
       -TargetParameterName "parameter name" `
       -Parameter @{"input parameter name"="input parameter value";"input parameter 2 name"="input parameter 2 value"} `
       -MaxConcurrency "10" `
       -MaxError "25%"
   ```

------

   *使用参数值设置目标*

------
#### [ Linux & macOS ]

   ```
   aws ssm start-automation-execution \
       --document-name runbook name \
       --targets Key=ParameterValues,Values=value,value 2,value 3 \
       --target-parameter-name parameter name \
       --parameters "input parameter name=input parameter value" \
       --max-concurrency 10 \
       --max-errors 25%
   ```

------
#### [ Windows ]

   ```
   aws ssm start-automation-execution ^
       --document-name runbook name ^
       --targets Key=ParameterValues,Values=value,value 2,value 3 ^
       --target-parameter-name parameter name ^
       --parameters "input parameter name=input parameter value" ^
       --max-concurrency 10 ^
       --max-errors 25%
   ```

------
#### [ PowerShell ]

   ```
   $Targets = New-Object Amazon.SimpleSystemsManagement.Model.Target
   $Targets.Key = "ParameterValues"
   $Targets.Values = "value","value 2","value 3"
   
   Start-SSMAutomationExecution `
       -DocumentName "runbook name" `
       -Targets $Targets `
       -TargetParameterName "parameter name" `
       -Parameter @{"input parameter name"="input parameter value"} `
       -MaxConcurrency "10" `
       -MaxError "25%"
   ```

------

   *使用 AWS Resource Groups 设置目标*

------
#### [ Linux & macOS ]

   ```
   aws ssm start-automation-execution \
       --document-name runbook name \
       --targets Key=ResourceGroup,Values=Resource group nname \
       --target-parameter-name parameter name \
       --parameters "input parameter name=input parameter value" \
       --max-concurrency 10 \
       --max-errors 25%
   ```

------
#### [ Windows ]

   ```
   aws ssm start-automation-execution ^
       --document-name runbook name ^
       --targets Key=ResourceGroup,Values=Resource group name ^
       --target-parameter-name parameter name ^
       --parameters "input parameter name=input parameter value" ^
       --max-concurrency 10 ^
       --max-errors 25%
   ```

------
#### [ PowerShell ]

   ```
   $Targets = New-Object Amazon.SimpleSystemsManagement.Model.Target
   $Targets.Key = "ResourceGroup"
   $Targets.Values = "Resource group name"
   
   Start-SSMAutomationExecution `
       -DocumentName "runbook name" `
       -Targets $Targets `
       -TargetParameterName "parameter name" `
       -Parameter @{"input parameter name"="input parameter value"} `
       -MaxConcurrency "10" `
       -MaxError "25%"
   ```

------

   *将当前 AWS 账户 和 AWS 区域 中的所有 Amazon EC2 实例设置为目标*

------
#### [ Linux & macOS ]

   ```
   aws ssm start-automation-execution \
       --document-name runbook name \
       --targets "Key=AWS::EC2::Instance,Values=*"  \
       --target-parameter-name instanceId \
       --parameters "input parameter name=input parameter value" \
       --max-concurrency 10 \
       --max-errors 25%
   ```

------
#### [ Windows ]

   ```
   aws ssm start-automation-execution ^
       --document-name runbook name ^
       --targets Key=AWS::EC2::Instance,Values=* ^
       --target-parameter-name instanceId ^
       --parameters "input parameter name=input parameter value" ^
       --max-concurrency 10 ^
       --max-errors 25%
   ```

------
#### [ PowerShell ]

   ```
   $Targets = New-Object Amazon.SimpleSystemsManagement.Model.Target
   $Targets.Key = "AWS::EC2::Instance"
   $Targets.Values = "*"
   
   Start-SSMAutomationExecution `
       -DocumentName "runbook name" `
       -Targets $Targets `
       -TargetParameterName "instanceId" `
       -Parameter @{"input parameter name"="input parameter value"} `
       -MaxConcurrency "10" `
       -MaxError "25%"
   ```

------

   该命令将会返回执行 ID。请将该 ID 复制到剪贴板。您可以使用此 ID 查看自动化的状态。

------
#### [ Linux & macOS ]

   ```
   {
       "AutomationExecutionId": "a4a3c0e9-7efd-462a-8594-01234EXAMPLE"
   }
   ```

------
#### [ Windows ]

   ```
   {
       "AutomationExecutionId": "a4a3c0e9-7efd-462a-8594-01234EXAMPLE"
   }
   ```

------
#### [ PowerShell ]

   ```
   a4a3c0e9-7efd-462a-8594-01234EXAMPLE
   ```

------

1. 运行以下命令以查看自动化。将每个 *automation execution ID* 替换为您自己的信息。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-automation-executions \
       --filter Key=ExecutionId,Values=automation execution ID
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-automation-executions ^
       --filter Key=ExecutionId,Values=automation execution ID
   ```

------
#### [ PowerShell ]

   ```
   Get-SSMAutomationExecutionList | `
       Where {$_.AutomationExecutionId -eq "automation execution ID"}
   ```

------

1. 运行以下命令以查看有关自动化进展的详细信息。将每个 *automation execution ID* 替换为您自己的信息。

------
#### [ Linux & macOS ]

   ```
   aws ssm get-automation-execution \
       --automation-execution-id automation execution ID
   ```

------
#### [ Windows ]

   ```
   aws ssm get-automation-execution ^
       --automation-execution-id automation execution ID
   ```

------
#### [ PowerShell ]

   ```
   Get-SSMAutomationExecution `
       -AutomationExecutionId automation execution ID
   ```

------

   系统将返回类似于以下内容的信息。

------
#### [ Linux & macOS ]

   ```
   {
       "AutomationExecution": {
           "StepExecutionsTruncated": false,
           "AutomationExecutionStatus": "Success",
           "MaxConcurrency": "1",
           "Parameters": {},
           "MaxErrors": "1",
           "Outputs": {},
           "DocumentName": "AWS-StopEC2Instance",
           "AutomationExecutionId": "a4a3c0e9-7efd-462a-8594-01234EXAMPLE",
           "ResolvedTargets": {
               "ParameterValues": [
                   "i-02573cafcfEXAMPLE"
               ],
               "Truncated": false
           },
           "ExecutionEndTime": 1564681619.915,
           "Targets": [
               {
                   "Values": [
                       "DEV"
                   ],
                   "Key": "tag:ENV"
               }
           ],
           "DocumentVersion": "1",
           "ExecutionStartTime": 1564681576.09,
           "ExecutedBy": "arn:aws:sts::123456789012:assumed-role/Administrator/Admin",
           "StepExecutions": [
               {
                   "Inputs": {
                       "InstanceId": "i-02573cafcfEXAMPLE"
                   },
                   "Outputs": {},
                   "StepName": "i-02573cafcfEXAMPLE",
                   "ExecutionEndTime": 1564681619.093,
                   "StepExecutionId": "86c7b811-3896-4b78-b897-01234EXAMPLE",
                   "ExecutionStartTime": 1564681576.836,
                   "Action": "aws:executeAutomation",
                   "StepStatus": "Success"
               }
           ],
           "TargetParameterName": "InstanceId",
           "Mode": "Auto"
       }
   }
   ```

------
#### [ Windows ]

   ```
   {
       "AutomationExecution": {
           "StepExecutionsTruncated": false,
           "AutomationExecutionStatus": "Success",
           "MaxConcurrency": "1",
           "Parameters": {},
           "MaxErrors": "1",
           "Outputs": {},
           "DocumentName": "AWS-StopEC2Instance",
           "AutomationExecutionId": "a4a3c0e9-7efd-462a-8594-01234EXAMPLE",
           "ResolvedTargets": {
               "ParameterValues": [
                   "i-02573cafcfEXAMPLE"
               ],
               "Truncated": false
           },
           "ExecutionEndTime": 1564681619.915,
           "Targets": [
               {
                   "Values": [
                       "DEV"
                   ],
                   "Key": "tag:ENV"
               }
           ],
           "DocumentVersion": "1",
           "ExecutionStartTime": 1564681576.09,
           "ExecutedBy": "arn:aws:sts::123456789012:assumed-role/Administrator/Admin",
           "StepExecutions": [
               {
                   "Inputs": {
                       "InstanceId": "i-02573cafcfEXAMPLE"
                   },
                   "Outputs": {},
                   "StepName": "i-02573cafcfEXAMPLE",
                   "ExecutionEndTime": 1564681619.093,
                   "StepExecutionId": "86c7b811-3896-4b78-b897-01234EXAMPLE",
                   "ExecutionStartTime": 1564681576.836,
                   "Action": "aws:executeAutomation",
                   "StepStatus": "Success"
               }
           ],
           "TargetParameterName": "InstanceId",
           "Mode": "Auto"
       }
   }
   ```

------
#### [ PowerShell ]

   ```
   AutomationExecutionId       : a4a3c0e9-7efd-462a-8594-01234EXAMPLE
   AutomationExecutionStatus   : Success
   CurrentAction               : 
   CurrentStepName             : 
   DocumentName                : AWS-StopEC2Instance
   DocumentVersion             : 1
   ExecutedBy                  : arn:aws:sts::123456789012:assumed-role/Administrator/Admin
   ExecutionEndTime            : 8/1/2019 5:46:59 PM
   ExecutionStartTime          : 8/1/2019 5:46:16 PM
   FailureMessage              : 
   MaxConcurrency              : 1
   MaxErrors                   : 1
   Mode                        : Auto
   Outputs                     : {}
   Parameters                  : {}
   ParentAutomationExecutionId : 
   ProgressCounters            : 
   ResolvedTargets             : Amazon.SimpleSystemsManagement.Model.ResolvedTargets
   StepExecutions              : {i-02573cafcfEXAMPLE}
   StepExecutionsTruncated     : False
   Target                      : 
   TargetLocations             : {}
   TargetMaps                  : {}
   TargetParameterName         : InstanceId
   Targets                     : {tag:Name}
   ```

------
**注意**  
您也可以在控制台中监控自动化的状态。在**自动化执行**列表中，请选择您刚才运行的自动化，然后选择**执行步骤**选项卡。该选项卡显示自动化操作的状态。

# 映射自动化目标
<a name="running-automations-map-targets"></a>

通过 `Targets` 参数，可以快速定义自动化的目标资源。例如，如果需要运行一个重新启动托管实例的自动化，不必在控制台中手动选择数十个实例 ID 或在命令中键入它们，您可以通过使用 `Targets` 参数指定 Amazon Elastic Compute Cloud (Amazon EC2) 标签将实例设为目标。

运行使用目标的自动化时，AWS Systems Manager 会为每个目标创建一个子自动化。例如，如果您通过指定标签将 Amazon Elastic Block Store (Amazon EBS) 卷指定为目标，并且这些标签解析为 100 个 Amazon EBS 卷，则 Systems Manager 创建 100 个子自动化。当所有子自动化都到达最终状态时，父自动化完成。

**注意**  
在运行时指定的任何 `input parameters`（在控制台的**输入参数**部分或在命令行中使用 `parameters` 选项）都由所有子自动化自动处理。

您可以使用标签、Resource Groups 和参数值为自动化设置目标资源。此外，您还可以使用 `TargetMaps` 选项在命令行或文件中将多个参数值设为目标。下一节更详细地介绍每个设置目标选项。

## 设置目标标签
<a name="target-tags"></a>

您可以指定单个标签作为自动化目标。多个 AWS 资源支持标签，包括 Amazon Elastic Compute Cloud (Amazon EC2) 和 Amazon Relational Database Service (Amazon RDS) 实例、Amazon Elastic Block Store (Amazon EBS) 卷和快照、Resource Groups 以及 Amazon Simple Storage Service (Amazon S3) 存储桶（仅举几例）。您可以通过设置目标标签在 AWS 资源上快速运行自动化。标签是一种键值对，例如 Operating\$1System:Linux 或 Department:Finance。如果为资源指定一个特定名称，则还可以使用“Name”作为键，将资源名称用作值。

在将标签指定为自动化目标时，还可以指定目标参数。目标参数使用 `TargetParameterName` 选项。通过选择目标参数，可以定义在其上运行自动化的资源的类型。使用标签指定的目标参数必须是运行手册中定义的有效参数。例如，如果要使用标签将数十个 EC2 实例设为目标，请选择 `InstanceId` 目标参数。通过选择此参数，可以将*实例* 定义为自动化的资源类型。在创建自定义运行手册时，您必须将**目标类型**指定为 `/AWS::EC2::Instance`，以确保仅使用实例。否则，所有具有相同标签的资源都将被设置为目标。使用标签定位实例时，可能会包括已终止的实例。

下面的屏幕截图使用 `AWS-DetachEBSVolume` 运行手册。逻辑目标参数为 `VolumeId`。

![\[使用标签作为 Systems Manager Automation 的目标\]](http://docs.aws.amazon.com/zh_cn/systems-manager/latest/userguide/images/automation-rate-control-tags-1-new.png)


`AWS-DetachEBSVolume` 运行手册还包含一个名为**目标类型**的特殊属性，设置为 `/AWS::EC2::Volume`。这意味着，如果标签键对 `Finance:TestEnv` 返回不同类型的资源（例如，EC2 实例、Amazon EBS 卷、Amazon EBS 快照），则仅使用 Amazon EBS 卷。

**重要**  
目标参数名称区分大小写。如果使用 AWS Command Line Interface (AWS CLI) 或 AWS Tools for Windows PowerShell 运行自动化，则必须完全按照运行手册中的定义输入目标参数名称。否则，系统将返回 `InvalidAutomationExecutionParametersException` 错误。您可以使用 [DescribeDocument](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_DescribeDocument.html) API 操作来查看有关特定运行手册中可用目标参数的信息。下面是一个示例 AWS CLI 命令，其中提供有关 `AWS-DeleteSnapshot` 文档的信息。  

```
aws ssm describe-document \
    --name AWS-DeleteSnapshot
```

下面是一些使用标签设置资源目标的示例 AWS CLI 命令。

**示例 1：使用键值对将标签指定为目标以重启 Amazon EC2 实例**

此示例重新启动带有键为 *Department*、值为 *HumanResources* 的标签的所有 Amazon EC2 实例。目标参数使用运行手册中的 *InstanceId* 参数。该示例使用了一个附加参数，通过使用自动化服务角色（也称为*担任角色*）来运行自动化。

```
aws ssm start-automation-execution \
    --document-name AWS-RestartEC2Instance \
    --targets Key=tag:Department,Values=HumanResources \
    --target-parameter-name InstanceId \
    --parameters "AutomationAssumeRole=arn:aws:iam::111122223333:role/AutomationServiceRole"
```

**示例 2：使用键值对将标签指定为目标以删除 Amazon EBS 快照**

下面的示例使用 `AWS-DeleteSnapshot` 运行手册删除键为 *Name*、值为 *January2018Backups* 的所有快照。目标参数使用 *VolumeId* 参数。

```
aws ssm start-automation-execution \
    --document-name AWS-DeleteSnapshot \
    --targets Key=tag:Name,Values=January2018Backups \
    --target-parameter-name VolumeId
```

## 设置目标 AWS Resource Groups
<a name="target-resource-groups"></a>

您可以指定单个 AWS 资源组作为自动化目标。Systems Manager 为目标资源组中的每个对象创建一个子自动化。

例如，假设您的一个 Resource Groups 名为 PatchedAMIs。此资源组包括一个列表，其中有 25 个定期进行修补的 Windows Amazon Machine Images (AMIs)。如果您运行使用 `AWS-CreateManagedWindowsInstance` 运行手册并以此资源组为目标的自动化，则 Systems Manager 会为 25 个 AMIs 中的每一个创建一个子自动化。也就是说，通过将 PatchedAMIs 资源组设置为目标，自动化会从修补的 AMIs 列表中创建 25 个实例。当所有子自动化都完成处理或到达最终状态时，父自动化完成。

下面的 AWS CLI 命令适用于此 PatchAMIs 资源组示例。该命令中，`--target-parameter-name` 选项采用 *AmiId* 参数。该命令不包含定义从每个 AMI 创建的实例类型的附加参数。`AWS-CreateManagedWindowsInstance` 运行手册默认为 t2.medium 实例类型，因此该命令将创建 25 个适用于 Windows Server 的 t2.medium Amazon EC2 实例。

```
aws ssm start-automation-execution \
    --document-name AWS-CreateManagedWindowsInstance \
    --targets Key=ResourceGroup,Values=PatchedAMIs  \
    --target-parameter-name AmiId
```

下面的控制台示例使用名为 t2-micro-instances 的资源组。

![\[将具有 Systems Manager 自动化功能的 AWS 资源组设为目标\]](http://docs.aws.amazon.com/zh_cn/systems-manager/latest/userguide/images/automation-rate-control-resource-groups-new.png)


## 设置目标参数值
<a name="target-parameter-values"></a>

您也可以将参数值设置为目标：输入 `ParameterValues` 作为键，然后输入要在其上运行自动化的特定资源值。如果指定多个值，Systems Manager 将在指定的每个值上运行子自动化。

例如，假设运行手册包含 **InstanceID** 参数。如果在运行此自动化时将 **InstanceID** 参数值设置为目标，则 Systems Manager 会为指定的每个实例 ID 值运行一个子自动化。当自动化完成每个指定实例的运行或执行失败时，父自动化完成。您最多可以将 50 个参数值设置为目标。

以下示例使用 `AWS-CreateImage` 运行手册。指定的目标参数名称为 *InstanceId*。键使用 *ParameterValues*。值为两个 Amazon EC2 实例 ID。此命令为每个实例创建一个自动化，这将从每个实例生成一个 AMI。

```
aws ssm start-automation-execution 
    --document-name AWS-CreateImage \
    --target-parameter-name InstanceId \
    --targets Key=ParameterValues,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE
```

**注意**  
`AutomationAssumeRole` 不是有效的参数。运行以参数值为目标的自动化时，不要选择此项。

### 设置目标参数值映射
<a name="target-maps"></a>

`TargetMaps` 选项扩展了您设置 `ParameterValues` 目标的能力。您可以在命令行中使用 `TargetMaps` 输入一个参数值数组。在命令行中，最多可以指定 50 个参数值。如果要运行指定 50 个以上参数值的命令，请在 JSON 文件中输入这些值。然后，可以在命令行中调用该文件。

**注意**  
控制台中不支持 `TargetMaps` 选项。

在命令中使用 `TargetMaps` 选项以下面的格式指定多个参数值。将每个*示例资源占位符*替换为您自己的信息。

```
aws ssm start-automation-execution \
    --document-name runbook name \
    --target-maps “parameter=value, parameter 2=value, parameter 3=value”  “parameter 4=value, parameter 5=value, parameter 6=value”
```

如果要为 `TargetMaps` 选项输入 50 个以上的参数值，请使用以下 JSON 格式在文件中指定这些值。在提供多个参数值时，使用 JSON 文件还有助于提高可读性。

```
[

    {“parameter”: "value", “parameter 2”: "value", “parameter 3”: "value"},

    {“parameter 4”: "value", “parameter 5”: "value", "parameter 6": "value"}

]
```

使用 .json 文件扩展名保存该文件。可以使用下面的命令调用此文件。将每个*示例资源占位符*替换为您自己的信息。

```
aws ssm start-automation-execution \
    --document-name runbook name \
    –-parameters input parameters \
    --target-maps path to file/file name.json
```

您还可以从 Amazon Simple Storage Service (Amazon S3) 存储桶下载此文件，前提是您有权从存储桶读取数据。使用下面的命令格式。将每个*示例资源占位符*替换为您自己的信息。

```
aws ssm start-automation-execution \
    --document-name runbook name \
    --target-maps http://amzn-s3-demo-bucket.s3.amazonaws.com/file_name.json
```

下面是一个帮助您了解 `TargetMaps` 选项的示例方案。在此方案中，用户想要从不同的 AMIs 创建不同类型的 Amazon EC2 实例。为了执行此任务，用户创建一个名为 AMI\$1Testing 的运行手册。此运行手册定义两个输入参数：`instanceType` 和 `imageId`。

```
{
  "description": "AMI Testing",
  "schemaVersion": "0.3",
  "assumeRole": "{{assumeRole}}",
  "parameters": {
    "assumeRole": {
      "type": "String",
      "description": "Role under which to run the automation",
      "default": ""
    },
    "instanceType": {
      "type": "String",
      "description": "Type of EC2 Instance to launch for this test"
    },
    "imageId": {
      "type": "String",
      "description": "Source AMI id from which to run instance"
    }
  },
  "mainSteps": [
    {
      "name": "runInstances",
      "action": "aws:runInstances",
      "maxAttempts": 1,
      "onFailure": "Abort",
      "inputs": {
        "ImageId": "{{imageId}}",
        "InstanceType": "{{instanceType}}",
        "MinInstanceCount": 1,
        "MaxInstanceCount": 1
      }
    }
  ],
  "outputs": [
    "runInstances.InstanceIds"
  ]
}
```

然后，用户在名为 `AMI_instance_types.json` 的文件中指定以下目标参数值。

```
[
  {
    "instanceType" : ["t2.micro"],     
    "imageId" : ["ami-b70554c8"]     
  },
  {
    "instanceType" : ["t2.small"],     
    "imageId" : ["ami-b70554c8"]     
  },
  {
    "instanceType" : ["t2.medium"],     
    "imageId" : ["ami-cfe4b2b0"]     
  },
  {
    "instanceType" : ["t2.medium"],     
    "imageId" : ["ami-cfe4b2b0"]     
  },
  {
    "instanceType" : ["t2.medium"],     
    "imageId" : ["ami-cfe4b2b0"]     
  }
]
```

用户可以通过运行以下命令来运行此自动化并创建在 `AMI_instance_types.json` 中定义的五个 EC2 实例：

```
aws ssm start-automation-execution \
    --document-name AMI_Testing \
    --target-parameter-name imageId \
    --target-maps file:///home/TestUser/workspace/runinstances/AMI_instance_types.json
```

## 将所有 Amazon EC2 实例设置为目标
<a name="target-all-instances"></a>

您可以通过选择**目标**列表中的**所有实例**，在当前 AWS 账户 和 AWS 区域 中的所有 Amazon EC2 实例上运行自动化。例如，如果要重新启动 AWS 账户 和当前 AWS 区域 的所有 Amazon EC2 实例，您可以选择 `AWS-RestartEC2Instance` 运行手册，然后选择**目标**列表中的**所有实例**。

![\[将运行手册的所有 Amazon EC2 实例设置为目标\]](http://docs.aws.amazon.com/zh_cn/systems-manager/latest/userguide/images/automation-rate-control-target-all-instances.png)


在您选择**所有实例**后，Systems Manager 将填充带有星号 (\$1) 的**实例**字段，并使字段不可更改（该字段呈灰色）。Systems Manager 还会使 **InstanceId** 字段中的**输入参数**字段不可更改。如果您选择将所有实例设置为目标，则使这些字段不可更改是预期行为。

# 大规模控制自动化
<a name="running-automations-scale-controls"></a>

您可以通过指定并发值和错误阈值来控制自动化在 AWS 资源队列上的部署。并发和错误阈值统称为*速率控制*。

**并发**  
通过并发，您可以指定允许同时运行自动化的资源数量。并发有助于在处理自动化时限制对资源的影响或停机时间。您可以指定绝对数量的资源（如 20），也可以指定目标集百分比（如 10%）。

队列系统将自动化传输到单个资源，等到初始调用完成之后，再将自动化发送到其他两个资源。系统以指数增长方式将自动化发送到更多资源，直到达到并发值。

**错误阈值**  
通过错误阈值，您可以指定在 AWS Systems Manager 停止将自动化发送到其他资源之前允许自动化失败的次数。您可以指定绝对数量的错误（如 10），也可以指定目标集百分比（如 10%）。

例如，如果指定错误的绝对数为 3，则系统将在收到第四个错误时停止发送自动化。如果指定 0，则系统会在返回第一个错误结果后停止向其他资源发送自动化。

例如，如果向 50 个实例发送自动化并将错误阈值设置为 10%，则系统会在收到第五个错误时停止向其他实例发送命令。当达到错误阈值时，允许完成已经运行自动化的调用，但是其中一些自动化也可能失败。如果需要确保错误数不超过为错误阈值指定的数量，请将**并发**值设置为 1，以便一次只处理一个自动化。