

• 2026 年 4 月 30 日之後將不再提供 AWS Systems Manager CloudWatch Dashboard。客戶可以繼續使用 Amazon CloudWatch 主控台來檢視、建立和管理其 Amazon CloudWatch 儀表板，就像現在一樣。如需詳細資訊，請參閱 [Amazon CloudWatch Dashboard 文件](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) 政策，只允許使用者存取您定義的 `Session` 文件。如需使用 IAM 政策來限制對工作階段中命令之存取權的詳細資訊，請參閱 [互動式命令的 IAM 政策範例](#interactive-command-policy-examples)。

具有 `sessionType`的 文件`InteractiveCommands`僅支援從 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. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Documents (文件)**。

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. 選擇 **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"
   ```

------
#### [  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時，您可以透過多種方式提供互動式命令參數。根據您用來使用 連線至受管節點的用戶端機器作業系統 (OS) AWS CLI，您為包含特殊或逸出字元的命令提供的語法可能會有所不同。下列範例顯示使用 時，您可以提供命令參數的一些不同方式 AWS CLI，以及如何處理特殊或逸出字元。

Parameter Store 您可以在 中參考存放在 中的參數 AWS CLI ，以取得命令參數，如下列範例所示。

------
#### [ 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 政策範例
<a name="interactive-command-policy-examples"></a>

您可以建立 IAM 政策，只允許使用者存取您定義的 `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}-*"]
      }
   ]
}
```