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

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

大規模執行自動化

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

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

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

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

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

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

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

  3. Automation document (自動化文件) 清單中,選擇 Runbook。在 Document categories (文件類別) 窗格中選擇一個或多個選項,根據 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. Targets (目標) 部分,選擇您想如何將要執行自動化的 AWS 資源設為目標。這些選項是必要的。

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

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

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

      2. 如果您選擇使用 AWS Resource Groups將資源設為目標,請從 Resource Group (資源群組) 清單選擇群組的名稱。

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

      4. 如果您想要在當前 AWS 帳戶 和 AWS 區域 的所有執行個體上執行 Automation Runbook,則選擇 All instances (所有執行個體)。

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

    注意

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

  9. 使用 Rate control (速率控制) 部分中的選項限制可在每個帳戶區域對自動化的 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 名稱取代為您要檢視其詳細資料的 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. 建立命令以使用您想要執行的目標和速率控制選項。將每個範例資源預留位置取代為您自己的資訊。

    「使用標籤設定目標」

    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
  5. 執行以下命令檢視自動化。把每個自動化執行 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. 執行以下命令檢視自動化進度的詳細資訊。把每個自動化執行 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 (執行步驟) 標籤。此索引標籤會顯示自動化動作的狀態。