大规模运行自动化 - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

大规模运行自动化

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

有关并发性和错误阈值的更多信息,请参阅 大规模控制自动化。有关目标的更多信息,请参阅 映射自动化目标

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

运行具有目标和速率控制自动化(控制台)

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

运行具有目标和速率控制的自动化
  1. 访问 https://console.aws.amazon.com/systems-manager/,打开 Amazon Systems Manager 控制台。

  2. 在导航窗格中,选择自动化,然后选择执行自动化

  3. 自动化文档列表中,请选择运行手册。在文档类别窗格中选择一个或多个选项,以便根据 SSM 文档的用途对其进行筛选。要查看您拥有的运行手册,请选择我拥有的选项卡。要查看与您的账户共享的运行手册,请选择与我共享选项卡。要查看所有运行手册,请选择所有文档选项卡。

    注意

    您可以通过选择运行手册名称来查看有关该手册的信息。

  4. 文档详细信息部分中,验证文档版本已设置为要运行的版本。系统包括以下版本选项:

    • 运行时的默认版本 – 如果定期更新自动化运行手册并分配新的默认版本,请选择此选项。

    • 运行时的最新版本 – 如果定期更新自动化运行手册并且想要运行最新更新的版本,请选择此选项。

    • 1(默认) – 选择此选项可执行文档的第一个版本,即默认版本。

  5. 选择下一步

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

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

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

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

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

      2. 如果选择使用 Amazon Resource Groups 将资源设置为目标,请从资源组列表中选择组的名称。

      3. 如果选择使用标签将资源设置为目标,请在提供的字段中输入标签键和(可选)标签值。选择添加

      4. 如果要在当前 Amazon Web Services 账户 和 Amazon Web Services 区域 中的所有实例上运行自动化运行手册,则选择所有实例

  8. 输入参数 部分中,指定所需的输入。或者,您也可以从 AutomationAssumeRole 列表中选择一个 IAM 服务角色。

    注意

    您可能不需要选择输入参数部分中的某些选项。原因是您使用标签或资源组将多个资源设置为目标。例如,如果选择了 AWS-RestartEC2Instance 运行手册,则无需在输入参数 部分中指定或选择实例 ID。自动化执行过程使用您指定的标签资源组查找要重新启动的实例。

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

    并发部分中,选择一个选项:

    • 选择目标,以输入可同时运行自动化工作流目标的绝对数量。

    • 选择百分比,以输入可同时运行自动化工作流的目标集的百分比。

  10. 错误阈值部分中,选择一个选项:

    • 选择错误,以输入自动化停止将工作流程发送到其他资源之前允许的错误的绝对数量。

    • 选择百分比,以输入自动化停止将工作流程发送到其他资源之前允许的错误的百分比。

  11. (可选)选择一个 CloudWatch 警报以应用于您的自动化进行监控。要将 CloudWatch 警报附加到自动化,启动自动化的 IAM 主体必须具有 iam:createServiceLinkedRole 操作的权限。有关 CloudWatch 警报的更多信息,请参阅使用 Amazon CloudWatch 警报。请注意,如果您的警报激活,自动化将停止。如果使用 Amazon CloudTrail,您将在跟踪中看到 API 调用。

  12. 选择执行

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

运行具有目标和速率控制的自动化(命令行)

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

运行具有目标和速率控制的自动化
  1. 安装并配置 Amazon CLI 或 Amazon Tools for PowerShell(如果尚未执行该操作)。

    有关信息,请参阅安装或更新 Amazon CLI 的最新版本以及安装 Amazon Tools for PowerShell

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

    Linux & macOS
    aws ssm list-documents
    Windows
    aws ssm list-documents
    PowerShell
    Get-SSMDocumentList

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

  3. 运行以下命令以查看有关运行手册的详细信息。用您想要查看其详细信息的运行手册的名称替换 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
  4. 创建一个要运行的使用目标和速率控制选项的命令。将每个示例资源占位符替换为您自己的信息。

    使用标签设置目标

    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%"

    使用 Amazon 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%"

    将当前 Amazon Web Services 账户 和 Amazon Web Services 区域 中的所有 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
  5. 运行以下命令以查看自动化。将每个 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"}
  6. 运行以下命令以查看有关自动化进展的详细信息。将每个 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}
    注意

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