

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

# 使用 Troposphere 生成包含 AWS Config 托管规则的 AWS CloudFormation 模板
<a name="generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere"></a>

*Lucas Nation 和 Freddie Wilson，Amazon Web Services*

## Summary
<a name="generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere-summary"></a>

许多组织使用 [AWS Config 托管](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_use-managed-rules.html)规则来评估其 Amazon Web Services (AWS) 资源是否符合常见的最佳实践。但是，维护这些规则可能很耗时，这种模式可以帮助您利用 Python 库 [Troposphere](https://troposphere.readthedocs.io/en/latest/quick_start.html) 生成和管理 AWS Config 托管规则。

该模式使用 Python 脚本将包含 AWS 托管规则的 Microsoft Excel 电子表格转换为 AWS CloudFormation 模板，从而帮助您管理 AWS Config 托管规则。Troposphere 充当基础设施即代码（IaC），这意味着您可以使用托管规则更新 Excel 电子表格，而不是使用 JSON 或 YAML 格式的文件。然后，您可以使用该模板启动一个 AWS CloudFormation 堆栈，该堆栈在您的 AWS 账户中创建和更新托管规则。

AWS CloudFormation 模板使用 Excel 电子表格定义每个 AWS Config 托管规则，并帮助您避免在 AWS 管理控制台中手动创建单个规则。该脚本将每个托管规则的参数默认为一个空字典，作用域的`ComplianceResourceTypes`默认值为`THE_RULE_IDENTIFIER.template file`*。* 有关规则标识符的更多信息，请参阅 [AWS Config 文档中的使用 AWS CloudFormation 模板创建 AWS Config 托管规则](https://docs.aws.amazon.com/config/latest/developerguide/aws-config-managed-rules-cloudformation-templates.html)。

## 先决条件和限制
<a name="generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere-prereqs"></a>

**先决条件**
+ 一个活跃的 AWS 账户。
+ 熟悉使用 AWS CloudFormation 模板创建 AWS Config 托管规则。有关这方面的更多信息，请参阅 [AWS Config 文档中的使用 AWS CloudFormation 模板创建 AWS Config 托管规则](https://docs.aws.amazon.com/config/latest/developerguide/aws-config-managed-rules-cloudformation-templates.html)。 
+ Python 3，已安装并配置。有关这方面的更多信息，请参阅 [Python 文档](https://www.python.org/)。
+ 现有集成式开发环境（IDE）。 
+ 在示例 `excel_config_rules.xlsx` Excel 电子表格（附后OUs）的列中标识您的组织单位 ()。

## 操作说明
<a name="generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere-epics"></a>

### 自定义和配置 AWS Config 托管规则
<a name="customize-and-configure-the-aws-config-managed-rules"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 更新 Excel 电子表格示例。 | 下载示例 `excel_config_rules.xlsx` Excel 电子表格（附件），并将其标记为要使用的 AWS Config 托管规则 `Implemented`。 <br />标记为的规则`Implemented`将添加到 AWS CloudFormation 模板中。 | 开发者版 | 
| （可选）使用 AWS Config 规则参数更新 config\_rules\_params.json 文件。 | 某些 AWS Config 托管规则需要参数，应使用 `--param-file` 选项将其作为 JSON 文件传递到 Python 脚本。例如，`access-keys-rotated` 托管规则使用以下 `maxAccessKeyAge` 参数：<pre>{<br />         "access-keys-rotated": {<br />             "InputParameters": {<br />                 "maxAccessKeyAge": 90<br />             }<br />         }<br />     }</pre><br />在此示例参数中，`maxAccessKeyAge` 设置为 90 天。该脚本读取参数文件并添加它找到的任何 `InputParameters`。 | 开发者版 | 
| （可选）使用 AWS Config 更新 config\_rules\_params.json 文件。 ComplianceResourceTypes | 默认情况下，Python 脚本会从 AWS 定义的模板中检索 `ComplianceResourceTypes`。如果您想覆盖特定 AWS Config 托管规则范围，则需要使用 `--param-file` 选项将其作为 JSON 文件传递给 Python 脚本。<br />例如，以下示例代码显示了如何将 `ec2-volume-inuse-check` 的 `ComplianceResourceTypes` 设置为 `["AWS::EC2::Volume"]` 列表：<pre>{<br />         "ec2-volume-inuse-check": {<br />             "Scope": {<br />                 "ComplianceResourceTypes": [<br />                     "AWS::EC2::Volume"<br />                 ]<br />             }<br />         }<br />     }</pre> | 开发者版 | 

### 运行 Python 脚本
<a name="run-the-python-script"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 从 requirements.txt 文件处安装 pip 程序包。 | 下载 `requirements.txt` 文件（附件），然后在 IDE 中运行以下命令来安装 Python 程序包：<br />`pip3 install -r requirements.txt` | 开发者版 | 
| 运行 Python 脚本。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere.html)您还可以添加以下可选参数：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere.html) | 开发者版 | 

### 部署 AWS Config 托管规则
<a name="deploy-the-aws-config-managed-rules"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 启动 AWS CloudFormation 堆栈。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/generate-an-aws-cloudformation-template-containing-aws-config-managed-rules-using-troposphere.html) | 开发者版 | 

## 附件
<a name="attachments-07c1cfff-fc9e-4a1f-bd36-48f025808bd8"></a>

要访问与此文档相关联的其他内容，请解压以下文件：[attachment.zip](samples/p-attach/07c1cfff-fc9e-4a1f-bd36-48f025808bd8/attachments/attachment.zip)