

• AWS Systems Manager CloudWatch ダッシュボードは、2026 年 4 月 30 日以降は利用できなくなります。お客様は、これまでと同様に Amazon CloudWatch コンソールを使用して、Amazon CloudWatch ダッシュボードの表示、作成、管理を継続できます。詳細については、「[Amazon CloudWatch ダッシュボードのドキュメント](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)」を参照してください。

# 例: タスクをメンテナンスウィンドウに登録する
<a name="mw-cli-register-tasks-examples"></a>

「[メンテナンスウィンドウを使用してタスクを登録する](mw-cli-tutorial-tasks.md)」に示されているように、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) に対するターゲットはオプションです。ターゲットを指定しないタスクの実行の詳細については、「[ターゲットのないメンテナンスウィンドウタスクを登録](maintenance-windows-targetless-tasks.md)」を参照してください。

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

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

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

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

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

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

1. ファイルを保存したのと同じディレクトリで、以下のコマンドを実行します。ファイル名を *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` コンテンツ内の疑似パラメータの詳細については、「[メンテナンスウィンドウのタスクを登録する際の疑似パラメータの使用](maintenance-window-tasks-pseudo-parameters.md)」を参照してください。

**詳細情報**  
+ ["register-task-with-maintenance-window" コマンドのパラメーターオプション](mw-cli-task-options.md).
+ [register-task-with-maintenance-window「 コマンドリファレンス」](https://docs.aws.amazon.com/cli/latest/reference/ssm/register-task-with-maintenance-window.html)の*「AWS CLI」*を参照してください。
+ 「*[https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_RegisterTaskWithMaintenanceWindow.html](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_RegisterTaskWithMaintenanceWindow.html) API リファレンス*」の「AWS Systems Manager」

## タスクの登録例
<a name="task-examples"></a>

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

### Systems Manager Run Command タスクを登録する
<a name="register-tasks-tutorial-run-command"></a>

以下の例は、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": "amzn-s3-demo-bucket",
            "OutputS3KeyPrefix": "S3-PREFIX",
            "TimeoutSeconds": 3600
        }
    }
}
```

### Systems Manager Automation タスクを登録する
<a name="register-tasks-tutorial-automation"></a>

以下の例では、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 Lambda タスクを登録する
<a name="register-tasks-tutorial-lambda"></a>

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

これらの例では、Lambda 関数を作成したユーザーがその関数に `SSMrestart-my-instances` という名前を付け、`instanceId` と `targetType` という名前の 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` ファイル設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-settings)」を参照してください。

```
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` ファイル設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-settings)」を参照してください。

```
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
}
```

### Step Functions タスクを登録する
<a name="register-tasks-tutorial-step-functions"></a>

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

**注記**  
メンテナンスウィンドウタスクは、Step Functions 標準ステートマシンワークフローのみをサポートします。Express ステートマシンワークフローはサポートされていません。ステートマシンワークフロータイプの詳細については、「AWS Step Functions デベロッパーガイド」の「[標準ワークフローと Express ワークフロー](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html)」を参照してください。

これらの例では、ステップ関数ステートマシンを作成したユーザーが、`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
}
```