步驟 3:向維護時段註冊任務 (AWS CLI) - AWS Systems Manager

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

步驟 3:向維護時段註冊任務 (AWS CLI)

在教學課程的這個步驟中,您會註冊 AWS Systems Manager Run Command 任務,可在適用於 Linnux 的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上執行 df 命令。此標準 Linux 命令的結果會顯示有多少可用空間,以及執行個體磁碟檔案系統上使用多少空間。

-或-

如果您以 Windows Server 的 Amazon EC2 執行個體為目標 (而不是 Linux),請在下列命令中以 ipconfig 取代 df。此命令中的輸出會列出在目標執行個體上適用於轉接器的 IP 地址、子網路遮罩以及預設閘道的詳細資訊。

當您準備好註冊其他任務類型,或使用更多可用 Systems Manager Run Command 選項時,請參閱 範例:向維護時段註冊任務。目前,我們提供所有四個任務類型的詳細資訊,以及其中一些最重要的選項,以協助您規劃更廣泛的真實世界案例。

向維護時段註冊任務
  1. 在本機機器上執行以下命令。將每個範例資源預留位置取代為您自己的資訊。從本機 Windows 機器執行的版本包含逸出字元 (「/」),您在透過命令列工具執行命令時會需要這些字元。

    Linux & macOS
    aws ssm register-task-with-maintenance-window \ --window-id mw-0c50858d01EXAMPLE \ --task-arn "AWS-RunShellScript" \ --max-concurrency 1 --max-errors 1 \ --priority 10 \ --targets "Key=InstanceIds,Values=i-0471e04240EXAMPLE" \ --task-type "RUN_COMMAND" \ --task-invocation-parameters '{"RunCommand":{"Parameters":{"commands":["df"]}}}'
    Windows
    aws ssm register-task-with-maintenance-window ^ --window-id mw-0c50858d01EXAMPLE ^ --task-arn "AWS-RunShellScript" ^ --max-concurrency 1 --max-errors 1 ^ --priority 10 ^ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE" ^ --task-type "RUN_COMMAND" ^ --task-invocation-parameters={\"RunCommand\":{\"Parameters\":{\"commands\":[\"df\"]}}}

    系統會傳回與以下相似的資訊:

    {
        "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE"
    }
  2. 立即執行以下命令來檢視與您建立之維護時段任務相關的詳細資訊。

    Linux & macOS
    aws ssm describe-maintenance-window-tasks \ --window-id mw-0c50858d01EXAMPLE
    Windows
    aws ssm describe-maintenance-window-tasks ^ --window-id mw-0c50858d01EXAMPLE
  3. 系統會傳回與以下相似的資訊。

    {
        "Tasks": [
            {
                "WindowId": "mw-0c50858d01EXAMPLE",
                "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
                "TaskArn": "AWS-RunShellScript",
                "Type": "RUN_COMMAND",
                "Targets": [
                    {
                        "Key": "InstanceIds",
                        "Values": [
                            "i-02573cafcfEXAMPLE"
                        ]
                    }
                ],
                "TaskParameters": {},
                "Priority": 10,
                "ServiceRoleArn": "arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole",
                "MaxConcurrency": "1",
                "MaxErrors": "1"
            }
        ]
    }
  4. 根據您在 步驟 1:建立維護時段 (AWS CLI) 指定的排程,等到任務的執行時間。例如,如果您已指定 --schedule "rate(5 minutes)",請等待五分鐘。然後執行以下命令,來檢視與此任務發生的任何執行所相關的資訊。

    Linux & macOS
    aws ssm describe-maintenance-window-executions \ --window-id mw-0c50858d01EXAMPLE
    Windows
    aws ssm describe-maintenance-window-executions ^ --window-id mw-0c50858d01EXAMPLE

    系統會傳回與以下相似的資訊。

    {
        "WindowExecutions": [
            {
                "WindowId": "mw-0c50858d01EXAMPLE",
                "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
                "Status": "SUCCESS",
                "StartTime": 1557593493.096,
                "EndTime": 1557593498.611
            }
        ]
    }
提示

任務順利執行後,您可以降低維護時段執行的速率。例如,執行以下命令來將頻率降低為一週一次。將 mw-0c50858d01EXAMPLE 取代為您自己的資訊。

Linux & macOS
aws ssm update-maintenance-window \ --window-id mw-0c50858d01EXAMPLE \ --schedule "rate(7 days)"
Windows
aws ssm update-maintenance-window ^ --window-id mw-0c50858d01EXAMPLE ^ --schedule "rate(7 days)"

如需管理維護時段排程的詳細資訊,請參閱參考:Systems Manager 的 Cron 和 Rate 運算式維護時段排程與作用期間選項

如需使用 AWS Command Line Interface (AWS CLI) 來修改維護時段的詳細資訊,請參閱 教學課程:更新維護時段 (AWS CLI)

如需練習執行 AWS CLI 命令,來檢視維護時段任務及其執行的更多詳細資訊,請繼續進行教學課程:檢視任務和任務執行的相關資訊 (AWS CLI)

關於教學命令輸出

檢視與維護時段任務執行相關聯之 Run Command 命令的 輸出的 AWS CLI 用法已超出此教學的範圍。

不過,您可以使用 AWS CLI 來檢視此資料。(您也可以在 Systems Manager 主控台中或在 Amazon Simple Storage Service (Amazon S3) 儲存貯體中存放的日誌檔檢視輸出 (如果您已將維護時段設定為在前述日誌檔中存放命令輸出)。) 您會發現在 Linux EC2 執行個體上的 df 命令輸出與以下內容類似。

Filesystem 1K-blocks Used Available Use% Mounted on

devtmpfs 485716 0 485716 0% /dev

tmpfs 503624 0 503624 0% /dev/shm

tmpfs 503624 328 503296 1% /run

tmpfs 503624 0 503624 0% /sys/fs/cgroup

/dev/xvda1 8376300 1464160 6912140 18% /

在 Windows Server EC2 執行個體的 ipconfig 命令輸出與以下內容類似:

Windows IP Configuration


Ethernet adapter Ethernet 2:

   Connection-specific DNS Suffix  . : example.com
   IPv4 Address. . . . . . . . . . . : 10.24.34.0/23
   Subnet Mask . . . . . . . . . . . : 255.255.255.255
   Default Gateway . . . . . . . . . : 0.0.0.0

Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : abc1.wa.example.net

Wireless LAN adapter Local Area Connection* 1:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::100b:c234:66d6:d24f%4
   IPv4 Address. . . . . . . . . . . : 192.0.2.0
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.0.2.0

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :