

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

# 在 CloudFormation StackSets 上執行漂移偵測
<a name="stacksets-drift"></a>

即使您透過 CloudFormation 管理您的堆疊，以及這些堆疊中包含的資源，使用者仍然可以在 CloudFormation 外部變更這些資源。使用者可以使用建立資源的基礎服務來直接編輯資源。藉由在 StackSet 上執行漂移偵測，您可以判斷是否有任何屬於該 StackSet 的堆疊執行個體與其預期組態出現差異 (或是產生了*漂移*)。

**Topics**
+ [CloudFormation 在 StackSet 上執行漂移偵測的方式](#stacksets-drift-how)
+ [偵測 StackSet 上的漂移 (主控台)](#stacksets-drift-console-procedure)
+ [偵測 StackSet 上的漂移 (AWS CLI)](#stacksets-drift-cli-procedure)
+ [停止 StackSet 上的漂移偵測](#stacksets-drift-stop)

## CloudFormation 在 StackSet 上執行漂移偵測的方式
<a name="stacksets-drift-how"></a>

CloudFormation 在 StackSet 上執行漂移偵測時，會在與 StackSet 中每個堆疊執行個體相關聯的堆疊上執行漂移偵測。為了執行此作業，CloudFormation 會將堆疊中每個資源目前的狀態與該資源的預期狀態進行比較，如堆疊範本及任何指定輸入參數所定義。如果資源目前的狀態與預期狀態不同，便會將該資源視為已偏離。如果堆疊中有一或多個資源產生偏離，便會將堆疊本身視為已偏離，且也會將與該堆疊建立關聯的堆疊執行個體視為已偏離。如果堆疊中有一或多個堆疊執行個體產生漂移，也會將 StackSet 本身視為已漂移。

漂移偵測可以識別非受管的變更，即在 CloudFormation 外部對堆疊進行的變更。直接透過 CloudFormation 對堆疊進行的變更，而非在 StackSet 層級進行的變更，則不會視為漂移。例如，假設您有一個與 StackSet 中堆疊執行個體建立關聯的堆疊。如果您使用 CloudFormation 來更新堆疊，以使用不同的範本，這便不會被視為漂移，即使該堆疊現在具備的範本與屬於該 StackSet 的任何其他堆疊不同也一樣。這是因為堆疊仍然與其預期範本和 CloudFormation 中的參數組態相符。

如需 CloudFormation 在堆疊上執行漂移偵測方式的詳細資訊，請參閱 [透過漂移偵測功能，偵測堆疊和資源的未受管組態變更](using-cfn-stack-drift.md)。

因為 CloudFormation 會在每個堆疊上個別執行漂移偵測，其會在判斷堆疊是否漂移時，將任何覆寫的參數值納入考量。如需覆寫堆疊執行個體中範本參數的詳細資訊，請參閱 [覆寫 CloudFormation StackSet 中堆疊的參數值](stackinstances-override.md)。

如果您[直接在與堆疊執行個體建立關聯的堆疊上](using-cfn-stack-drift.md)執行漂移偵測，這些漂移結果便無法從 **StackSets** (堆疊集合) 主控台頁面取得。

## 偵測 StackSet 上的漂移 (主控台)
<a name="stacksets-drift-console-procedure"></a>

**偵測 StackSet 上的漂移**

1. 在 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) 開啟 CloudFormation 主控台。

1. 在 **StackSets** 頁面上，選取您希望執行漂移偵測的 StackSets。

1. 在 **Actions (動作)** 選單中，選取 **Detect drifts (偵測偏離)**。

   CloudFormation 會顯示資訊列，說明已啟動所選 StackSet 的漂移偵測。

1. 選用：監控漂移偵測操作的進度：

   1. 選取 StackSet 名稱，以顯示 **StackSet 詳細資訊**頁面。

   1. 選取 **Operations (操作)** 標籤、選取漂移偵測操作，然後選取 **View drift details (檢視漂移詳細資訊)**。

   CloudFormation 會顯示 **Operation details** (操作詳細資訊) 對話方塊。

1. 等待 CloudFormation 完成漂移偵測操作。當漂移偵測操作完成後，CloudFormation 會更新堆疊的**漂移狀態**和**上次檢查漂移的時間**。這些欄位會列在所選取 StackSet 的 **StackSet 詳細資訊**頁面的**概觀**索引標籤上。

   漂移偵測操作可能會耗費一些時間，這取決於 StackSet 中所包含的堆疊執行個體數，及包含在 StackSet 中的資源數。您在指定 StackSet 上一次只能執行單一漂移偵測操作。即使關閉資訊列，CloudFormation 仍會繼續執行漂移偵測操作。

1. 如要檢閱 StackSet 中堆疊執行個體的漂移偵測結果，請選取**堆疊執行個體**索引標籤。

   **Stack name (堆疊名稱)** 行會列出與每個堆疊執行個體建立關聯的堆疊名稱，**Drift status (偏離狀態)** 行則會列出該堆疊的偏離狀態。如果堆疊的一個或多個資源已偏離，堆疊就視為已偏離。

1. 檢閱與特定堆疊執行個體建立關聯的堆疊漂移偵測結果：

   1. 選擇**操作**子標籤。

   1. 選擇您想要檢視漂移偵測結果的漂移操作。分割面板將顯示堆疊執行個體狀態和所選操作的原因。對於漂移操作，狀態原因欄顯示堆疊執行個體的漂移狀態。

   1. 選擇您想檢視漂移詳細資訊的堆疊執行個體，然後選擇**檢視資源漂移**。在**資源漂移**頁面上，**資源漂移狀態**表單會列出每項堆疊資源及其漂移狀態，以及上次在資源上啟動漂移偵測的時間。每個資源會顯示邏輯 ID 和實體 ID，以協助您識別它們。

1. 您可以根據資源的偏移狀態，使用 **Drift status (偏離狀態)** 欄排序資源。

   檢視已修改過的資源的詳細資訊：

   1. 在選取資源的情況下，選擇**檢視漂移詳細資訊**。

     CloudFormation 會顯示該特定資源的漂移詳細資訊頁面。此頁面列出了資源的差異。其還會列出資源的預期屬性值和目前屬性值。
**注意**  
如果堆疊屬於與您目前登入的區域和帳戶不同的區域和帳戶，**偵測漂移**按鈕將會停用，且您將無法檢視詳細資訊。

## 偵測 StackSet 上的漂移 (AWS CLI)
<a name="stacksets-drift-cli-procedure"></a>

若要使用 偵測整個堆疊上的偏離 AWS CLI，請使用下列程序：

**偵測 StackSet 上的漂移**

1. 使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/detect-stack-set-drift.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/detect-stack-set-drift.html) 命令以偵測整個 StackSet 以及其相關聯堆疊執行個體上的漂移。

   以下範例會在 StackSet `stack-set-drift-example` 上啟動漂移偵測。

   ```
   aws cloudformation detect-stack-set-drift \
       --stack-set-name stack-set-drift-example
   ```

   輸出：

   ```
   {
       "OperationId": "c36e44aa-3a83-411a-b503-cb611example"
   }
   ```

1. 因為 StackSet 漂移偵測操作可能會長時間執行，請使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-set-operation.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-set-operation.html) 命令來監控漂移操作的狀態。此命令接受由 **detect-stack-set-drift** 命令傳回的 StackSet 操作 ID。

   下列範例會使用先前範例的操作 ID 來傳回 StackSet 漂移偵測操作的資訊。在此範例中，操作仍正在執行。在與 StackSet 建立關聯的七個堆疊執行個體中，已找到一個堆疊執行個體已產生漂移，兩個執行個體正在同步，而其餘四個堆疊執行個體的漂移偵測則仍在進行。由於一個執行個體已產生漂移， StackSet 本身的漂移狀態現在是 `DRIFTED`。

   ```
   aws cloudformation describe-stack-set-operation \
       --stack-set-name stack-set-drift-example \
       --operation-id c36e44aa-3a83-411a-b503-cb611example
   ```

   輸出：

   ```
   {
       "StackSetOperation": {
           "Status": "RUNNING",
           "AdministrationRoleARN": "arn:aws:iam::123456789012:role/AWSCloudFormationStackSetAdministrationRole",
           "OperationPreferences": {
               "RegionOrder": []
           },
           "ExecutionRoleName": "AWSCloudFormationStackSetExecutionRole",
           "StackSetDriftDetectionDetails": {
               "DriftedStackInstancesCount": 1,
               "TotalStackInstancesCount": 7,
               "LastDriftCheckTimestamp": "2019-12-04T20:34:28.543Z",
               "InSyncStackInstancesCount": 2,
               "InProgressStackInstancesCount": 4,
               "DriftStatus": "DRIFTED",
               "FailedStackInstancesCount": 0
           },
           "Action": "DETECT_DRIFT",
           "CreationTimestamp": "2019-12-04T20:33:13.673Z",
           "StackSetId": "stack-set-drift-example:bd1f4017-d4f9-432e-a73f-8c22example",
           "OperationId": "c36e44aa-3a83-411a-b503-cb611example"
       }
   }
   ```

   稍後執行相同的命令，此範例便會顯示漂移偵測操作完成後傳回的資訊。與此 StackSet 建立關聯的七個堆疊執行個體中，有兩個已產生漂移，使 StackSet 本身的漂移狀態成為 `DRIFTED`。

   ```
   aws cloudformation describe-stack-set-operation \
       --stack-set-name stack-set-drift-example \
       --operation-id c36e44aa-3a83-411a-b503-cb611example
   ```

   輸出：

   ```
   {
       "StackSetOperation": {
           "Status": "SUCCEEDED",
           "AdministrationRoleARN": "arn:aws:iam::123456789012:role/AWSCloudFormationStackSetAdministrationRole",
           "OperationPreferences": {
               "RegionOrder": []
           } 
           "ExecutionRoleName": "AWSCloudFormationStackSetExecutionRole",
           "EndTimestamp": "2019-12-04T20:37:32.829Z",
           "StackSetDriftDetectionDetails": {
               "DriftedStackInstancesCount": 2,
               "TotalStackInstancesCount": 7,
               "LastDriftCheckTimestamp": "2019-12-04T20:36:55.612Z",
               "InSyncStackInstancesCount": 5,
               "InProgressStackInstancesCount": 0,
               "DriftStatus": "DRIFTED",
               "FailedStackInstancesCount": 0
           },
           "Action": "DETECT_DRIFT",
           "CreationTimestamp": "2019-12-04T20:33:13.673Z",
           "StackSetId": "stack-set-drift-example:bd1f4017-d4f9-432e-a73f-8c22example",
           "OperationId": "c36e44aa-3a83-411a-b503-cb611example"
       }
   }
   ```

1. 完成 StackSet 的漂移偵測操作時，請使用 **describe-stack-set**、**list-stack-instances**、**describe-stack-instance** 和 **list-stack-instance-resource-drifts** 命令來檢閱結果。

   [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-set.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-set.html) 命令會包含 **describe-stack-set-operation** 命令傳回的相同詳細漂移資訊。

   ```
   aws cloudformation describe-stack-set \
       --stack-set-name stack-set-drift-example
   ```

   輸出：

   ```
   {
       "StackSet": {
           "Status": "ACTIVE",
           "Description": "Demonstration of drift detection on stack sets.",
           "Parameters": [],
           "Tags": [
               {
                   "Value": "Drift detection",
                   "Key": "Feature"
               }
           ], 
           "ExecutionRoleName": "AWSCloudFormationStackSetExecutionRole",
           "Capabilities": [],
           "AdministrationRoleARN": "arn:aws:iam::123456789012:role/AWSCloudFormationStackSetAdministrationRole",
           "StackSetDriftDetectionDetails": {
               "DriftedStackInstancesCount": 2,
               "TotalStackInstancesCount": 7,
               "LastDriftCheckTimestamp": "2019-12-04T20:36:55.612Z",
               "InProgressStackInstancesCount": 0,
               "DriftStatus": "DRIFTED",
               "DriftDetectionStatus": "COMPLETED",
               "InSyncStackInstancesCount": 5,
               "FailedStackInstancesCount": 0
           },
           "StackSetARN": "arn:aws:cloudformation:us-east-1:123456789012:stackset/stack-set-drift-example:bd1f4017-d4f9-432e-a73f-8c22example",
           "TemplateBody": [details omitted],
           "StackSetId": "stack-set-drift-example:bd1f4017-d4f9-432e-a73f-8c22ebexample",
           "StackSetName": "stack-set-drift-example"
       }
   }
   ```

   您可以使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stack-instances.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stack-instances.html) 命令來傳回與 StackSet 建立關聯的堆疊執行個體摘要資訊，包括每個堆疊執行個體的漂移狀態。

   在此範例中，在範例 StackSet 上執行 **list-stack-instances** 並將漂移狀態篩選設為，`DRIFTED`可讓您識別哪兩個堆疊執行個體的漂移狀態為 `DRIFTED`。

   ```
   aws cloudformation list-stack-instances \
       --stack-set-name stack-set-drift-example \
       --filters Name=DRIFT_STATUS,Values=DRIFTED
   ```

   輸出：

   ```
   {
   "Summaries": [
           {
   "StackId": "arn:aws:cloudformation:eu-west-1:123456789012:stack/StackSet-stack-set-drift-example-b0fb6083-60c0-4e39-af15-2f071e0db90c/0e4f0940-16d4-11ea-93d8-0641cexample",
               "Status": "CURRENT",
               "Account": "012345678910",
               "Region": "eu-west-1",
               "LastDriftCheckTimestamp": "2019-12-04T20:37:32.687Z",
               "DriftStatus": "DRIFTED",
               "StackSetId": "stack-set-drift-example:bd1f4017-d4f9-432e-a73f-8c22eexample",
               "LastOperationId": "c36e44aa-3a83-411a-b503-cb611example"
           },
           {
               "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/StackSet-stack-set-drift-example-b7fde68e-e541-44c2-b33d-ef2e2988071a/008e6030-16d4-11ea-8090-12f89example",
               "Status": "CURRENT",
               "Account": "123456789012",
               "Region": "us-east-1",
               "LastDriftCheckTimestamp": "2019-12-04T20:34:28.275Z",
               "DriftStatus": "DRIFTED",
               "StackSetId": "stack-set-drift-example:bd1f4017-d4f9-432e-a73f-8c22eexample",
               "LastOperationId": "c36e44aa-3a83-411a-b503-cb611example"
           },
           
           [additional stack instances omitted]
    
       ]
   }
   ```

   [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-instance.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-instance.html) 命令也會傳回此資訊，但只會傳回單一堆疊執行個體的資訊，如以下範例所示。

   ```
   aws cloudformation describe-stack-instance \
       --stack-set-name stack-set-drift-example \
       --stack-instance-account 012345678910 --stack-instance-region us-east-1
   ```

   輸出：

   ```
   {
       "StackInstance": {
           "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/StackSet-stack-set-drift-example-b7fde68e-e541-44c2-b33d-ef2e2988071a/008e6030-16d4-11ea-8090-12f89example",
           "Status": "CURRENT",
           "Account": "123456789012",
           "Region": "us-east-1",
           "ParameterOverrides": [],
           "DriftStatus": "DRIFTED",
           "LastDriftCheckTimestamp": "2019-12-04T20:34:28.275Z",
           "StackSetId": "stack-set-drift-example:bd1f4017-d4f9-432e-a73f-8c22eexample",
           "LastOperationId": "c36e44aa-3a83-411a-b503-cb611example"
       }
   }
   ```

