本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
本教學課程示範如何使用 AWS Command Line Interface (AWS CLI) 來更新維護時段。它也說明如何更新不同的任務類型,包括 AWS Systems Manager Run Command和 自動化的任務類型 AWS Lambda,以及 AWS Step Functions。
本節中的範例使用以下 Systems Manager 動作來更新維護時段。
如需如何使用 Systems Manager 主控台更新維護時段的資訊,請參閱 使用主控台更新或刪除維護時段資源。
當您按照此教學課程中的步驟,使用自己的選項和 ID 來取代斜體紅色
文字。例如,使用您所建立之資源 ID 取代維護時段 ID mw-0c50858d01EXAMPLE
和執行個體 ID i-02573cafcfEXAMPLE
。
使用 更新維護時段 AWS CLI
-
開啟 AWS CLI 並執行下列命令來更新目標,以包含名稱和描述。
aws ssm update-maintenance-window-target \ --window-id "
mw-0c50858d01EXAMPLE
" \ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE
" \ --name "My-Maintenance-Window-Target
" \ --description "Description for my maintenance window target
"系統會傳回與以下相似的資訊。
{ "WindowId": "mw-0c50858d01EXAMPLE", "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE", "Targets": [ { "Key": "InstanceIds", "Values": [ "i-02573cafcfEXAMPLE" ] } ], "Name": "My-Maintenance-Window-Target", "Description": "Description for my maintenance window target" }
-
執行以下命令來使用
replace
選項移除描述欄位,並新增額外的目標。移除描述欄位,因為更新後不包括此欄位 (空值)。請確定已指定一個額外的執行個體,此節點已設定為與 Systems Manager 搭配使用。aws ssm update-maintenance-window-target \ --window-id "
mw-0c50858d01EXAMPLE
" \ --window-target-id "d208dedf-3f6b-41ff-ace8-8e751EXAMPLE
" \ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE
,i-0471e04240EXAMPLE
" \ --name "My-Maintenance-Window-Target
" \ --replace系統會傳回與以下相似的資訊。
{ "WindowId": "mw-0c50858d01EXAMPLE", "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE", "Targets": [ { "Key": "InstanceIds", "Values": [ "i-02573cafcfEXAMPLE", "i-0471e04240EXAMPLE" ] } ], "Name": "My-Maintenance-Window-Target" }
-
start-date
允許您延遲啟用維護時段直到指定的未來日期。end-date
選項可讓您設定不再執行維護時段的未來日期和時間點。以 ISO-8601 Extended 格式指定選項。執行以下命令,來指定定期排定維護時段執行的日期和時間範圍。
aws ssm update-maintenance-window \ --window-id "
mw-0c50858d01EXAMPLE
" \ --start-date "2020-10-01T10:10:10Z
" \ --end-date "2020-11-01T10:10:10Z
" -
執行下列命令以更新 Run Command 任務
提示
如果您的目標是適用於 Windows Server 的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體,則將下列命令中的
df
變更為ipconfig
,以及AWS-RunShellScript
變更為AWS-RunPowerShellScript
。aws ssm update-maintenance-window-task \ --window-id "
mw-0c50858d01EXAMPLE
" \ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE
" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE
" \ --task-arn "AWS-RunShellScript" \ --service-role-arn "arn:aws:iam::account-id
:role/MaintenanceWindowsRole" \ --task-invocation-parameters "RunCommand={Comment=Revising my Run Command task
,Parameters={commands=df
}}" \ --priority1
--max-concurrency10
--max-errors4
\ --name "My-Task-Name
" --description "A description for my Run Command task
"系統會傳回與以下相似的資訊。
{ "WindowId": "mw-0c50858d01EXAMPLE", "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE", "Targets": [ { "Key": "WindowTargetIds", "Values": [ "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ] } ], "TaskArn": "AWS-RunShellScript", "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole", "TaskParameters": {}, "TaskInvocationParameters": { "RunCommand": { "Comment": "Revising my Run Command task", "Parameters": { "commands": [ "df" ] } } }, "Priority": 1, "MaxConcurrency": "10", "MaxErrors": "4", "Name": "My-Task-Name", "Description": "A description for my Run Command task" }
-
調整並執行下列命令以更新 Lambda 任務。
aws ssm update-maintenance-window-task \ --window-id
mw-0c50858d01EXAMPLE
\ --window-task-id4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE
\ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE
" \ --task-arn "arn:aws:lambda:region
:111122223333:function:SSMTestLambda" \ --service-role-arn "arn:aws:iam:account-id
:role/MaintenanceWindowsRole" \ --task-invocation-parameters '{"Lambda":{"Payload":"{\"InstanceId\":\"{{RESOURCE_ID}}\",\"targetType\":\"{{TARGET_TYPE}}\"}"}}' \ --priority1
--max-concurrency10
--max-errors5
\ --name "New-Lambda-Task-Name
" \ --description "A description for my Lambda task
"系統會傳回與以下相似的資訊。
{ "WindowId": "mw-0c50858d01EXAMPLE", "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE", "Targets": [ { "Key": "WindowTargetIds", "Values": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" } ], "TaskArn": "arn:aws:lambda:us-east-2:111122223333:function:SSMTestLambda", "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole", "TaskParameters": {}, "TaskInvocationParameters": { "Lambda": { "Payload": "e30=" } }, "Priority": 1, "MaxConcurrency": "10", "MaxErrors": "5", "Name": "New-Lambda-Task-Name", "Description": "A description for my Lambda task" }
-
如果您更新的是 Step Functions 任務,請調整和執行以下命令來更新其 task-invocation-parameters。
aws ssm update-maintenance-window-task \ --window-id "
mw-0c50858d01EXAMPLE
" \ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE
" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE
" \ --task-arn "arn:aws:states:region
:execution:SSMStepFunctionTest" \ --service-role-arn "arn:aws:iam:account-id
:role/MaintenanceWindowsRole" \ --task-invocation-parameters '{"StepFunctions":{"Input":"{\"InstanceId\":\"{{RESOURCE_ID}}\"}"}}' \ --priority0
--max-concurrency10
--max-errors5
\ --name "My-Step-Functions-Task
" \ --description "A description for my Step Functions task
"系統會傳回與以下相似的資訊。
{ "WindowId": "mw-0c50858d01EXAMPLE", "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE", "Targets": [ { "Key": "WindowTargetIds", "Values": [ "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ] } ], "TaskArn": "arn:aws:states:us-east-2:111122223333:execution:SSMStepFunctionTest", "ServiceRoleArn": "arn:aws:iam::111122223333:role/MaintenanceWindowsRole", "TaskParameters": {}, "TaskInvocationParameters": { "StepFunctions": { "Input": "{\"instanceId\":\"{{RESOURCE_ID}}\"}" } }, "Priority": 0, "MaxConcurrency": "10", "MaxErrors": "5", "Name": "My-Step-Functions-Task", "Description": "A description for my Step Functions task" }
-
執行下列命令替維護時段取消註冊任務。此範例使用
safe
參數來判斷任次參考此目標,因此能夠安全的取消註冊。aws ssm deregister-target-from-maintenance-window \ --window-id "
mw-0c50858d01EXAMPLE
" \ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE
" \ --safe系統會傳回與以下相似的資訊。
An error occurred (TargetInUseException) when calling the DeregisterTargetFromMaintenanceWindow operation: This Target cannot be deregistered because it is still referenced in Task: 4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE
-
執行以下命令替維護時段取消註冊目標,即使任務參考此目標。您可以使用
no-safe
強制取消註冊操作。aws ssm deregister-target-from-maintenance-window \ --window-id "
mw-0c50858d01EXAMPLE
" \ --window-target-id "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE
" \ --no-safe系統會傳回與以下相似的資訊。
{ "WindowId": "mw-0c50858d01EXAMPLE", "WindowTargetId": "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" }
-
執行下列命令以更新 Run Command 任務 這個範例使用名為
UpdateLevel
的 Systems Manager Parameter Store 參數,格式如下:'{{ssm:UpdateLevel}}
'aws ssm update-maintenance-window-task \ --window-id "
mw-0c50858d01EXAMPLE
" \ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE
" \ --targets "Key=InstanceIds,Values=i-02573cafcfEXAMPLE
" \ --task-invocation-parameters "RunCommand={Comment=A comment for my task update
,Parameters={UpdateLevel='{{ssm:UpdateLevel}}'}}"系統會傳回與以下相似的資訊。
{ "WindowId": "mw-0c50858d01EXAMPLE", "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE", "Targets": [ { "Key": "InstanceIds", "Values": [ "i-02573cafcfEXAMPLE" ] } ], "TaskArn": "AWS-RunShellScript", "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole", "TaskParameters": {}, "TaskInvocationParameters": { "RunCommand": { "Comment": "A comment for my task update", "Parameters": { "UpdateLevel": [ "{{ssm:UpdateLevel}}" ] } } }, "Priority": 10, "MaxConcurrency": "1", "MaxErrors": "1" }
-
執行以下命令更新 Automation 任務來替
task-invocation-parameters
參數指定WINDOW_ID
和WINDOW_TASK_ID
參數:aws ssm update-maintenance-window-task \ --window-id "
mw-0c50858d01EXAMPLE
" \ --window-task-id "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE
" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE
\ --task-arn "AutoTestDoc
" \ --service-role-arn "arn:aws:iam:account-id
:role/MyMaintenanceWindowServiceRole \ --task-invocation-parameters "Automation={Parameters={InstanceId='{{RESOURCE_ID}}',initiator='{{WINDOW_ID}}.Task-{{WINDOW_TASK_ID}}'}}" \ --priority3
--max-concurrency10
--max-errors5
系統會傳回與以下相似的資訊。
{ "WindowId": "mw-0c50858d01EXAMPLE", "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE", "Targets": [ { "Key": "WindowTargetIds", "Values": [ "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ] } ], "TaskArn": "AutoTestDoc", "ServiceRoleArn": "arn:aws:iam::111122223333:role/MyMaintenanceWindowServiceRole", "TaskParameters": {}, "TaskInvocationParameters": { "Automation": { "Parameters": { "multi": [ "{{WINDOW_TASK_ID}}" ], "single": [ "{{WINDOW_ID}}" ] } } }, "Priority": 0, "MaxConcurrency": "10", "MaxErrors": "5", "Name": "My-Automation-Task", "Description": "A description for my Automation task" }