演練:建立維護時段以更新 SSM Agent (AWS CLI) - AWS Systems Manager

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

演練:建立維護時段以更新 SSM Agent (AWS CLI)

以下的演練說明如何使用 AWS Command Line Interface (AWS CLI) 來建立 AWS Systems Manager 維護時段。此演練也說明如何將受管節點註冊為目標和註冊 Systems Manager Run Command 任務來更新 SSM Agent。

開始之前

完成下列程序前,您必須在想要設定的節點上具有管理員許可,或已在 AWS Identity and Access Management (IAM) 中被授予適當的許可。此外,請確認您的混合多雲端環境中有至少一個已針對 Systems Manager 設定的執行中 Linux 或 Windows Server 受管節點。如需更多詳細資訊,請參閱 設定 AWS Systems Manager

步驟 1:開始使用

使用 AWS CLI 執行命令
  1. 如果您尚未安裝並設定 AWS Command Line Interface (AWS CLI),請進行相應的操作。

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

  2. 確認有節點可註冊為維護時段的目標。

    執行以下命令來檢視哪些節點可供註冊。

    aws ssm describe-instance-information --query "InstanceInformationList[*]"

    使用下列命令來檢視特定節點的詳細資訊。

    aws ssm describe-instance-information --instance-information-filter-list key=InstanceIds,valueSet=instance-id

步驟 2:建立維護時段

請使用下列程序來建立維護時段並指定其基本選項,例如排程和持續時間。

建立維護時段 (AWS CLI)
  1. 開啟 AWS CLI 並執行以下命令,來建立在美國太平洋時區每週日 02:00 執行的維護時段,截止值為 1 小時。

    Linux & macOS
    aws ssm create-maintenance-window \ --name "My-First-Maintenance-Window" \ --schedule "cron(0 2 ? * SUN *)" \ --duration 2 \ --schedule-timezone "America/Los_Angeles" \ --cutoff 1 \ --no-allow-unassociated-targets
    Windows
    aws ssm create-maintenance-window ^ --name "My-First-Maintenance-Window" ^ --schedule "cron(0 2 ? * SUN *)" ^ --duration 2 ^ --schedule-timezone "America/Los_Angeles" ^ --cutoff 1 ^ --no-allow-unassociated-targets

    如需有關替 schedule 參數建立 cron 表達式的詳細資訊,請參閱參考:Systems Manager 的 Cron 和 Rate 運算式

    如需維護時段的各種排程相關選項彼此之間有何關聯的說明,請參閱 維護時段排程與作用期間選項

    如需使用 --schedule 操作的詳細資訊,請參閱參考:Systems Manager 的 Cron 和 Rate 運算式

    系統會傳回如下資訊。

    {
       "WindowId":"mw-0c50858d01EXAMPLE"
    }
  2. 請執行下列命令,列出在目前 AWS 區域 區域的 AWS 帳戶 中建立的這個維護時段和任何其他維護時段。

    aws ssm describe-maintenance-windows

    系統會傳回如下資訊。

    {
        "WindowIdentities": [
            {
                "Cutoff": 1,
                "Name": "My-First-Maintenance-Window",
                "NextExecutionTime": "2019-02-03T02:00-08:00",
                "Enabled": true,
                "WindowId": "mw-0c50858d01EXAMPLE",
                "Duration": 2
            }
        ]
    }

步驟 3:註冊維護時段目標 (AWS CLI)

請使用下列程序,向您在步驟 2 中建立的維護時段註冊目標。透過註冊目標,您就能指定要更新哪些節點。

