演练:创建维护时段来更新 SSM Agent (Amazon CLI) - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

演练:创建维护时段来更新 SSM Agent (Amazon CLI)

以下演练向您演示了如何使用 Amazon Command Line Interface (Amazon CLI) 创建 Amazon Systems Manager 维护时段。本演练还介绍了如何将您的托管式节点注册为目标,以及如何注册 Systems Manager Run Command 任务来更新 SSM Agent。

开始前的准备工作

您必须拥有要配置的节点的管理员权限,或必须已获得 Amazon Identity and Access Management (IAM) 中的适当权限,才能完成以下过程。此外,请验证并确保在混合和多云环境中至少有一个为 Systems Manager 配置的正在运行的 Linux 或 Windows Server 托管式节点。有关更多信息,请参阅 设置 Amazon Systems Manager

步骤 1:开始使用

使用 Amazon CLI 运行命令
  1. 安装并配置 Amazon Command Line Interface (Amazon CLI)(如果尚未执行该操作)。

    有关信息,请参阅安装或更新 Amazon CLI 的最新版本

  2. 确认节点已准备好注册为维护时段的目标。

    运行以下命令来查看哪些节点在线。

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

    运行以下命令来查看有关特定节点的详细信息。

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

步骤 2:创建维护时段

使用以下过程创建维护时段并指定其基本选项,如计划和持续时间。

创建维护时段 (Amazon CLI)
  1. 打开 Amazon 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. 要列出此信息以及在您当前的 Amazon Web Services 区域 内的 Amazon Web Services 账户中创建的任何其他维护时段,请运行以下命令。

    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:注册维护时段目标 (Amazon CLI)

使用以下过程向您在步骤 2 中创建的维护时段注册目标。通过注册目标,您需指定要更新的节点。

注册维护时段目标 (Amazon 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

使用以下过程为您在步骤 1 中创建的维护时段注册 Run Command 任务。Run Command 任务更新已注册目标上的 SSM Agent。

为维护时段注册 Run Command 任务以更新 SSM Agent (Amazon 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"
            }
        ]
    }