

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

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

# 使用 State Manager 關聯排程自動化
<a name="scheduling-automations-state-manager-associations"></a>

您可以透過將 State Manager 與執行手冊建立關聯來啟動自動化。State Manager 是 AWS Systems Manager中的工具。透過建立與 Runbook 的State Manager關聯，您可以鎖定不同類型的 AWS 資源。例如，您可以建立在 AWS 資源上強制執行所需狀態的關聯，包括下列項目：
+ 將 Systems Manager 角色連接到 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體以作為*受管執行個體*。
+ 為安全群組強制執行所需的輸入和輸出規則。
+ 建立或刪除 Amazon DynamoDB 備份。
+ 建立 Amazon Elastic Block Store (Amazon EBS) 快照。
+ 關閉 Amazon Simple Storage Service (Amazon S3) 儲存貯體上的讀取和寫入許可。
+ 啟動、重新啟動或停止受管執行個體和 Amazon Relational Database Service (Amazon RDS) 執行個體。
+ 將修補程式套用至 Linux、macOS、和 WiWindows AMIs。

使用下列程序建立使用 AWS Systems Manager 主控台和 AWS Command Line Interface () 執行自動化的State Manager關聯AWS CLI。如需有關關聯的一般資訊，以及有關建立使用 SSM `Command` 文件或 `Policy` 文件之關聯的資訊，請參閱[建立關聯](state-manager-associations-creating.md)。