註冊維護時段目標 (AWS CLI)
  1. 執行下列命令。將每個範例資源預留位置取代為您自己的資訊。

    Linux & macOS
    aws ssm register-target-with-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --target "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" \ --resource-type "INSTANCE"
    Windows
    aws ssm register-target-with-maintenance-window ^ --window-id "mw-0c50858d01EXAMPLE" ^ --target "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" ^ --resource-type "INSTANCE"

    系統會傳回類似以下資訊,其中包含維護時段目標 ID。複製或記下 WindowTargetId 值。您必須在後續步驟中指定此 ID 來註冊此維護時段的任務。

    {
       "WindowTargetId":"1a2b3c4d-1a2b-1a2b-1a2b-1a2b3c4d-1a2"
    }
    替代命令

    使用以下命令來註冊多個受管節點。

    Linux & macOS
    aws ssm register-target-with-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE" \ --resource-type "INSTANCE"
    Windows
    aws ssm register-target-with-maintenance-window ^ --window-id "mw-0c50858d01EXAMPLE" ^ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE,i-0471e04240EXAMPLE" ^ --resource-type "INSTANCE"

    使用以下命令,透過使用標籤來註冊節點。

    Linux & macOS
    aws ssm register-target-with-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --targets "Key=tag:Environment,Values=Prod" "Key=tag:Role,Values=Web" \ --resource-type "INSTANCE"
    Windows
    aws ssm register-target-with-maintenance-window ^ --window-id "mw-0c50858d01EXAMPLE" ^ --targets "Key=tag:Environment,Values=Prod" "Key=tag:Role,Values=Web" ^ --resource-type "INSTANCE"
  2. 執行以下命令來顯示維護時段的目標。

    aws ssm describe-maintenance-window-targets --window-id "mw-0c50858d01EXAMPLE"

    系統會傳回如下資訊。

    {
        "Targets": [
            {
                "ResourceType": "INSTANCE",
                "WindowId": "mw-0c50858d01EXAMPLE",
                "Targets": [
                    {
                        "Values": [
                            "i-02573cafcfEXAMPLE"
                        ],
                        "Key": "InstanceIds"
                    }
                ],
                "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
            },
            {
                "ResourceType": "INSTANCE",
                "WindowId": "mw-0c50858d01EXAMPLE",
                "Targets": [
                    {
                        "Values": [
                            "Prod"
                        ],
                        "Key": "tag:Environment"
                    },
                    {
                        "Values": [
                            "Web"
                        ],
                        "Key": "tag:Role"
                    }
                ],
                "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
            }
        ]
    }

步驟 4:註冊維護時段的Run Command任務以更新 SSM Agent

使用下列程序,為您在步驟 2 建立的維護時段註冊 Run Command 任務。Run Command 任務會在已註冊目標上更新 SSM Agent。

註冊維護時段的 Run Command 任務以更新 SSM Agent (AWS CLI)
  1. 執行以下命令,使用步驟 3 的 WindowTargetId 值,為維護時段註冊 Run Command 任務。將每個範例資源預留位置取代為您自己的資訊。此任務會透過使用 AWS-UpdateSSMAgent 文件來更新 SSM Agent。

    Linux & macOS
    aws ssm register-task-with-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --task-arn "AWS-UpdateSSMAgent" \ --name "UpdateSSMAgent" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --service-role-arn "arn:aws:iam:account-id:role/MW-Role" \ --task-type "RUN_COMMAND" \ --max-concurrency 1 --max-errors 1 --priority 10
    Windows
    aws ssm register-task-with-maintenance-window ^ --window-id "mw-0c50858d01EXAMPLE" ^ --task-arn "AWS-UpdateSSMAgent" ^ --name "UpdateSSMAgent" ^ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ^ --service-role-arn "arn:aws:iam:account-id:role/MW-Role" ^ --task-type "RUN_COMMAND" ^ --max-concurrency 1 --max-errors 1 --priority 10
    注意

    如果您在先前步驟中註冊的目標是 Windows Server 2012 R2 或更早的版本,您必須使用 AWS-UpdateEC2Config 文件。

    系統會傳回如下資訊。

    {
       "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE"
    }
  2. 執行下列命令列出所有維護時段的註冊任務。

    aws ssm describe-maintenance-window-tasks --window-id "mw-0c50858d01EXAMPLE"

    系統會傳回如下資訊。

    {
        "Tasks": [
            {
                "ServiceRoleArn": "arn:aws:iam::111122223333:role/MW-Role",
                "MaxErrors": "1",
                "TaskArn": "AWS-UpdateSSMAgent",
                "MaxConcurrency": "1",
                "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
                "TaskParameters": {},
                "Priority": 10,
                "WindowId": "mw-0c50858d01EXAMPLE",
                "Type": "RUN_COMMAND",
                "Targets": [
                    {
                        "Values": [
                            "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE"
                        ],
                        "Key": "WindowTargetIds"
                    }
                ],
                "Name": "UpdateSSMAgent"
            }
        ]
    }