

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在多区域、多账户组织中设置 CloudFormation 偏差检测
<a name="set-up-aws-cloudformation-drift-detection-in-a-multi-region-multi-account-organization"></a>

*Ram Kandaswamy，Amazon Web Services*

## Summary
<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 这两种输出格式。

## 架构
<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_cn/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 Notiﬁcation 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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建聚合器。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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`。输入具有堆栈偏差检测权限的 IAM 角色 Amazon 资源名称（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_cn/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)