

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

# 在多區域、多帳戶組織中設定 CloudFormation 偏離偵測
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization"></a>

*Ram Kandaswamy，Amazon Web Services*

## 總結
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization-summary"></a>

Amazon Web Services (AWS) 使用者通常會尋找有效的方法來偵測資源組態不相符，包括 AWS CloudFormation 堆疊中的漂移，並盡快修正它們。 AWS Control Tower 使用 時尤其如此。

此模式提供方案解決方案，透過使用合併的資源組態變更並對這些變更採取行動來產生結果，有效率地解決問題。此解決方案專為在多個堆疊中建立多個堆疊 AWS 區域，或在多個帳戶中建立多個 CloudFormation 堆疊，或兩者結合的情況而設計。解決方案的目標如下：
+ 簡化偏離偵測程序
+ 設定通知和提醒
+ 設定合併報告

## 先決條件和限制
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization-prereqs"></a>

**先決條件**
+ AWS Config 在所有必須監控的區域和帳戶中啟用

**限制**
+ 產生的報告僅支援逗號分隔值 (CSV) 和 JSON 輸出格式。

## Architecture
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization-architecture"></a>

下圖顯示 AWS Organizations 設定多個帳戶。 AWS Config 規則會在帳戶之間進行通訊。 

![監控兩個 AWS Organizations 帳戶中堆疊的五個步驟程序。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/735d0987-b953-47f8-a9bc-b02a88957ee5/images/340cee9a-5a4e-49ea-bd73-d37dcea5e098.png)


 工作流程包含下列步驟：

1.  AWS Config 規則會偵測偏離。

1. 在其他帳戶中找到的偏離偵測結果會傳送至管理帳戶。

1. Amazon CloudWatch 規則會呼叫 AWS Lambda 函數。

1. Lambda 函數會查詢彙總結果的 AWS Config 規則。

1. Lambda 函數會通知 Amazon Simple Notification Service (Amazon SNS)，其會傳送偏離的電子郵件通知。

**自動化和擴展**

此處提供的解決方案可以同時針對其他區域和帳戶進行擴展。

## 工具
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization-tools"></a>

**AWS 服務**
+ [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) 提供 中 AWS 資源組態的詳細檢視 AWS 帳戶。這包含資源彼此之間的關係和之前的組態方式，所以您可以看到一段時間中組態和關係的變化。
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 可協助您 AWS 即時監控 AWS 資源的指標，以及您在 上執行的應用程式。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可協助您協調和管理發佈者和用戶端之間的訊息交換，包括 Web 伺服器和電子郵件地址。

## 史詩
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization-epics"></a>

### 自動化 的偏離偵測 CloudFormation
<a name="automate-drift-detection-for-cfn"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立彙總工具。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization.html) | 雲端架構師 | 
| 建立 AWS 受管規則。 | 新增 `cloudformation-stack-drift-detection-check` AWS****受管規則。規則需要一個參數值：`cloudformationArn`。<br />輸入具有偵測堆疊偏離許可的 IAM 角色 Amazon Resource Name (ARN)。角色必須具有可讓 AWS Config 擔任角色的信任政策。 | 雲端架構師 | 
| 建立彙總工具的進階查詢區段。 | 若要從多個來源擷取漂移堆疊，請建立下列查詢：<pre>SELECT resourceId, configuration.driftInformation.stackDriftStatus WHERE resourceType = 'AWS::CloudFormation::Stack'  AND configuration.driftInformation.stackDriftStatus IN ('DRIFTED')</pre> | 雲端架構師、開發人員 | 
| 自動化執行查詢並發佈。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization.html) | 雲端架構師、開發人員 | 
| 建立 CloudWatch 規則。 | 建立排程型 CloudWatch 規則來呼叫負責提醒的 Lambda 函數。 | 雲端架構師 | 

## 相關資源
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization-resources"></a>

**資源**
+ [什麼是 AWS Config？](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html)
+ [多帳戶多區域資料彙總](https://docs.aws.amazon.com/config/latest/developerguide/aggregate-data.html)
+ [偵測堆疊和資源的未受管組態變更](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-drift.html)
+ [IAM：將 IAM 角色傳遞至特定 AWS 服務](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_iam-passrole-service.html)
+ [什麼是 Amazon SNS？](https://docs.aws.amazon.com/sns/latest/dg/welcome.html)

## 其他資訊
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization-additional"></a>

**考量**

我們建議使用此模式中顯示的解決方案，而不是使用涉及特定間隔 API 呼叫的自訂解決方案，來在每個 CloudFormation 堆疊或堆疊集上啟動偏離偵測。以特定間隔使用 API 呼叫的自訂解決方案可能會導致大量的 API 呼叫並影響效能。由於 API 呼叫的數量，可能會發生限流。如果僅根據排程識別資源變更，則另一個潛在問題是偵測延遲。

由於堆疊集是由堆疊組成，因此您可以使用此解決方案。堆疊執行個體詳細資訊也可作為解決方案的一部分使用。

## 附件
<a name="attachments-735d0987-b953-47f8-a9bc-b02a88957ee5"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/735d0987-b953-47f8-a9bc-b02a88957ee5/attachments/attachment.zip)