

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

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

# 設定 Run Command
<a name="run-command-setting-up"></a>

在您可以使用 中的工具來管理節點之前Run Command，請為將執行命令的任何使用者 AWS Systems Manager設定 AWS Identity and Access Management (IAM) 政策。如果針對 IAM 政策中的 `SendCommand` 動作使用任何全域條件索引鍵，則必須包含 `aws:ViaAWSService` 條件索引鍵並將布林值設定為 `true`。下列是 範例。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ssm:SendCommand"
            ],
            "Resource": [
                "arn:aws:ssm:us-east-1:111122223333:document/YourDocument"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:SourceVpce": [
                        "vpce-1234567890abcdef0"
                    ]
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:SendCommand"
            ],
            "Resource": [
                "arn:aws:ssm:us-east-1:111122223333:document/YourDocument"
            ],
            "Condition": {
                "Bool": {
                    "aws:ViaAWSService": "true"
                }
            }
        }
    ]
}
```

------

您也必須為 Systems Manager 設定節點。如需詳細資訊，請參閱[設定 的受管節點 AWS Systems Manager](systems-manager-setting-up-nodes.md)。

我們建議您完成下列選擇性設定任務，以將受管節點安全狀態和日常的管理降到最低。

使用 Amazon EventBridge 監控命令執行  
您可以使用 EventBridge 來記錄命令執行狀態變更。您可以建立規則，在有狀態轉換或有轉移到一或多個與您相關的狀態時執行。您也可以指定在 EventBridge 事件發生時，將 Run Command 做為目標動作。如需詳細資訊，請參閱[為 Systems Manager 事件設定 EventBridge](monitoring-systems-manager-events.md)。

使用 Amazon CloudWatch Logs 來監控命令執行  
您可以設定 Run Command 來定期將所有命令輸出和錯誤日誌傳送至 Amazon CloudWatch 日誌群組。您可以以幾乎即時的方式監控這些輸出日誌、搜尋特定字詞、數值或模式，並根據搜尋建立警示。如需詳細資訊，請參閱[設定 Run Command 的 Amazon CloudWatch Logs](sysman-rc-setting-up-cwlogs.md)。

限制 Run Command 對特定受管節點的存取  
您可以使用 AWS Identity and Access Management (IAM) 限制使用者在受管節點上執行命令的能力。具體來說，您可以建立 IAM 政策，其條件是使用者只能在標記了特定標籤的受管節點上執行命令。如需詳細資訊，請參閱[根據標籤限制 Run Command 存取](#tag-based-access)。

## 根據標籤限制 Run Command 存取
<a name="tag-based-access"></a>

本節描述了如何透過在 IAM 政策中指定標籤條件，限制使用者在受管節點上執行命令的能力。受管節點包含針對 Systems Manager 設定之[混合多雲端](operating-systems-and-machine-types.md#supported-machine-types)環境中的 Amazon EC2 執行個體和非 EC2 節點。雖然資訊未明確顯示，但您也可以限制對受管 AWS IoT Greengrass 核心裝置的存取。若要開始使用，您必須標記您的 AWS IoT Greengrass 裝置。如需詳細資訊，請參閱《*AWS IoT Greengrass Version 2 開發人員指南*》中的[標記您的 AWS IoT Greengrass Version 2 資源](https://docs.aws.amazon.com/greengrass/v2/developerguide/tag-resources.html)。

您可以透過建立 IAM 政策 (其中包含條件，限制使用者只能在具有特定標籤的節點上執行命令)，來限制只針對特定受管節點執行命令。在下列範例中，使用者可以在任何節點 (`Resource: arn:aws:ec2:*:*:instance/*`) 使用任何 SSM 文件 (`Resource: arn:aws:ssm:*:*:document/*`) 來使用 Run Command (`Effect: Allow, Action: ssm:SendCommand`)，條件為節點是 Finance WebServer (`ssm:resourceTag/Finance: WebServer`)。如果使用者將命令傳送至未加上標記或有 `Finance: WebServer` 以外任何標記的節點：執行結果會顯示 `AccessDenied`。

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

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "ssm:SendCommand"
         ],
         "Resource":[
            "arn:aws:ssm:*:*:document/*"
         ]
      },
      {
         "Effect":"Allow",
         "Action":[
            "ssm:SendCommand"
         ],
         "Resource":[
            "arn:aws:ec2:*:*:instance/*"
         ],
         "Condition":{
            "StringLike":{
               "ssm:resourceTag/Finance":[
                  "WebServers"
               ]
            }
         }
      }
   ]
}
```

------

您可以建立 IAM 政策，可讓使用者在以多個標籤所標記的受管節點上執行命令。以下政策可讓使用者在擁有兩個標籤的受管理節點上執行命令。如果使用者將命令傳送至未以那兩個標記所標記的節點，執行結果會顯示 `AccessDenied`。

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

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "ssm:SendCommand"
         ],
         "Resource":"*",
         "Condition":{
            "StringLike":{
               "ssm:resourceTag/tag_key1":[
                  "tag_value1"
               ],
               "ssm:resourceTag/tag_key2":[
                  "tag_value2"
               ]
            }
         }
      },
      {
         "Effect":"Allow",
         "Action":[
            "ssm:SendCommand"
         ],
         "Resource":[
            "arn:aws:ssm:us-west-1::document/AWS-*",
            "arn:aws:ssm:us-east-2::document/AWS-*"
         ]
      },
      {
         "Effect":"Allow",
         "Action":[
            "ssm:UpdateInstanceInformation",
            "ssm:ListCommands",
            "ssm:ListCommandInvocations",
            "ssm:GetDocument"
         ],
         "Resource":"*"
      }
   ]
}
```

------

您也可以建立 IAM 政策，其可讓使用者在多組加上標籤的受管節點上執行命令。以下範例政策可讓使用者在一組或兩組含標記的節點上執行命令。

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

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "ssm:SendCommand"
         ],
         "Resource":"*",
         "Condition":{
            "StringLike":{
               "ssm:resourceTag/tag_key1":[
                  "tag_value1"
               ]
            }
         }
      },
      {
         "Effect":"Allow",
         "Action":[
            "ssm:SendCommand"
         ],
         "Resource":"*",
         "Condition":{
            "StringLike":{
               "ssm:resourceTag/tag_key2":[
                  "tag_value2"
               ]
            }
         }
      },
      {
         "Effect":"Allow",
         "Action":[
            "ssm:SendCommand"
         ],
         "Resource":[
            "arn:aws:ssm:us-west-1::document/AWS-*",
            "arn:aws:ssm:us-east-2::document/AWS-*"
         ]
      },
      {
         "Effect":"Allow",
         "Action":[
            "ssm:UpdateInstanceInformation",
            "ssm:ListCommands",
            "ssm:ListCommandInvocations",
            "ssm:GetDocument"
         ],
         "Resource":"*"
      }
   ]
}
```

------

如需有關建立 IAM 政策的詳細資訊，請參閱《IAM 使用者指南》**中的[受管政策和內嵌政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html)。如需標記受管節點的詳細資訊，請參閱《*AWS Resource Groups 使用者指南*》中的[標籤編輯器](https://docs.aws.amazon.com/ARG/latest/userguide/tag-editor.html)。