大規模執行自動化作業 - AWS Systems Manager

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

大規模執行自動化作業

透過 AWS Systems Manager 自動化,您可以使用目標對一系列 AWS 資源執行自動化作業。此外,您可以藉由指定並行值和錯誤閾值來控制自動化在機群中的部署。並行和錯誤閾值功能統稱為「速率控制」。並行值會決定允許以多少資源同時執行自動化。Automation 還提供了可以選用的自適應並行模式。自適應並行會自動將自動化配額從 100 個並行執行自動化擴展到 500 個。錯誤閾值會決定在 Systems Manager 停止傳送自動化至其他資源之前允許多少次自動化失敗。

如需並行和錯誤閾值的詳細資訊,請參閱大規模控制自動化。如需目標的詳細資訊,請參閱對應自動化的目標

下列程序說明如何開啟自適應並行,以及如何借助 Systems Manager 主控台和 AWS Command Line Interface (AWS CLI) 來使用目標和速率控制執行自動化。

以目標和速率控制執行自動化 (主控台)

以下程序說明如何使用 Systems Manager 主控台,以目標和速率控制來執行自動化。

以目標和速率控制執行自動化
  1. 在開啟 AWS Systems Manager 主控台https://console.aws.amazon.com/systems-manager/

  2. 在導覽窗格中,選擇 Automation (自動化),接著選擇 Execute automation (執行自動化)

  3. Automation document (自動化文件)清單中,選擇 Runbook。在 [文件類別] 窗格中選擇一或多個選項,以根據SSM文件用途篩選文件。若要檢視您擁有的 Runbook,請選擇 Owned by me (我所擁有的) 索引標籤。若要檢視與您帳戶共用的 Runbook,請選擇 Shared with me (與我共用的) 索引標籤。若要檢視所有 Runbook,請選擇 All documents (所有文件) 索引標籤。

    注意

    您可以選擇 Runbook 名稱檢視 Runbook 資訊。

  4. Document details (文件詳細資訊) 部分,確認 Document version (文件版本) 設定為您想要執行的版本。系統包括以下版本選項:

    • 執行期的預設版本:如果 Automation 執行手冊會定期更新且已指派新的預設版本,則請選擇此選項。

    • 執行期的最新版本:如果 Automation 執行手冊會定期更新,而您想要執行最近更新的版本,請選擇此選項。

    • 1 (預設):選擇此選項以執行文件的第一個版本,也是預設版本。

  5. 選擇 Next (下一步)

  6. Execution Mode (執行模式) 部分,選擇 Rate Control (速率控制)。如果想要使用目標和速率控制,就必須使用此模式或 Multi-account and Region (多帳戶和區域)

  7. 在「目」段落中,選擇要以 AWS 資源為目標的目標方式來執行「自動化」。這些選項是必要的。

    1. 使用 Parameter (參數) 清單選擇一個參數。Parameter (參數) 清單中的項目,是由您在此程序一開始所選取自動化 Runbook 中的參數決定。藉由選擇參數,您就會定義自動化工作流程執行的資源類型。

    2. 使用 Targets (目標) 清單選擇您想要如何將資源設為目標。

      1. 如果您選擇使用參數值將資源設為目標,則請在 Input parameters (輸入參數) 區段為您所選的參數輸入參數值。

      2. 如果您選擇使用來鎖定資源的目標 AWS Resource Groups,請從資源群組清單中選擇群組的名稱。

      3. 如果您選擇使用標籤將資源設為目標,請在提供的欄位中輸入標籤索引鍵 (選用) 和標籤值。選擇新增

      4. 如果您想要在目前 AWS 帳戶 的所有執行個體上執行自動化 Runbook AWS 區域,請選擇 [所有執行個體]。

  8. Input parameters (輸入參數) 部分,指定所需的輸入。或者,您可以從AutomationAssumeRole清單中選擇IAM服務角色。

    注意

    您可能不必選擇 Input parameters (輸入參數) 部分的其中一些選項。這是因為您使用標籤或資源群組將資源設為目標。例如,如果您選擇 AWS-RestartEC2Instance runbook,則不需要在 [輸入參數] 區段IDs中指定或選擇執行個體。自動化執行會使用您指定的標籤或資源組找出要重新啟動的執行個體。

  9. 使用「費率控制」區段中的選項,限制可在每個帳戶-區域配對中執行「自動化」的 AWS 資源數目。

    Concurrency (並行) 部分,選擇一個選項:

    • 選擇 targets (目標),輸入可以同時執行自動化工作流程的目標絕對數量。

    • 選擇 percentage (百分比),輸入可以同時執行自動化工作流程的目標集百分比。

  10. Error threshold (錯誤閾值) 部分,選擇一個選項:

    • 選擇 errors (錯誤),輸入在自動化停止傳送工作流程至其他資源之前允許的錯誤絕對數量。

    • 選擇 percentage (百分比),輸入在自動化停止傳送工作流程至其他資源之前允許的錯誤百分比。

  11. (選擇性) 選擇要套用至您的自動化動作以進行監控的 CloudWatch 警示。若要將 CloudWatch 警示附加至您的自動化操作,啟動自動化操作的IAM主體必須具有iam:createServiceLinkedRole動作的權限。如需有關 CloudWatch 警示的詳細資訊,請參閱使用 Amazon CloudWatch 警示。請注意,如果您的警示啟用,則會停止自動化。如果使用 AWS CloudTrail,則會在追蹤中看到API通話。

  12. 選擇 Execute (執行)

若要檢視由您的速率控制自動化啟動的自動化,請在導覽窗格中選擇 Automation,接著選取 Show child automations (顯示子系自動化)。

以目標和速率控制執行自動化 (命令列)

下列程序說明如何使用 AWS CLI (在 Linux 或 Windows 上) 或 AWS Tools for PowerShell 執行具有目標和速率控制項的自動化操作。

以目標和速率控制執行自動化
  1. 安裝和配置 AWS CLI 或 AWS Tools for PowerShell,如果您尚未安裝。

    如需相關資訊,請參閱安裝或更新 AWS CLI的最新版本安裝 AWS Tools for PowerShell

  2. 執行以下命令來檢視文件清單。

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

    請注意您要使用的 Runbook 名稱。

  3. 執行以下命令來檢視您建立的 Runbook 詳細資訊。更換 runbook name 與您要查看其詳細信息的 runbook 的名稱。此外,記下您希望用於--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. 建立命令以使用您想要執行的目標和速率控制選項。替換每個 example resource placeholder 使用您自己的信息。

    「使用標籤設定目標」

    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 帳戶 和中的所有 EC2 Amazon 實例 AWS 區域

    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}
    注意

    您也可以在主控台中監控自動化的狀態。在 Automation executions (自動化執行清單) 中,選擇您剛執行的自動化,接著選擇 Execution steps (執行步驟) 標籤。此索引標籤會顯示自動化動作的狀態。