**開始之前**  
使用 State Manager 執行自動化前，請注意以下重要的詳細資訊。
+ 在您建立使用執行手冊的關聯之前，請先確認您已設定 Automation ( AWS Systems Manager中的工具) 的許可。如需詳細資訊，請參閱[設定自動化](automation-setup.md)。
+ 使用 Runbook 的 State Manager 關聯會影響您 AWS 帳戶中同時執行的自動化作業的最大數量。一次最多可執行 100 個並行自動化作業。如需相關資訊，請參閱《Amazon Web Services 一般參考》**中的 [Systems Manager 服務配額](https://docs.aws.amazon.com/general/latest/gr/ssm.html#limits_ssm)一節。
+ 執行自動化時，State Manager 不會在 AWS CloudTrail中記錄由自動化啟動的 API 操作。
+ Systems Manager 會自動建立服務連結角色，以便 State Manager 擁有呼叫 Systems Manager Automation API 操作的許可。如果需要，您可以從 或 執行下列命令， AWS CLI 自行建立服務連結角色 AWS Tools for PowerShell。

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

  ```
  aws iam create-service-linked-role \
  --aws-service-name ssm.amazonaws.com
  ```

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

  ```
  aws iam create-service-linked-role ^
  --aws-service-name ssm.amazonaws.com
  ```

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

  ```
  New-IAMServiceLinkedRole `
  -AWSServiceName ssm.amazonaws.com
  ```

------

  如需服務連結角色的詳細資訊，請參閱[使用 Systems Manager 的服務連結角色](using-service-linked-roles.md)。

## 建立執行自動化的關聯 (主控台)
<a name="create-automation-association-console"></a>

下列程序說明如何使用 Systems Manager 主控台來建立執行自動化的 State Manager 關聯。

**建立執行自動化的 State Manager 關聯**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **State Manager**，然後選擇 **Create association (建立關聯)**。

1. 在 **Name (名稱)** 欄位中指定名稱。此為選用操作，但建議您採用。

1. 在 **Document** (文件) 清單中，選擇 Runbook。使用搜尋列來篩選 **Document type : Equal : Automation** Runbook。若要查看更多 Runbook，請使用搜尋列右側的號碼。
**注意**  
您可以選擇 Runbook 名稱檢視 Runbook 資訊。

1. 選擇 **Simple execution (簡易執行)**，透過為這些目標指定資源 ID，在一或多個目標上執行自動化。選擇**速率控制**，透過指定標籤或 等目標選項，跨 AWS 資源機群執行自動化 AWS Resource Groups。您也可以透過指定並行和錯誤閾值，控制在資源上的自動化操作。

   如果選擇 **Rate control** (速率控制)，便會顯示 **Targets** (目標) 部分。

1. 在 **Targets (目標)** 部分，選擇將資源設為目標的方法。

   1. (必要) 在 **Parameter (參數)** 清單中，選擇一個參數。**Parameter** (參數) 清單中的項目，是由您在此程序一開始所選取 Runbook 中的參數決定。藉由選擇參數，您就會定義自動化執行的資源類型。

   1. (必要) 在 **Targets (目標)** 清單中，選擇將資源設為目標的方法。
      + **Resource Group (資源群組)**：從 **Resource Group (資源群組)** 清單中選擇群組名稱。如需在 Runbook AWS Resource Groups 中鎖定目標的詳細資訊，請參閱 [鎖定目標 AWS Resource Groups](running-automations-map-targets.md#target-resource-groups)。
      + **Tags (標籤)**：在提供的欄位中輸入標籤鍵和 (選擇性) 標籤值。選擇**新增**。如需有關在 Runbook 中鎖定標籤的詳細資訊，請參閱 [將標籤設為目標](running-automations-map-targets.md#target-tags)。
      + **Parameter Values (參數值)**：在 **Input parameters (輸入參數)** 部分輸入值。如果您指定多個值，Systems Manager 會於每個指定的值執行子自動化。

        例如，假設您的 Runbook 包括 **InstanceID** 參數。如果您在執行自動化時以 **InstanceID** 參數的值為目標，則 Systems Manager 會針對每個指定的執行個體 ID 值執行子自動化。當自動化完成執行每個指定的執行個體，或是自動化失敗，父自動化就會完成。您最多可將 50 個參數值設為目標。如需有關在 Runbook 中設定參數值目標的詳細資訊，請參閱 [將參數值設為目標](running-automations-map-targets.md#target-parameter-values)。

1. 在 **Input Parameters (輸入參數)** 部分，指定所需的輸入參數。

   如果選擇使用標籤或資源群組將資源設為目標，您可能不必選擇 **Input parameters** (輸入參數) 部分中的某些選項。例如，假設您選擇 `AWS-RestartEC2Instance` Runbook，而您選擇使用標籤將執行個體設為目標，則您不必在 **Input parameters** (輸入參數) 部分中指定或選擇執行個體 ID。自動化會使用您指定的標籤找出要重新啟動的執行個體。
**重要**  
您必須在 **AutomationAssumeRole** 欄位中指定角色 ARN。 State Manager會使用擔任角色來呼叫 Runbook 中 AWS 服務 指定的 ，並代表您執行自動化關聯。

1. 在 **Specify schedule (指定排程)** 部分，如果想以固定間隔執行關聯，請選擇 **On Schedule (依排程)**。如果選擇此選項，然後使用 Cron 或 Rate 運算式，利用提供的選項建立排程。如需 State Manager 適用的 Cron 和 Rate 運算式的詳細資訊，請參閱[關聯的 Cron 與 Rate 運算式](reference-cron-and-rate-expressions.md#reference-cron-and-rate-expressions-association)。
**注意**  
Rate 運算式是執行 Runbook 的 State Manager 關聯所偏好的排程機制。Rate 運算式可在您達到自動化同時執行最大數量下提供更多執行關聯的靈活性。使用速率排程時，Systems Manager 便能在收到同時自動化已達最大值且遭調節的通知後立即重試自動化。

   如果您希望執行一次關聯，請選擇 **No schedule (無排程)**。

1. （選用） 在**速率控制**區段中，選擇**並行**和**錯誤閾值**選項，以控制跨 AWS 資源的自動化部署。

   1. 在 **Concurrency (並行)** 部分，選擇一個選項：
      + 選擇 **targets** (目標)，輸入可以同時執行自動化的目標絕對數量。
      + 選擇 **percentage** (百分比)，輸入可以同時執行自動化的目標集百分比。

   1. 在 **Error threshold (錯誤閾值)** 部分，選擇一個選項：
      + 選擇 **errors** (錯誤)，輸入在 Automation 停止傳送自動化至其他資源之前允許的錯誤絕對數量。
      + 選擇 **percentage** (百分比)，輸入在 Automation 停止傳送自動化至其他資源之前允許的錯誤百分比。

   如需有關使用目標和速率控制與自動化的詳細資訊，請參閱[大規模執行自動化操作](running-automations-scale.md)。

1. 選擇 **Create Association (建立關聯)**。
**重要**  
建立關聯時，關聯便會立即在指定的目標上執行。接著關聯會依照您選擇的 Cron 或 Rate 運算式執行。如果您選擇 **No schedule** (無排程)，關聯不會再次執行。

## 建立執行自動化的關聯 (命令列)
<a name="create-automation-association-cli"></a>

下列程序說明如何使用 AWS CLI （在 Linux 或 上Windows Server) 或 AWS Tools for PowerShell 來建立執行自動化的State Manager關聯。

**開始之前**  
完成下列程序之前，應先確認已建立含有執行執行手冊所需許可的 IAM 服務角色，並為 Automation ( AWS Systems Manager中的工具) 設定了信任關係。如需詳細資訊，請參閱[任務 1：建立自動化的服務角色](automation-setup-iam.md#create-service-role)。

**建立執行自動化的關聯**

1.  AWS Tools for PowerShell如果您尚未安裝和設定 AWS CLI 或 。

   如需相關資訊，請參閱[安裝或更新 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)。

1. 執行以下命令來檢視文件清單。

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

   ```
   aws ssm list-documents
   ```

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

   ```
   aws ssm list-documents
   ```

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

   ```
   Get-SSMDocumentList
   ```

------

   記下您要用於關聯的 Runbook 名稱。

1. 執行以下命令來檢視您建立的 Runbook 詳細資訊。在下列命令中，用您自己的資訊取代 {{Runbook name}} (Runbook 名稱)。

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

   ```
   aws ssm describe-document \
   --name {{runbook name}}
   ```

   記下您希望用於 `--automation-target-parameter-name` 選項的參數名稱 (例如 `InstanceId`)。此參數會決定自動化執行的資源類型。

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

   ```
   aws ssm describe-document ^
   --name {{runbook name}}
   ```

   記下您希望用於 `--automation-target-parameter-name` 選項的參數名稱 (例如 `InstanceId`)。此參數會決定自動化執行的資源類型。

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

   ```
   Get-SSMDocumentDescription `
   -Name {{runbook name}}
   ```

   記下您希望用於 `AutomationTargetParameterName` 選項的參數名稱 (例如 `InstanceId`)。此參數會決定自動化執行的資源類型。

------

1. 使用 State Manager 關聯建立執行自動化的命令。將每個{{範例資源預留位置}}取代為您自己的資訊。

   「使用標籤設定目標」**

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

   ```
   aws ssm create-association \
   --association-name {{association name}} \
   --targets Key=tag:{{key name}},Values={{value}} \
   --name {{runbook name}} \
   --parameters AutomationAssumeRole={{arn:aws:iam::123456789012:role/RunbookAssumeRole}} \
   --automation-target-parameter-name {{target parameter}} \
   --schedule "{{cron or rate expression}}"
   ```

**注意**  
如果您使用 建立關聯 AWS CLI，請使用 `--targets` 參數將關聯的目標執行個體設為目標。請勿使用 `--instance-id` 參數。`--instance-id` 參數是舊參數。

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

   ```
   aws ssm create-association ^
   --association-name {{association name}} ^
   --targets Key=tag:{{key name}},Values={{value}} ^
   --name {{runbook name}} ^
   --parameters AutomationAssumeRole={{arn:aws:iam::123456789012:role/RunbookAssumeRole}} ^
   --automation-target-parameter-name {{target parameter}} ^
   --schedule "{{cron or rate expression}}"
   ```

**注意**  
如果您使用 建立關聯 AWS CLI，請使用 `--targets` 參數將關聯的目標執行個體設為目標。請勿使用 `--instance-id` 參數。`--instance-id` 參數是舊參數。

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

   ```
   $Targets = New-Object Amazon.SimpleSystemsManagement.Model.Target
   $Targets.Key = "tag:{{key name}}"
   $Targets.Values = "{{value}}"
   
   New-SSMAssociation `
   -AssociationName "{{association name}}" `
   -Target $Targets `
   -Name "{{runbook name}}" `
   -Parameters @{
   "AutomationAssumeRole"="{{arn:aws:iam::123456789012:role/RunbookAssumeRole}}" } `
   -AutomationTargetParameterName "{{target parameter}}" `
   -ScheduleExpression "{{cron or rate expression}}"
   ```

**注意**  
如果您使用 建立關聯 AWS Tools for PowerShell，請使用 `Target` 參數將關聯的目標執行個體設為目標。請勿使用 `InstanceId` 參數。`InstanceId` 參數是舊參數。

------

   「使用參數值設定目標」**

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

   ```
   aws ssm create-association \
   --association-name {{association name}} \
   --targets Key=ParameterValues,Values={{value}},{{value 2}},{{value 3}} \
   --name {{runbook name}} \
   --parameters AutomationAssumeRole={{arn:aws:iam::123456789012:role/RunbookAssumeRole}} \
   --automation-target-parameter-name {{target parameter}} \
   --schedule "{{cron or rate expression}}"
   ```

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

   ```
   aws ssm create-association ^
   --association-name {{association name}} ^
   --targets Key=ParameterValues,Values={{value}},{{value 2}},{{value 3}} ^
   --name {{runbook name}} ^
   --parameters AutomationAssumeRole={{arn:aws:iam::123456789012:role/RunbookAssumeRole}} ^
   --automation-target-parameter-name {{target parameter}} ^
   --schedule "{{cron or rate expression}}"
   ```

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

   ```
   $Targets = New-Object Amazon.SimpleSystemsManagement.Model.Target
   $Targets.Key = "ParameterValues"
   $Targets.Values = "{{value}}","{{value 2}}","{{value 3}}"
   
   New-SSMAssociation `
   -AssociationName "{{association name}}" `
   -Target $Targets `
   -Name "{{runbook name}}" `
   -Parameters @{
   "AutomationAssumeRole"="{{arn:aws:iam::123456789012:role/RunbookAssumeRole}}"} `
   -AutomationTargetParameterName "{{target parameter}}" `
   -ScheduleExpression "{{cron or rate expression}}"
   ```

------

   *使用 鎖定目標 AWS Resource Groups*

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

   ```
   aws ssm create-association \
   --association-name {{association name}} \
   --targets Key=ResourceGroup,Values={{resource group name}} \
   --name {{runbook name}} \
   --parameters AutomationAssumeRole={{arn:aws:iam::123456789012:role/RunbookAssumeRole}} \
   --automation-target-parameter-name {{target parameter}} \
   --schedule "{{cron or rate expression}}"
   ```

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

   ```
   aws ssm create-association ^
   --association-name {{association name}} ^
   --targets Key=ResourceGroup,Values={{resource group name}} ^
   --name {{runbook name}} ^
   --parameters AutomationAssumeRole={{arn:aws:iam::123456789012:role/RunbookAssumeRole}} ^
   --automation-target-parameter-name {{target parameter}} ^
   --schedule "{{cron or rate expression}}"
   ```

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

   ```
   $Targets = New-Object Amazon.SimpleSystemsManagement.Model.Target
   $Targets.Key = "ResourceGroup"
   $Targets.Values = "{{resource group name}}"
   
   New-SSMAssociation `
   -AssociationName "{{association name}}" `
   -Target $Targets `
   -Name "{{runbook name}}" `
   -Parameters @{
   "AutomationAssumeRole"="{{arn:aws:iam::123456789012:role/RunbookAssumeRole}}"} `
   -AutomationTargetParameterName "{{target parameter}}" `
   -ScheduleExpression "{{cron or rate expression}}"
   ```

------

   *鎖定多個帳戶和區域*

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

   ```
   aws ssm create-association \
   --association-name {{association name}} \
   --targets Key=ResourceGroup,Values={{resource group name}} \
   --name {{runbook name}} \
   --parameters AutomationAssumeRole={{arn:aws:iam::123456789012:role/RunbookAssumeRole}} \
   --automation-target-parameter-name {{target parameter}} \
   --schedule "{{cron or rate expression}}" \ 
   --target-locations Accounts={{111122223333}},{{444455556666}},{{444455556666}},Regions={{region}},{{region}}
   ```

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

   ```
   aws ssm create-association ^
   --association-name {{association name}} ^
   --targets Key=ResourceGroup,Values={{resource group name}} ^
   --name {{runbook name}} ^
   --parameters AutomationAssumeRole={{arn:aws:iam::123456789012:role/RunbookAssumeRole}} ^
   --automation-target-parameter-name {{target parameter}} ^
   --schedule "{{cron or rate expression}}" ^ 
   --target-locations Accounts={{111122223333}},{{444455556666}},{{444455556666}},Regions={{region}},{{region}}
   ```

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

   ```
   $Targets = New-Object Amazon.SimpleSystemsManagement.Model.Target
   $Targets.Key = "ResourceGroup"
   $Targets.Values = "{{resource group name}}"
   
   New-SSMAssociation `
   -AssociationName "{{association name}}" `
   -Target $Targets `
   -Name "{{runbook name}}" `
   -Parameters @{
   "AutomationAssumeRole"="{{arn:aws:iam::123456789012:role/RunbookAssumeRole}}"} `
   -AutomationTargetParameterName "{{target parameter}}" `
   -ScheduleExpression "{{cron or rate expression}}" `
   -TargetLocations @{
       "Accounts"=["{{111122223333}},{{444455556666}},{{444455556666}}"],
       "Regions"=["{{region}},{{region}}"]
   ```

------

   命令會傳回與以下相似的新關聯詳細資訊：

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

   ```
   {
   "AssociationDescription": {
       "ScheduleExpression": "cron(0 7 ? * MON *)",
       "Name": "AWS-StartEC2Instance",
       "Parameters": {
           "AutomationAssumeRole": [
               "arn:aws:iam::123456789012:role/RunbookAssumeRole"
           ]
       },
       "Overview": {
           "Status": "Pending",
           "DetailedStatus": "Creating"
       },
       "AssociationId": "1450b4b7-bea2-4e4b-b340-01234EXAMPLE",
       "DocumentVersion": "$DEFAULT",
       "AutomationTargetParameterName": "InstanceId",
       "LastUpdateAssociationDate": 1564686638.498,
       "Date": 1564686638.498,
       "AssociationVersion": "1",
       "AssociationName": "CLI",
       "Targets": [
           {
               "Values": [
                   "DEV"
               ],
               "Key": "tag:ENV"
           }
       ]
   }
   }
   ```

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

   ```
   {
   "AssociationDescription": {
       "ScheduleExpression": "cron(0 7 ? * MON *)",
       "Name": "AWS-StartEC2Instance",
       "Parameters": {
           "AutomationAssumeRole": [
               "arn:aws:iam::123456789012:role/RunbookAssumeRole"
           ]
       },
       "Overview": {
           "Status": "Pending",
           "DetailedStatus": "Creating"
       },
       "AssociationId": "1450b4b7-bea2-4e4b-b340-01234EXAMPLE",
       "DocumentVersion": "$DEFAULT",
       "AutomationTargetParameterName": "InstanceId",
       "LastUpdateAssociationDate": 1564686638.498,
       "Date": 1564686638.498,
       "AssociationVersion": "1",
       "AssociationName": "CLI",
       "Targets": [
           {
               "Values": [
                   "DEV"
               ],
               "Key": "tag:ENV"
           }
       ]
   }
   }
   ```

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

   ```
   Name                  : AWS-StartEC2Instance
   InstanceId            : 
   Date                  : 8/1/2019 7:31:38 PM
   Status.Name           : 
   Status.Date           : 
   Status.Message        : 
   Status.AdditionalInfo :
   ```

------

**注意**  
如果您使用標籤在一或多個目標執行個體上建立關聯，然後從執行個體移除標籤，則該執行個體將不再執行該關聯。系統會從State Manager文件中取消該執行個體的關聯。

## 疑難排解由 State Manager 關聯執行的自動化
<a name="troubleshooting-automation-associations"></a>

Systems Manager Automation 強制執行 100 個並行自動化，以及每個區域每個帳戶 1,000 個排入佇列的自動化限制。如果使用 Runbook 的 State Manager 關聯顯示 **Failed** (失敗) 狀態和 **AutomationExecutionLimitExceeded** 詳細狀態，則表示可能已達到自動化的限制。因此，Systems Manager 會調節自動化。要解決此問題，請依照下列步驟：
+ 為您的關聯使用不同的 Rate 或 Cron 運算式。例如，如果關聯排程每隔 30 分鐘執行，則變更運算式可每一或二小時執行一次。
+ 刪除狀態為 **Pending** (待定) 的現有自動化。透過刪除這些自動化，即可清除目前的佇列。