

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

# ステップ 5: (オプション) セッションでのコマンドへのアクセスを制限する
<a name="session-manager-restrict-command-access"></a>

カスタム `Session` タイプの AWS Systems Manager (SSM) ドキュメントを使用すると、AWS Systems Manager Session Manager セッションでユーザーが実行できるコマンドを制限できます。ドキュメントでは、ユーザーがセッションを開始したときに実行されるコマンド、およびユーザーがコマンドに指定できるパラメータを定義します。`Session` ドキュメントの `schemaVersion` は 1.0 であり、`sessionType` は `InteractiveCommands` であることが必要です。その後、お客様が定義した `Session` ドキュメントのみへのアクセスをユーザーに許可する AWS Identity and Access Management (IAM) ポリシーを作成できます。IAM ポリシーを使用してセッションでのコマンドへのアクセスを制限する方法の詳細については、「[対話型コマンドの IAM ポリシーの例](#interactive-command-policy-examples)」を参照してください。

`InteractiveCommands` の `sessionType` を持つドキュメントは、AWS Command Line Interface (AWS CLI) から開始されたセッションでのみサポートされます。ユーザーはカスタムドキュメント名を `--document-name` パラメータ値として指定し、`--parameters` オプションを使用して任意のコマンドパラメータ値を指定します。対話型コマンドの実行の詳細については、「[セッションの開始 (対話形式と非対話形式のコマンド)](session-manager-working-with-sessions-start.md#sessions-start-interactive-commands)」を参照してください。

ユーザーに実行を許可するコマンドを定義するカスタムの `Session` タイプの SSM ドキュメントを作成するには以下の手順を使用します。

## セッションでコマンドへのアクセスを制限する (コンソール)
<a name="restrict-command-access-console"></a>

**Session Manager セッションでユーザーが実行できるコマンドを制限するには (コンソール)**

1. AWS Systems Manager コンソール ([https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/)) を開きます。

1. ナビゲーションペインで、[**ドキュメント**] を選択します。

1. [**Create command or session (コマンドまたはセッションの作成)**] を選択します。

1. [**名前**] に、ドキュメントのわかりやすい名前を入力します。

1. [**Document type (ドキュメントタイプ)**] で、[**Session document (セッションドキュメント)**] を選択します。

1. 以下の例に示すように、JSON または YAML を使用して、Session Manager セッションでユーザーが実行できるコマンドを定義するドキュメントコンテンツを入力します。

------
#### [ YAML ]

   ```
   ---
   schemaVersion: '1.0'
   description: Document to view a log file on a Linux instance
   sessionType: InteractiveCommands
   parameters:
     logpath:
       type: String
       description: The log file path to read.
       default: "/var/log/amazon/ssm/amazon-ssm-agent.log"
       allowedPattern: "^[a-zA-Z0-9-_/]+(.log)$"
   properties:
     linux:
       commands: "tail -f {{ logpath }}"
       runAsElevated: true
   ```

------
#### [ JSON ]

   ```
   {
       "schemaVersion": "1.0",
       "description": "Document to view a log file on a Linux instance",
       "sessionType": "InteractiveCommands",
       "parameters": {
           "logpath": {
               "type": "String",
               "description": "The log file path to read.",
               "default": "/var/log/amazon/ssm/amazon-ssm-agent.log",
               "allowedPattern": "^[a-zA-Z0-9-_/]+(.log)$"
           }
       },
       "properties": {
           "linux": {
               "commands": "tail -f {{ logpath }}",
               "runAsElevated": true
           }
       }
   }
   ```

------

1. [**Create document**] を選択します。

## セッションでコマンドへのアクセスを制限する (コマンドライン)
<a name="restrict-command-access-commandline"></a>

**開始する前に**  
まだ AWS Command Line Interface (AWS CLI) または AWS Tools for PowerShell をインストールして設定していない場合は、インストールして設定します。詳細については、「[AWS CLI の最新バージョンをインストールまたは更新します。](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)」および「[AWS Tools for PowerShell のインストール](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up.html)」を参照してください。

**Session Manager セッションでユーザーが実行できるコマンドを制限するには (コマンドライン)**

1. 以下の例に示すように、Session Manager セッションでユーザーが実行できるコマンドを定義するドキュメントコンテンツの JSON または YAML ファイルを作成します。

------
#### [ YAML ]

   ```
   ---
   schemaVersion: '1.0'
   description: Document to view a log file on a Linux instance
   sessionType: InteractiveCommands
   parameters:
     logpath:
       type: String
       description: The log file path to read.
       default: "/var/log/amazon/ssm/amazon-ssm-agent.log"
       allowedPattern: "^[a-zA-Z0-9-_/]+(.log)$"
   properties:
     linux:
       commands: "tail -f {{ logpath }}"
       runAsElevated: true
   ```

------
#### [ JSON ]

   ```
   {
       "schemaVersion": "1.0",
       "description": "Document to view a log file on a Linux instance",
       "sessionType": "InteractiveCommands",
       "parameters": {
           "logpath": {
               "type": "String",
               "description": "The log file path to read.",
               "default": "/var/log/amazon/ssm/amazon-ssm-agent.log",
               "allowedPattern": "^[a-zA-Z0-9-_/]+(.log)$"
           }
       },
       "properties": {
           "linux": {
               "commands": "tail -f {{ logpath }}",
               "runAsElevated": true
           }
       }
   }
   ```

------

1. 以下のコマンドを実行して、Session Manager セッションでユーザーが実行できるコマンドを定義するコンテンツから、SSM ドキュメントを作成します。

------
#### [ Linux & macOS ]

   ```
   aws ssm create-document \
       --content file://path/to/file/documentContent.json \
       --name "exampleAllowedSessionDocument" \
       --document-type "Session"
   ```

------
#### [  Server   ]

   ```
   aws ssm create-document ^
       --content file://C:\path\to\file\documentContent.json ^
       --name "exampleAllowedSessionDocument" ^
       --document-type "Session"
   ```

------
#### [   PowerShell   ]

   ```
   $json = Get-Content -Path "C:\path\to\file\documentContent.json" | Out-String
   New-SSMDocument `
       -Content $json `
       -Name "exampleAllowedSessionDocument" `
       -DocumentType "Session"
   ```

------

## 対話型コマンドパラメータと AWS CLI
<a name="restrict-command-access-parameters-cli"></a>

AWS CLI を使用する際、対話型のコマンドパラメータを指定する方法はさまざまあります。AWS CLI でマネージドノードに接続する際に使用するクライアントマシンのオペレーティングシステム (OS) によっては、特殊またはエスケープ記号を含むコマンドの構文が異なる場合があります。次の例は、AWS CLI の使用時にコマンドパラメータを指定する方法と、特殊文字またはエスケープ文字の処理方法を示しています。

Parameter Store に保存されたパラメータは、次の例に示すように、 コマンドパラメータの AWS CLI で参照できます。

------
#### [ Linux & macOS ]

```
aws ssm start-session \
    --target instance-id \
    --document-name MyInteractiveCommandDocument \ 
    --parameters '{"command":["{{ssm:mycommand}}"]}'
```

------
#### [  Server   ]

```
aws ssm start-session ^
    --target instance-id ^
    --document-name MyInteractiveCommandDocument ^
    --parameters '{"command":["{{ssm:mycommand}}"]}'
```

------

次の例は、AWS CLI で省略形構文を使用してパラメータを渡す方法を示しています。

------
#### [ Linux & macOS ]

```
aws ssm start-session \
    --target instance-id \
    --document-name MyInteractiveCommandDocument \ 
    --parameters command="ifconfig"
```

------
#### [  Server   ]

```
aws ssm start-session ^
    --target instance-id ^
    --document-name MyInteractiveCommandDocument ^
    --parameters command="ipconfig"
```

------

次の例に示すように、JSONでパラメータを指定することもできます。

------
#### [ Linux & macOS ]

```
aws ssm start-session \
    --target instance-id \
    --document-name MyInteractiveCommandDocument \ 
    --parameters '{"command":["ifconfig"]}'
```

------
#### [  Server   ]

```
aws ssm start-session ^
    --target instance-id ^
    --document-name MyInteractiveCommandDocument ^
    --parameters '{"command":["ipconfig"]}'
```

------

次の例に示すように、パラメータを JSON ファイルに格納し、AWS CLI に提供することもできます。ファイルから AWS CLI パラメータを使用する方法の詳細については、*AWS Command Line Interface ユーザーガイド*の「[ファイルから AWS CLI パラメータをロードする](https://docs.aws.amazon.com/cli/latest/userguide/;cli-usage-parameters-file.html)」を参照してください。

```
{
    "command": [
        "my command"
    ]
}
```

------
#### [ Linux & macOS ]

```
aws ssm start-session \
    --target instance-id \
    --document-name MyInteractiveCommandDocument \ 
    --parameters file://complete/path/to/file/parameters.json
```

------
#### [  Server   ]

```
aws ssm start-session ^
    --target instance-id ^
    --document-name MyInteractiveCommandDocument ^
    --parameters file://complete/path/to/file/parameters.json
```

------

次の例に示すように、JSON 入力ファイルから AWS CLI スケルトンを生成することもできます。JSON 入力ファイルから AWS CLI スケルトンを生成する方法の詳細については、*AWS Command Line Interface ユーザーガイド*の「[JSON または YAML 入力ファイルから AWS CLI スケルトンと入力パラメータを生成する](https://docs.aws.amazon.com/cli/latest/userguide/;cli-usage-skeleton.html)」を参照してください。

```
{
    "Target": "instance-id",
    "DocumentName": "MyInteractiveCommandDocument",
    "Parameters": {
        "command": [
            "my command"
        ]
    }
}
```

------
#### [ Linux & macOS ]

```
aws ssm start-session \
    --cli-input-json file://complete/path/to/file/parameters.json
```

------
#### [  Server   ]

```
aws ssm start-session ^
    --cli-input-json file://complete/path/to/file/parameters.json
```

------

引用符で囲まれた文字をエスケープするには、次の例に示すように、エスケープ文字にバックスラッシュを追加する必要があります。

------
#### [ Linux & macOS ]

```
aws ssm start-session \
    --target instance-id \
    --document-name MyInteractiveCommandDocument \ 
    --parameters '{"command":["printf \"abc\\\\tdef\""]}'
```

------
#### [  Server   ]

```
aws ssm start-session ^
    --target instance-id ^
    --document-name MyInteractiveCommandDocument ^
    --parameters '{"command":["printf \"abc\\\\tdef\""]}'
```

------

AWS CLI のコマンドパラメータで引用符を使用する方法については、*AWS Command Line Interface ユーザーガイド*の「[AWS CLI の文字列で引用符を使用する](https://docs.aws.amazon.com/cli/latest/userguide/;cli-usage-parameters-quoting-strings.html)」を参照してください。

## 対話型コマンドの IAM ポリシーの例
<a name="interactive-command-policy-examples"></a>

お客様が定義した `Session` ドキュメントのみへのアクセスをユーザーに許可する IAM ポリシーを作成できます。これにより、Session Manager セッションでユーザーが実行できるコマンドは、カスタムの `Session` タイプの SSM ドキュメントで定義されたコマンドのみに制限されます。

 **ユーザーに対して 1 つのマネージドノードに 1 つの対話コマンドの実行を許可**     
****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Effect":"Allow",
         "Action":"ssm:StartSession",
         "Resource":[
            "arn:aws:ec2:us-east-1:444455556666:instance/i-02573cafcfEXAMPLE",
            "arn:aws:ssm:us-east-1:444455556666:document/allowed-session-document"
         ]
      },
      {
         "Effect": "Allow",
         "Action": ["ssmmessages:OpenDataChannel"],
         "Resource": ["arn:aws:ssm:*:*:session/${aws:userid}-*"]
      }
   ]
}
```

 **ユーザーに対してすべてのマネージドノードに 1 つの対話コマンドの実行を許可**     
****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Effect":"Allow",
         "Action":"ssm:StartSession",
         "Resource":[
            "arn:aws:ec2:us-east-1:444455556666:instance/*",
            "arn:aws:ssm:us-east-1:444455556666:document/allowed-session-document"
         ]
      },
      {
         "Effect": "Allow",
         "Action": ["ssmmessages:OpenDataChannel"],
         "Resource": ["arn:aws:ssm:*:*:session/${aws:userid}-*"]
      }
   ]
}
```

 **ユーザーに対してすべてのマネージドノードに複数の対話コマンドの実行を許可**     
****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Effect":"Allow",
         "Action":"ssm:StartSession",
         "Resource":[
            "arn:aws:ec2:us-east-1:444455556666:instance/*",
            "arn:aws:ssm:us-east-1:444455556666:document/allowed-session-document",
            "arn:aws:ssm:us-east-1:444455556666:document/allowed-session-document-2"
         ]
      },
      {
         "Effect": "Allow",
         "Action": ["ssmmessages:OpenDataChannel"],
         "Resource": ["arn:aws:ssm:*:*:session/${aws:userid}-*"]
      }
   ]
}
```