

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

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

# 大規模執行命令
<a name="send-commands-multiple"></a>

您可以使用 中的Run Command工具 AWS Systems Manager，在受管節點的機群上執行命令`targets`。`targets` 參數接受以您為受管節點指定的標記為基礎的 `Key,Value` 組。當您執行命令時，系統會尋找並嘗試在符合指定標記的所有受管節點上執行命令。如需標記受管執行個體的詳細資訊，請參閱[《標記資源使用者指南》中的標記您的 AWS](https://docs.aws.amazon.com/tag-editor/latest/userguide/tag-editor.html)資源。 * AWS *如需有關標記受管 IoT 裝置的資訊，請參閱《 *AWS IoT Greengrass Version 2 開發人員指南*》中的[標記您的 AWS IoT Greengrass Version 2 資源](https://docs.aws.amazon.com/greengrass/v2/developerguide/tag-resources.html)。

您也可以使用 `targets` 參數設為目標的特定受管節點 ID，清單中所述的下一個部分。

若要在數百或者數千個受管節點間控制命令執行，Run Command 還包含可限制可以同時處理請求之節點數的參數，以及取消命令之前命令可以擲回的錯誤數。

**Topics**
+ [以多個受管節點為目標](#send-commands-targeting)
+ [使用速率控制](#send-commands-rate)

## 以多個受管節點為目標
<a name="send-commands-targeting"></a>

您可以透過指定標籤、 AWS 資源群組名稱或受管節點 ID，執行命令和目標受管節點。

下列範例顯示Run Command從 AWS Command Line Interface () 使用 時的命令格式AWS CLI 。將每個*範例資源預留位置*取代為您自己的資訊。使用 `[...]` 將此區段中的範本命令截斷。

**範例 1：以標籤為目標**

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=tag:tag-name,Values=tag-value \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=tag:tag-name,Values=tag-value ^
    [...]
```

------

**範例 2：依名稱鎖定 AWS 資源群組**

您可以為每個命令指定最多一個資源群組名稱。建立資源群組時，我們建議包括在分組條件中包含 `AWS::SSM:ManagedInstance` 和 `AWS::EC2::Instance` 作為資源類型。

**注意**  
若要傳送以資源群組為目標的命令，您必須已獲授予 AWS Identity and Access Management (IAM) 許可，才能列出或檢視屬於該群組的資源。如需詳細資訊，請參閱《*AWS Resource Groups 使用者指南*》中的[設定許可](https://docs.aws.amazon.com/ARG/latest/userguide/gettingstarted-prereqs.html#gettingstarted-prereqs-permissions)。

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

```
aws ssm send-command \    
    --document-name document-name \
    --targets Key=resource-groups:Name,Values=resource-group-name \
    [...]
```

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

```
aws ssm send-command ^    
    --document-name document-name ^
    --targets Key=resource-groups:Name,Values=resource-group-name ^
    [...]
```

------

**範例 3：依 AWS 資源類型鎖定資源群組**

您可以為每個命令指定最多五個資源群組類型。建立資源群組時，我們建議包括在分組條件中包含 `AWS::SSM:ManagedInstance` 和 `AWS::EC2::Instance` 作為資源類型。

**注意**  
為了傳送以資源群組為目標的命令，您必須獲得 IAM 許可來列出或檢視屬於該群組的資源。如需詳細資訊，請參閱《[AWS Resource Groups 使用者指南](https://docs.aws.amazon.com/ARG/latest/userguide/gettingstarted-prereqs.html#gettingstarted-prereqs-permissions)》中的*設定許可*。

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

```
aws ssm send-command \    
    --document-name document-name \
    --targets Key=resource-groups:ResourceTypeFilters,Values=resource-type-1,resource-type-2 \
    [...]
```

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

```
aws ssm send-command ^    
    --document-name document-name ^
    --targets Key=resource-groups:ResourceTypeFilters,Values=resource-type-1,resource-type-2 ^
    [...]
```

------

**範例 4：以執行個體 ID 為目標**

下列範例示範如何將 `instanceids` 金鑰與 `targets` 參數搭配使用才能鎖定受管節點。您可以使用此金鑰以受管 AWS IoT Greengrass 核心裝置為目標，因為每個裝置都會指派 mi-*ID\$1number*。您可以在 Fleet Manager ( AWS Systems Manager中的工具) 中檢視裝置 ID。

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=instanceids,Values=instance-ID-1,instance-ID-2,instance-ID-3 \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=instanceids,Values=instance-ID-1,instance-ID-2,instance-ID-3 ^
    [...]
```

------

如果您使用名為 `Environment` 的 `Key` 以及 `Development`、`Test` 和 `Pre-production` 的 `Values` 以及 `Production`，為不同環境的受管節點加上標記，則您可以使用 `targets` 參數搭配以下語法，將命令傳送至這些環境中的所有節點*之一*。

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=tag:Environment,Values=Development \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=tag:Environment,Values=Development ^
    [...]
```

------

您可以透過新增到 `Values` 清單，在其他環境中以其他受管節點為目標。使用逗號分隔的獨立項目。

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=tag:Environment,Values=Development,Test,Pre-production \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=tag:Environment,Values=Development,Test,Pre-production ^
    [...]
```

------

**變異**：使用多個 `Key` 條件精簡您的目標

您可以透過包含多個 `Key` 條件來精簡您命令的目標數。如果您包含多個 `Key` 條件，系統會將符合*所有*條件的受管節點視為目標。以下命令為將標上金融部門*和*資料庫伺服器角色的所有受管節點視為目標。

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=tag:Department,Values=Finance Key=tag:ServerRole,Values=Database \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=tag:Department,Values=Finance Key=tag:ServerRole,Values=Database ^
    [...]
```

------

**變異**：使用多個 `Key` 和 `Value` 條件

在之前的範例中展開，您可以透過在 `Values` 條件中包含額外的項目，來將多個部門和多個伺服器角色視為目標。

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=tag:Department,Values=Finance,Marketing Key=tag:ServerRole,Values=WebServer,Database \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=tag:Department,Values=Finance,Marketing Key=tag:ServerRole,Values=WebServer,Database ^
    [...]
```

------

**變化**：使用多個 `Values` 條件鎖定標記的受管節點

如果您使用名為 `Department` 的 `Key` 以及 `Sales` 和 `Finance` 的 `Values`，為不同環境的受管節點加上標記，則您可以使用 `targets` 參數搭配以下語法，將命令傳送至這些環境中的所有節點。

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=tag:Department,Values=Sales,Finance \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=tag:Department,Values=Sales,Finance ^
    [...]
```

------

您最多可以指定五個索引鍵，每個索引鍵可以指定五個數值。

如果標籤索引鍵 (標籤名稱) 或標籤值包含空格，則將標籤索引鍵或值括在引號中，如下範例所示。

**範例**：`Value` 標籤中的空格

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=tag:OS,Values="Windows Server 2016" \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=tag:OS,Values="Windows Server 2016" ^
    [...]
```

------

**範例**：`tag` 索引鍵中的空格和 `Value`

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key="tag:Operating System",Values="Windows Server 2016" \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key="tag:Operating System",Values="Windows Server 2016" ^
    [...]
```

------

**範例** ：在 `Values` 清單的單一項目中的空格。

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

```
aws ssm send-command \
    --document-name document-name \
    --targets Key=tag:Department,Values="Sales","Finance","Systems Mgmt" \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --targets Key=tag:Department,Values="Sales","Finance","Systems Mgmt" ^
    [...]
```

------

## 使用速率控制
<a name="send-commands-rate"></a>

您可以控制命令傳送到群組中受管節點的速率，方法是使用*並行控制項*和*錯誤控制項*。

**Topics**
+ [使用並行控制](#send-commands-velocity)
+ [使用錯誤控制](#send-commands-maxerrors)

### 使用並行控制
<a name="send-commands-velocity"></a>

您可以使用 `max-concurrency` 參數 (**Run a command** (執行命令) 頁面中的 **Concurrecy** (並行) 選項) 控制同時執行之受管節點的數量。您可以指定絕對數量的受管節點 (例如 **10**) 或目標集的百分比 (例如 **10%**)。佇列系統會將命令傳遞至單一節點並等到系統確認初始叫用，再將該命令傳送至另外兩個節點。系統會以指數方式將命令傳送至更多受管節點，直到系統達到 `max-concurrency` 值為止。`max-concurrency` 的預設值為 50。以下範例說明您為 `max-concurrency` 參數指定數值的方式：

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

```
aws ssm send-command \
    --document-name document-name \
    --max-concurrency 10 \
    --targets Key=tag:Environment,Values=Development \
    [...]
```

```
aws ssm send-command \
    --document-name document-name \
    --max-concurrency 10% \
    --targets Key=tag:Department,Values=Finance,Marketing Key=tag:ServerRole,Values=WebServer,Database \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --max-concurrency 10 ^
    --targets Key=tag:Environment,Values=Development ^
    [...]
```

```
aws ssm send-command ^
    --document-name document-name ^
    --max-concurrency 10% ^
    --targets Key=tag:Department,Values=Finance,Marketing Key=tag:ServerRole,Values=WebServer,Database ^
    [...]
```

------

### 使用錯誤控制
<a name="send-commands-maxerrors"></a>

透過使用 `max-errors` 參數(**Run a command** (執行命令) 頁面中的 **Error threshold** (錯誤閾值) 欄位) 設定錯誤限制，您也可以控制數百個或數千個受管節點的命令執行。參數會指定在系統停止將命令傳送至其他受管節點前允許的錯誤數。您可以指定錯誤的絕對數目 (例如 **10**)，或目標集的百分比 (例如 **10%**)。舉例來說，如果您指定 **3**，系統會在收到第 4 個錯誤時停止傳送命令。如果您指定 **0**，系統會在第一個錯誤結果傳回後停止將命令傳送至其他受管節點。如果您將命令傳送至 50 個受管節點，並將 `max-errors` 設為 **10%**，系統會在收到第六個錯誤時停止將命令傳送至其他節點。

達到 `max-errors` 時，系統會允許完成已在執行命令的呼叫，但一些呼叫也可能會失敗。如果您需要確保不會有超過 `max-errors` 個失敗的呼叫，請將 `max-concurrency` 設定為 **1**，以每次執行一個呼叫。錯誤上限預設值為 0。以下範例說明您為 `max-errors` 參數指定數值的方式：

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

```
aws ssm send-command \
    --document-name document-name \
    --max-errors 10 \
    --targets Key=tag:Database,Values=Development \
    [...]
```

```
aws ssm send-command \
    --document-name document-name \
    --max-errors 10% \
    --targets Key=tag:Environment,Values=Development \
    [...]
```

```
aws ssm send-command \
    --document-name document-name \
    --max-concurrency 1 \
    --max-errors 1 \
    --targets Key=tag:Environment,Values=Production \
    [...]
```

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

```
aws ssm send-command ^
    --document-name document-name ^
    --max-errors 10 ^
    --targets Key=tag:Database,Values=Development ^
    [...]
```

```
aws ssm send-command ^
    --document-name document-name ^
    --max-errors 10% ^
    --targets Key=tag:Environment,Values=Development ^
    [...]
```

```
aws ssm send-command ^
    --document-name document-name ^
    --max-concurrency 1 ^
    --max-errors 1 ^
    --targets Key=tag:Environment,Values=Production ^
    [...]
```

------