

• 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`。然后，您可以创建 AWS Identity and Access Management（IAM）policy 以仅允许用户访问您定义的 `Session` 文档。有关使用 IAM policy 限制对会话中命令的访问的更多信息，请参阅 [交互式命令的 IAM policy 示例](#interactive-command-policy-examples)。

仅从 AWS Command Line Interface（AWS CLI）启动的会话支持 `sessionType` 为 `InteractiveCommands` 的文档。用户提供自定义文档名称作为 `--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. 访问 [https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/)，打开 AWS Systems Manager 控制台。

1. 在导航窗格中，选择**文档**。

1. 选择 **Create command or session (创建命令或会话)**。

1. 对于 **Name (名称)**，为文档输入一个描述性名称。

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. 选择**创建文档**。

## 限制对会话中命令的访问（命令行）
<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. 为文档内容创建 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. 运行以下命令，使用您的内容创建 SSM 文档，该内容定义了用户可以在 Session Manager 会话中运行的命令。

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

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

------
#### [  Windows  ]

   ```
   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 时提供命令参数的一些不同方法，以及如何处理特殊字符或转义字符。

您的命令参数可在 AWS CLI 中引用存储在 Parameter Store 中的参数，如以下示例所示。

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

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

------
#### [  Windows  ]

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

------
#### [  Windows  ]

```
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"]}'
```

------
#### [  Windows  ]

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

------
#### [  Windows  ]

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

------
#### [  Windows  ]

```
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\""]}'
```

------
#### [  Windows  ]

```
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 policy 示例
<a name="interactive-command-policy-examples"></a>

您可以创建 IAM policy 以仅允许用户访问您定义的 `Session` 文档。这将用户可以在 Session Manager 会话中运行的命令限制为仅在自定义 `Session` 类型 SSM 文档中定义的命令。

 **允许用户在单个托管式节点上运行交互式命令**     
****  

```
{
   "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}-*"]
      }
   ]
}
```

 **允许用户在所有托管式节点上运行交互式命令**     
****  

```
{
   "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}-*"]
      }
   ]
}
```