例: タスクをメンテナンスウィンドウに登録する - AWS Systems Manager

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

例: タスクをメンテナンスウィンドウに登録する

[メンテナンスウィンドウを使用してタスクを登録する] に示されているように、AWS Command Line Interface (AWS CLI) を使用してメンテナンスウィンドウに Run Command (AWS Systems Manager の一機能) でタスクを登録できます。このトピックで後ほど示すように、Systems Manager Automation ワークフローのタスク、AWS Lambda 関数、AWS Step Functions タスクを登録することもできます。

注記

メンテナンスウィンドウの Run Command タイプのタスクには、1つ以上のターゲットを指定します。タスクに応じて、他のメンテナンスウィンドウタスクタイプ (Automation、AWS Lambda、AWS Step Functions) に対するターゲットはオプションです。ターゲットを指定しないタスクの実行の詳細については、「ターゲットのないメンテナンスウィンドウタスクを登録」を参照してください。

このトピックでは、AWS Command Line Interface (AWS CLI) コマンド register-task-with-maintenance-window を使用して、サポートされている 4 つのタスクタイプをそれぞれメンテナンスウィンドウに登録する例を示しています。それらの例はデモ専用ですが、作業タスクの登録コマンドを作成するように変更できます。

--cli-input-json option の使用

タスクオプションをより適切に管理するために、--cli-input-json コマンドオプションに、JSON ファイルで参照されるオプション値を指定できます。

以下の例で提供しているサンプル JSON ファイルコンテンツを使用するには、ローカルマシンで以下の手順を実行します。

  1. MyRunCommandTask.jsonMyAutomationTask.json などの名前、または他の任意の名前でファイルを作成します。

  2. そのファイルに JSON サンプルの内容をコピーします。

  3. タスク登録用にその内容を変更し、そのファイルを保存します。

  4. ファイルを保存したのと同じディレクトリで、以下のコマンドを実行します。ファイル名を MyFile.json に置き換えます。

    Linux & macOS
    aws ssm register-task-with-maintenance-window \ --cli-input-json file://MyFile.json
    Windows
    aws ssm register-task-with-maintenance-window ^ --cli-input-json file://MyFile.json
疑似パラメータについて

いくつかの例では、ID 情報をタスクに渡すための方法として疑似パラメータを使用します。例えば、{{TARGET_ID}}{{RESOURCE_ID}} は、AWS リソースの ID を Automation 、Lambda、Step Functions のタスクに渡すために使用されます。--task-invocation-parameters コンテンツ内の疑似パラメータの詳細については、「メンテナンスウィンドウタスクの登録時の擬似パラメータの使用」を参照してください。

詳細情報

タスクの登録例

以下のセクションでは、サポートされているタスクタイプを登録するサンプルの AWS CLI コマンドと、--cli-input-json オプションで用できる JSON サンプルを示しています。

以下の例は、AWS CLI を使用して、メンテナンスウィンドウで Systems Manager Run Command タスクを登録する方法を示しています。

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-02573cafcfEXAMPLE" \ --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\"]}}}"

--cli-input-json ファイルオプションで使用する JSON コンテンツ:

