

• 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)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立 Session Manager 偏好設定文件 (命令列)
<a name="getting-started-create-preferences-cli"></a>

使用下列程序來建立 SSM 文件，以定義工作階段的 AWS Systems Manager Session Manager偏好設定。您可以使用該文件來設定工作階段選項，包括資料加密、工作階段持續時間和日誌記錄。例如，您可以指定在 Amazon Simple Storage Service (Amazon S3) 儲存貯體或 Amazon CloudWatch Logs 日誌群組中存放工作階段日誌資料的選項。您可以為 AWS 帳戶 和 的所有工作階段建立定義一般偏好設定的文件 AWS 區域，或為個別工作階段定義偏好設定的文件。

**注意**  
您也可以使用 Session Manager 主控台設定一般工作階段偏好設定。

用來設定 Session Manager 偏好設定的文件的 `sessionType` 必須為 `Standard_Stream`。如需有關工作階段文件的詳細資訊，請參閱 [工作階段文件結構描述](session-manager-schema.md)。

如需使用命令列來更新現有 Session Manager 偏好設定的資訊，請參閱 [更新 Session Manager 偏好設定 (命令列)](getting-started-configure-preferences-cli.md)。

如需如何使用 建立工作階段偏好設定的範例 CloudFormation，請參閱*AWS CloudFormation 《 使用者指南*》中的[為Session Manager偏好設定建立 Systems Manager 文件](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ssm-document.html#aws-resource-ssm-document--examples)。

**注意**  
此程序說明如何建立文件，以在 AWS 帳戶 層級設定Session Manager偏好設定。若要建立將用於設定工作階段層級偏好設定的文件，請為檔案名稱相關指令輸入指定初 `SSM-SessionManagerRunShell` 之外的值。  
若要使用文件來設定從 AWS Command Line Interface (AWS CLI) 啟動之工作階段的偏好設定，請提供文件名稱作為 `--document-name` 參數值。若要為從 Session Manager 主控台啟動的工作階段設定偏好設定，您可以輸入文件名稱或從清單中選取文件的名稱。

**建立 Session Manager 偏好設定 (命令列)**

1. 在您的本機電腦建立 JSON 檔案的名稱，例如 `SessionManagerRunShell.json`，然後貼上以下內容。

   ```
   {
       "schemaVersion": "1.0",
       "description": "Document to hold regional settings for Session Manager",
       "sessionType": "Standard_Stream",
       "inputs": {
           "s3BucketName": "",
           "s3KeyPrefix": "",
           "s3EncryptionEnabled": true,
           "cloudWatchLogGroupName": "",
           "cloudWatchEncryptionEnabled": true,
           "cloudWatchStreamingEnabled": false,
           "kmsKeyId": "",
           "runAsEnabled": false,
           "runAsDefaultUser": "",
           "idleSessionTimeout": "",
           "maxSessionDuration": "",
           "shellProfile": {
               "windows": "date",
               "linux": "pwd;ls"
           }
       }
   }
   ```

   您也可以使用參數將值傳遞到工作階段偏好設定，而不是對這些值進行硬編碼，如以下範例所示。

   ```
   {
      "schemaVersion":"1.0",
      "description":"Session Document Parameter Example JSON Template",
      "sessionType":"Standard_Stream",
      "parameters":{
         "s3BucketName":{
            "type":"String",
            "default":""
         },
         "s3KeyPrefix":{
            "type":"String",
            "default":""
         },
         "s3EncryptionEnabled":{
            "type":"Boolean",
            "default":"false"
         },
         "cloudWatchLogGroupName":{
            "type":"String",
            "default":""
         },
         "cloudWatchEncryptionEnabled":{
            "type":"Boolean",
            "default":"false"
         }
      },
      "inputs":{
         "s3BucketName":"{{s3BucketName}}",
         "s3KeyPrefix":"{{s3KeyPrefix}}",
         "s3EncryptionEnabled":"{{s3EncryptionEnabled}}",
         "cloudWatchLogGroupName":"{{cloudWatchLogGroupName}}",
         "cloudWatchEncryptionEnabled":"{{cloudWatchEncryptionEnabled}}",
         "kmsKeyId":""
      }
   }
   ```

1. 指定您要傳送工作階段資料的位置。您可以指定 S3 儲存貯體名稱 (包含選用字首) 或 CloudWatch Logs 日誌群組名稱。如果您想要進一步加密本機用戶端和受管節點的資料，請提供用於加密的 KMS 金鑰。下列是 範例。

   ```
   {
     "schemaVersion": "1.0",
     "description": "Document to hold regional settings for Session Manager",
     "sessionType": "Standard_Stream",
     "inputs": {
       "s3BucketName": "amzn-s3-demo-bucket",
       "s3KeyPrefix": "MyS3Prefix",
       "s3EncryptionEnabled": true,
       "cloudWatchLogGroupName": "MyLogGroupName",
       "cloudWatchEncryptionEnabled": true,
       "cloudWatchStreamingEnabled": false,
       "kmsKeyId": "MyKMSKeyID",
       "runAsEnabled": true,
       "runAsDefaultUser": "MyDefaultRunAsUser",
       "idleSessionTimeout": "20",
       "maxSessionDuration": "60",
       "shellProfile": {
           "windows": "MyCommands",
           "linux": "MyCommands"
       }
     }
   }
   ```
**注意**  
如果您不想加密工作階段日誌資料，請將 `s3EncryptionEnabled` 的 `true` 設為 `false`。  
如果您不是將日誌傳送到 Amazon Simple Storage Service (Amazon S3) 儲存貯體或 CloudWatch Logs 日誌群組、不想加密作用中的工作階段資料，或不想為帳戶中的工作階段啟用執行身分支援，您可以刪除這幾行選項。請確定在 `inputs` 部分最後一行不是用逗號結尾。  
如果您新增可加密工作階段資料的 KMS 金鑰 ID，啟動工作階段的使用者和這些工作階段連接的受管節點必須具有該金鑰的使用許可。您可透過 IAM 政策提供將 KMS 金輪 與 Session Manager 搭配使用的許可。如需詳細資訊，請參閱以下主題：  
新增您帳戶中使用者的 AWS KMS 許可： [適用於 Session Manager 的範例 IAM 政策](getting-started-restrict-access-quickstart.md)
新增您帳戶中受管節點的 AWS KMS 許可： [步驟 2：為 Session Manager 確認或新增執行個體許可](session-manager-getting-started-instance-profile.md)

1. 儲存檔案。

1. 在您建立 JSON 檔案的目錄裡執行下列命令。

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

   ```
   aws ssm create-document \
       --name SSM-SessionManagerRunShell \
       --content "file://SessionManagerRunShell.json" \
       --document-type "Session" \
       --document-format JSON
   ```

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

   ```
   aws ssm create-document ^
       --name SSM-SessionManagerRunShell ^
       --content "file://SessionManagerRunShell.json" ^
       --document-type "Session" ^
       --document-format JSON
   ```

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

   ```
   New-SSMDocument `
       -Name "SSM-SessionManagerRunShell" `
       -Content (Get-Content -Raw SessionManagerRunShell.json) `
       -DocumentType "Session" `
       -DocumentFormat JSON
   ```

------

   如果成功，此命令傳回的輸出會類似如下。

   ```
   {
       "DocumentDescription": {
           "Status": "Creating",
           "Hash": "ce4fd0a2ab9b0fae759004ba603174c3ec2231f21a81db8690a33eb66EXAMPLE",
           "Name": "SSM-SessionManagerRunShell",
           "Tags": [],
           "DocumentType": "Session",
           "PlatformTypes": [
               "Windows",
               "Linux"
           ],
           "DocumentVersion": "1",
           "HashType": "Sha256",
           "CreatedDate": 1547750660.918,
           "Owner": "111122223333",
           "SchemaVersion": "1.0",
           "DefaultVersion": "1",
           "DocumentFormat": "JSON",
           "LatestVersion": "1"
       }
   }
   ```