1. 在您識別漂移的堆疊執行個體後，您可以使用 **list-stack-instances** 或 **describe-stack-instance** 命令傳回的堆疊執行個體資訊，來執行 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stack-instance-resource-drifts.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stack-instance-resource-drifts.html) 命令。此命令會傳回堆疊中已為特定漂移操作產生漂移的資源的詳細資訊。

   以下範例使用 `--stack-instance-resource-drift-statuses` 參數，以請求在上一個漂移操作範例中已修改或已刪除資源的堆疊漂移資訊。該請求傳回已修改的一項資源的相關資訊，包括它的兩項屬性及其更改值的詳細資訊。未刪除任何資源。

   ```
   aws cloudformation list-stack-instance-resource-drifts \
       --stack-set-name my-stack-set-with-resource-drift \
       --stack-instance-account 123456789012 \
       --stack-instance-region us-east-1 \
       --operation-id c36e44aa-3a83-411a-b503-cb611example \
       --stack-instance-resource-drift-statuses MODIFIED DELETED
   ```

   輸出：

   ```
   {
       "Summaries": [
           {
               "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/my-stack-set-with-resource-drift/489e5570-df85-11e7-a7d9-50example",
               "ResourceType": "AWS::SQS::Queue",
               "Timestamp": "2018-03-26T17:23:34.489Z",
               "PhysicalResourceId": "https://sqs.us-east-1.amazonaws.com/123456789012/my-stack-with-resource-drift-Queue-494PBHCO76H4",
               "StackResourceDriftStatus": "MODIFIED",
               "PropertyDifferences": [
                   {
                       "PropertyPath": "/DelaySeconds",
                       "ActualValue": "120",
                       "ExpectedValue": "20",
                       "DifferenceType": "NOT_EQUAL"
                   },
                   {
                       "PropertyPath": "/RedrivePolicy/maxReceiveCount",
                       "ActualValue": "12",
                       "ExpectedValue": "10",
                       "DifferenceType": "NOT_EQUAL"
                   }
               ],
               "LogicalResourceId": "Queue"
           }
       ]
   }
   ```

## 停止 StackSet 上的漂移偵測
<a name="stacksets-drift-stop"></a>

因為 StackSet 上的漂移偵測操作可能會耗費相當長的時間，有時候您可能會希望停止目前正在 StackSet 上執行的漂移偵測操作。

**在 StackSet 上停止漂移偵測 (主控台)**

1. 在 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) 開啟 CloudFormation 主控台。

1. 在 **StackSets** 頁面上，選取 StackSet 的名稱。

   CloudFormation 會顯示所選取 StackSets 的 **StackSets 詳細資訊**。

1. 在 **StackSets details (堆疊集合詳細資訊)** 頁面上，選取 **Operations (操作)** 標籤，然後選取漂移偵測操作。

1. 選取 **Stop operation (停止操作)**。

**在 StackSet 上停止漂移偵測 (AWS CLI)**
+ 使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/stop-stack-set-operation.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/stop-stack-set-operation.html) 命令。您必須提供 StackSet 名稱和漂移偵測 StackSet 操作的操作 ID。

  ```
  aws cloudformation stop-stack-set-operation \
      --stack-set-name stack-set-drift-example \
      --operation-id 624af370-311a-11e8-b6b7-500cexample
  ```