{ "TaskType": "RUN_COMMAND", "WindowId": "mw-0c50858d01EXAMPLE", "Description": "My Run Command task to update SSM Agent on an instance", "MaxConcurrency": "1", "MaxErrors": "1", "Name": "My-Run-Command-Task", "Priority": 10, "Targets": [ { "Key": "WindowTargetIds", "Values": [ "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ] } ], "TaskArn": "AWS-UpdateSSMAgent", "TaskInvocationParameters": { "RunCommand": { "Comment": "A TaskInvocationParameters test comment", "NotificationConfig": { "NotificationArn": "arn:aws:sns:region:123456789012:my-sns-topic-name", "NotificationEvents": [ "All" ], "NotificationType": "Invocation" }, "OutputS3BucketName": "DOC-EXAMPLE-BUCKET", "OutputS3KeyPrefix": "DOC-EXAMPLE-FOLDER", "TimeoutSeconds": 3600 } } }

以下の例では、AWS CLI を使用して Systems Manager Automation タスクをメンテナンスウィンドウに登録する方法を示しています。

AWS CLI コマンド:

Linux & macOS
aws ssm register-task-with-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --task-arn "AWS-RestartEC2Instance" \ --service-role-arn arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole \ --task-type AUTOMATION \ --task-invocation-parameters "Automation={DocumentVersion=5,Parameters={InstanceId='{{RESOURCE_ID}}'}}" \ --priority 0 --name "My-Restart-EC2-Instances-Automation-Task" \ --description "Automation task to restart EC2 instances"
Windows
aws ssm register-task-with-maintenance-window ^ --window-id "mw-0c50858d01EXAMPLE" ^ --task-arn "AWS-RestartEC2Instance" ^ --service-role-arn arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole ^ --task-type AUTOMATION ^ --task-invocation-parameters "Automation={DocumentVersion=5,Parameters={InstanceId='{{TARGET_ID}}'}}" ^ --priority 0 --name "My-Restart-EC2-Instances-Automation-Task" ^ --description "Automation task to restart EC2 instances"

--cli-input-json ファイルオプションで使用する JSON コンテンツ:

{ "WindowId": "mw-0c50858d01EXAMPLE", "TaskArn": "AWS-PatchInstanceWithRollback", "TaskType": "AUTOMATION","TaskInvocationParameters": { "Automation": { "DocumentVersion": "1", "Parameters": { "instanceId": [ "{{RESOURCE_ID}}" ] } } } }

以下の例では、AWS CLI を使用して Lambda 関数タスクをメンテナンスウィンドウに登録する方法を示しています。

これらの例では、Lambda 関数を作成したユーザーがその関数に SSMrestart-my-instances という名前を付け、instanceIdtargetType という名前の 2 つのパラメータを作成したとします。

重要

Maintenance Windows の IAM ポリシーでは、Lambda 関数名 (またはエイリアス) の前にプレフィックス SSM を付ける必要があります。このタイプのタスクを登録する前に、AWS Lambda で SSM を含めるようにその名前を更新します。例えば、Lambda 関数名が MyLambdaFunction の場合は、SSMMyLambdaFunction に変更します。

AWS CLI コマンド:

Linux & macOS
重要

AWS CLI のバージョン 2 を使用している場合、Lambda ペイロードが base64 エンコードでなければ、以下のコマンドにオプション --cli-binary-format raw-in-base64-out を含める必要があります。cli_binary_format オプションは、バージョン 2 でしか使用できません。これと他の AWS CLI configファイル設定については、AWS Command Line Interface ユーザーガイドで「サポート対象 config ファイル設定」を参照してください。

aws ssm register-task-with-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --priority 2 --max-concurrency 10 --max-errors 5 --name "My-Lambda-Example" \ --description "A description for my LAMBDA example task" --task-type "LAMBDA" \ --task-arn "arn:aws:lambda:region:123456789012:function:serverlessrepo-SSMrestart-my-instances-C4JF9EXAMPLE" \ --task-invocation-parameters '{"Lambda":{"Payload":"{\"InstanceId\":\"{{RESOURCE_ID}}\",\"targetType\":\"{{TARGET_TYPE}}\"}","Qualifier": "$LATEST"}}'
PowerShell
重要

AWS CLI のバージョン 2 を使用している場合、Lambda ペイロードが base64 エンコードでなければ、以下のコマンドにオプション --cli-binary-format raw-in-base64-out を含める必要があります。cli_binary_format オプションは、バージョン 2 でしか使用できません。これと他の AWS CLI configファイル設定については、AWS Command Line Interface ユーザーガイドで「サポート対象 config ファイル設定」を参照してください。

aws ssm register-task-with-maintenance-window ` --window-id "mw-0c50858d01EXAMPLE" ` --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ` --priority 2 --max-concurrency 10 --max-errors 5 --name "My-Lambda-Example" ` --description "A description for my LAMBDA example task" --task-type "LAMBDA" ` --task-arn "arn:aws:lambda:region:123456789012:function:serverlessrepo-SSMrestart-my-instances-C4JF9EXAMPLE" ` --task-invocation-parameters '{\"Lambda\":{\"Payload\":\"{\\\"InstanceId\\\":\\\"{{RESOURCE_ID}}\\\",\\\"targetType\\\":\\\"{{TARGET_TYPE}}\\\"}\",\"Qualifier\": \"$LATEST\"}}'

--cli-input-json ファイルオプションで使用する JSON コンテンツ:

{ "WindowId": "mw-0c50858d01EXAMPLE", "Targets": [ { "Key": "WindowTargetIds", "Values": [ "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ] } ], "TaskArn": "SSM_RestartMyInstances", "TaskType": "LAMBDA", "MaxConcurrency": "10", "MaxErrors": "10", "TaskInvocationParameters": { "Lambda": { "ClientContext": "ew0KICAi--truncated--0KIEXAMPLE", "Payload": "{ \"instanceId\": \"{{RESOURCE_ID}}\", \"targetType\": \"{{TARGET_TYPE}}\" }", "Qualifier": "$LATEST" } }, "Name": "My-Lambda-Task", "Description": "A description for my LAMBDA task", "Priority": 5 }

以下の例では、AWS CLI を使用して Step Functions ステートマシンタスクをメンテナンスウィンドウに登録する方法を示しています。

これらの例では、ステップ関数ステートマシンを作成したユーザーが、SSMMyStateMachine というパラメータを指定して、instanceId という名前のステートマシンを作成しました。

重要

Maintenance Windows の AWS Identity and Access Management (IAM) ポリシーでは、Step Functions ステートマシン名の前に SSM でプレフィックスを付ける必要があります。このタイプのタスクを登録する前に、AWS Step Functions で SSM を含めるようにその名前を更新する必要があります。たとえば、ステートマシン名が MyStateMachine の場合は、SSMMyStateMachine に変更します。

AWS CLI コマンド:

Linux & macOS
aws ssm register-task-with-maintenance-window \ --window-id "mw-0c50858d01EXAMPLE" \ --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" \ --task-arn arn:aws:states:region:123456789012:stateMachine:SSMMyStateMachine-MggiqEXAMPLE \ --task-type STEP_FUNCTIONS \ --task-invocation-parameters '{"StepFunctions":{"Input":"{\"InstanceId\":\"{{RESOURCE_ID}}\"}", "Name":"{{INVOCATION_ID}}"}}' \ --priority 0 --max-concurrency 10 --max-errors 5 \ --name "My-Step-Functions-Task" --description "A description for my Step Functions task"
PowerShell
aws ssm register-task-with-maintenance-window ` --window-id "mw-0c50858d01EXAMPLE" ` --targets "Key=WindowTargetIds,Values=e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ` --task-arn arn:aws:states:region:123456789012:stateMachine:SSMMyStateMachine-MggiqEXAMPLE ` --task-type STEP_FUNCTIONS ` --task-invocation-parameters '{\"StepFunctions\":{\"Input\":\"{\\\"InstanceId\\\":\\\"{{RESOURCE_ID}}\\\"}\", \"Name\":\"{{INVOCATION_ID}}\"}}' ` --priority 0 --max-concurrency 10 --max-errors 5 ` --name "My-Step-Functions-Task" --description "A description for my Step Functions task"

--cli-input-json ファイルオプションで使用する JSON コンテンツ:

{ "WindowId": "mw-0c50858d01EXAMPLE", "Targets": [ { "Key": "WindowTargetIds", "Values": [ "e32eecb2-646c-4f4b-8ed1-205fbEXAMPLE" ] } ], "TaskArn": "SSM_MyStateMachine", "TaskType": "STEP_FUNCTIONS", "MaxConcurrency": "10", "MaxErrors": "10", "TaskInvocationParameters": { "StepFunctions": { "Input": "{ \"instanceId\": \"{{TARGET_ID}}\" }", "Name": "{{INVOCATION_ID}}" } }, "Name": "My-Step-Functions-Task", "Description": "A description for my Step Functions task", "Priority": 5 }