

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

# 使用引导管道实施 Account Factory for Terraform（AFT）
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline"></a>

*Vinicius Elias 和 Edgar Costa Filho，Amazon Web Services*

## Summary
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-summary"></a>

此模式为从的管理 AWS Control Tower 账户部署 Account Factory for Terraform (AFT) 提供了一种简单而安全的方法。 AWS Organizations该解决方案的核心是一个 CloudFormation 模板，该模板通过创建Terraform管道来自动执行AFT配置，该管道的结构易于适应初始部署或后续更新。

安全和数据完整性是重中之重 AWS，因此，Terraform状态文件是跟踪托管基础设施和配置状态的关键组件，可以安全地存储在亚马逊简单存储服务 (Amazon S3) Simple Service 存储桶中。此存储桶配置了多种安全措施，包括服务器端加密和阻止公共访问的策略，可帮助确保您的 Terraform 状态免遭未经授权的访问和数据泄露。

管理账户负责协调和监督整个环境，因此它是环境中的关键资源。 AWS Control Tower这种模式遵循 AWS 最佳实践，确保部署过程不仅高效，而且符合安全和治理标准，从而为在您的 AWS 环境中部署 AFT 提供了一种全面、安全和高效的方式。

有关 AFT 的更多信息，请参阅 [AWS Control Tower 文档](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html)。

## 先决条件和限制
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-prereqs"></a>

**先决条件**
+ 基本的 AWS 多账户环境，至少包含以下账户：管理账户、日志存档账户、审计账户，以及一个用于 AFT 管理的额外账户。
+ 成熟的 AWS Control Tower 环境。应正确配置管理帐户，因为 CloudFormation 模板将在其中部署。
+  AWS 管理账户中的必要权限。您需要足够的权限才能创建和管理资源，例如 S3 存储桶、 AWS Lambda 函数、 AWS Identity and Access Management (IAM) 角色和 AWS CodePipeline 项目。
+ 熟悉 Terraform。了解 Terraform 的核心概念和工作流非常重要，因为部署涉及生成和管理 Terraform 配置。

**限制**
+ 务必注意您账户中的 [AWS 资源配额](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)。部署可能会创建多个资源，而遇到服务配额问题可能会阻碍部署过程。
+ 该模板专为特定版本的 Terraform 和 AWS 服务而设计。升级或更改版本可能需要进行模板修改。
+ 该模板不支持自我管理的版本控制系统 (VCS) 服务，例如 GitHub 企业版。

**产品版本**
+ Terraform 版本 1.6.6 或更高版本
+ AFT 版本 1.11 或更高版本

## 架构
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-architecture"></a>

**目标技术堆栈**
+ CloudFormation
+ AWS CodeBuild
+ AWS CodeCommit
+ AWS CodePipeline
+ Amazon EventBridge
+ IAM
+ AWS Lambda
+ Amazon S3

**目标架构**

下图阐明了此模式中讨论的实施。

![\[使用引导管道实施 AFT 的工作流。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/944f9912-87c7-4cc5-8478-7070cf67f7ee/images/4ee74757-940d-4d92-a7f0-fb0db1476247.png)


该工作流包括三个主要任务：创建资源、生成内容和运行管道。

*创建资源*

[此模式提供的CloudFormation 模板](https://github.com/aws-samples/aft-bootstrap-pipeline/blob/main/code/aft-deployment-pipeline.yaml)将创建和设置所有必需的资源，具体取决于您在部署模板时选择的参数。该模板至少会创建以下资源：
+ 用于实现 AFT 的 CodePipeline 管道
+ 一个 S3 存储桶，用于存储与 AFT 实施关联的 Terraform 状态文件
+ 两个 CodeBuild 项目用于实施 Terraform 计划并在管道的不同阶段应用命令
+  CodeBuild 和 CodePipeline 服务的 IAM 角色
+ 第二个 S3 存储桶，用于存储管道运行时构件

根据您选择的 VCS 提供商（CodeCommit 或外部 VCS），模板会创建以下资源。
+ 对于 **CodeCommit**：
  + 用于 CodeCommit 存储 AFT Terraform 引导程序代码的存储库
  + 捕获`main`分支上 CodeCommit 仓库变更的 EventBridge 规则
  + 该 EventBridge 规则的另一个 IAM 角色
+ 对于任何其他**外部 VCS 提供商**，例如 GitHub：
  + 一个 AWS CodeConnections 连接

此外，当您选择 CodeCommit 作为 VCS 提供者时，如果将`Generate AFT Files`参数设置为`true`，则模板会创建以下额外资源来生成内容：
+ 一个 S3 存储桶，用于存储生成的内容并用作 CodeCommit 存储库的来源
+ 一个 Lambda 函数，用于处理指定参数及生成相应内容
+ 一个 IAM 函数，用于运行 Lambda 函数
+ 部署模板时运行 Lambda 函数的 CloudFormation 自定义资源

*生成内容*

为生成 AFT 引导文件及其内容，该解决方案使用了 Lambda 函数和 S3 存储桶。该函数会在存储桶中创建一个文件夹，然后在该文件夹中创建两个文件：`main.tf` 和 `backend.tf`。该函数还处理提供的 CloudFormation 参数，并使用预定义的代码填充这些文件，替换相应的参数值。

要查看用作生成文件的模板的代码，请参阅解决方案的[GitHub 存储库](https://github.com/aws-samples/aft-bootstrap-pipeline)。基本上，文件会按如下方式生成。

**main.tf**

```
module "aft" {
  source = "github.com/aws-ia/terraform-aws-control_tower_account_factory?ref=<aft_version>"

  # Required variables
  ct_management_account_id  = "<ct_management_account_id>"
  log_archive_account_id    = "<log_archive_account_id>"
  audit_account_id          = "<audit_account_id>"
  aft_management_account_id = "<aft_management_account_id>"
  ct_home_region            = "<ct_home_region>"

  # Optional variables
  tf_backend_secondary_region = "<tf_backend_secondary_region>"
  aft_metrics_reporting       = "<false|true>"

  # AFT Feature flags
  aft_feature_cloudtrail_data_events      = "<false|true>"
  aft_feature_enterprise_support          = "<false|true>"
  aft_feature_delete_default_vpcs_enabled = "<false|true>"

  # Terraform variables
  terraform_version      = "<terraform_version>"
  terraform_distribution = "<terraform_distribution>"

  # VCS variables (if you have chosen an external VCS)
  vcs_provider                                  = "<github|githubenterprise|gitlab|gitlabselfmanaged|bitbucket>"
  account_request_repo_name                     = "<org-name>/aft-account-request"
  account_customizations_repo_name              = "<org-name>/aft-account-customizations"
  account_provisioning_customizations_repo_name = "<org-name>/aft-account-provisioning-customizations"
  global_customizations_repo_name               = "<org-name>/aft-global-customizations"

}
```

**backend.tf**

```
terraform {
  backend "s3" {
    region = "<aft-main-region>"
    bucket = "<s3-bucket-name>"
    key    = "aft-setup.tfstate"
  }
}
```

在创建 CodeCommit 存储库期间，如果您将`Generate AFT Files`参数设置为`true`，则模板将使用包含生成内容的 S3 存储桶作为`main`分支的来源，自动填充存储库。

*运行管道*

创建资源并配置引导文件后，即可运行管道。第一阶段（*源*）会从存储库的主分支获取源代码，第二阶段（*构建*）则会运行 Terraform 计划命令并生成要审查的结果。在第三阶段（*审批*）中，管道会等待批准或拒绝最后一个阶段（*部署*）的手动操作。在最后一个阶段，管道会使用前一个 Terraform `plan` 命令的结果作为输入来运行 Terraform `apply` 命令。最后，可利用跨账户角色和管理账户中的权限，在 AFT 管理账户中创建 AFT 资源。

**注意**  
如果您选择外部 VCS 提供商，则需要使用您的 VCS 提供商凭证来授权连接。要完成设置，请按照 AWS 开发者工具控制台文档中[更新待处理连接](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-update.html)中的步骤进行操作。

## 工具
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-tools"></a>

**AWS 服务**
+ [CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)帮助您设置 AWS 资源，快速一致地配置这些资源，并在各个区域的整个生命周期中 AWS 账户 对其进行管理。
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一项完全托管式构建服务，可编译源代码、运行单元测试和生成部署就绪的构件。 
+ [AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html) 是一项版本控制服务，可帮助您私下存储和管理 Git 存储库，而无需管理自己的源代码控制系统。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可帮助您快速对软件发布过程的不同阶段进行建模和配置，并自动执行持续发布软件变更所需步骤。
+ [AWS CodeConnections](https://docs.aws.amazon.com/dtconsole/latest/userguide/welcome-connections.html)使 AWS 资源和服务（例如 CodePipeline）能够连接到外部代码存储库，例如 GitHub。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项无服务器计算服务，可运行代码来响应事件并自动管理计算资源，从而提供一种快速创建现代、无服务器生产应用程序的方法。
+ [适用于 Python (Boto3) 的 AWS SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html)是一个软件开发套件，可帮助您将 Python 应用程序、库或脚本与集成 AWS 服务。

**其他工具**
+ [Terraform](https://developer.hashicorp.com/terraform?product_intent=terraform) 是一种基础设施即代码（IaC）工具，可让您安全高效地构建、更改基础设施并对基础设施进行版本控制。其中包括计算实例、存储和联网等低级组件；以及 DNS 条目和 SaaS 功能等高级组件。
+ [Python](https://docs.python.org/3.9/tutorial/index.html) 是一种简单易学且功能强大的编程语言。它具有高效的高级数据结构，并且为面向对象的编程提供了一种简单但有效的方法。

**代码存储库**

此模式的代码可在 GitHub [AFT bootstrap 管道存储库](https://github.com/aws-samples/aft-bootstrap-pipeline)中找到。

有关官方的 AFT 存储库，请参阅中的 T [erraform 的 Ac AWS Control Tower count Factory](https://github.com/aws-ia/terraform-aws-control_tower_account_factory/tree/main)。 GitHub

## 最佳实践
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-best-practices"></a>

使用提供的 CloudFormation 模板部署 AFT 时，我们建议您遵循最佳实践，以帮助确保安全、高效和成功的实施。实施和操作 AFT 的主要指导原则和建议包括以下内容。
+ **全面审查参数**：仔细检查并理解 CloudFormation 模板中的每个参数。准确的参数配置对于 AFT 的正确设置和运行至关重要。
+ **定期更新模板**：使用最新 AWS 功能和 Terraform 版本更新模板。定期更新可帮助您充分利用新功能并维护安全性。
+ **版本控制**：如果可能，请保持 AFT 模块版本不变，并使用单独的 AFT 部署进行测试。
+ **范围**：仅使用 AFT 来部署基础设施护栏和自定义。切勿使用它来部署应用程序。
+ **检查和验证**：AFT 管道需要经过检查和验证的 Terraform 配置。在将配置推送到 AFT 存储库之前，运行检查、验证和测试。
+ **Terraform 模块**：将可重复使用的 Terraform 代码构建为模块，并始终指定 Terraform 和 AWS 提供者版本以满足组织的要求。

## 操作说明
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-epics"></a>

### 设置和配置 AWS 环境
<a name="set-up-and-configure-the-aws-environment"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 准备 AWS Control Tower 环境。 | 在您的环境 AWS Control Tower 中进行设置和配置，以确保对您的 AWS 环境进行集中管理和治理 AWS 账户。有关更多信息，请参阅 AWS Control Tower 文档 AWS Control Tower中的[入门](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-with-control-tower.html)。 | 云管理员 | 
| 启动 AFT 管理账户。 | 使用 Ac AWS Control Tower count Factory 启动一个新的 AWS 账户 账户作为你的 AFT 管理账户。有关更多信息，请参阅 AWS Control Tower 文档中的使用 A [ccount AWS Service Catalog Factory 配置账户](https://docs.aws.amazon.com/controltower/latest/userguide/provision-as-end-user.html)。 | 云管理员 | 

### 在管理账户中部署 CloudFormation 模板
<a name="deploy-the-cfnshort-template-in-the-management-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 启动 CloudFormation 模板。 | 在这篇长篇故事中，您将部署此解决方案随附的 CloudFormation 模板，在您的 AWS 管理账户中设置 AFT 引导程序管道。该管道会将 AFT 解决方案部署在您在上一篇操作说明中设置的 AFT 管理账户中。**步骤 1：打开 CloudFormation 控制台**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 2：创建新堆栈**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 3：配置堆栈参数**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 4：决定文件生成**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**第 5 步：填写 AWS Control Tower 并且 AFT 账户详细信息**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 6：配置 AFT 选项**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 7：指定版本**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 8：审查并创建堆栈**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 9：监控堆栈创建**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 10：验证部署**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html) | 云管理员 | 

### 填充并验证 AFT 引导存储库和管道
<a name="populate-and-validate-the-aft-bootstrap-repository-and-pipeline"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 选项 1：为外部 VCS 填充 AFT 引导存储库。 | 如果您将 VCS 提供程序设置为外部 VCS（不是 CodeCommit），请按照以下步骤操作。（可选）部署 CloudFormation 模板后，您可以填充或验证新创建的 AFT bootstrap 存储库中的内容，并测试管道是否已成功运行。**步骤 1：更新连接**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 2：填充存储库**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 2：提交并推送您的更改**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html) | 云管理员 | 
| 选项 2：填充的 AFT 引导存储库。 CodeCommit | 如果您将 VCS 提供程序设置为 CodeCommit，请按照以下步骤操作。（可选）部署 CloudFormation 模板后，您可以填充或验证新创建的 AFT bootstrap 存储库中的内容，并测试管道是否已成功运行。如果将 `Generate AFT Files` 参数设置为 `true`，请跳至下一个故事（验证管道）。**步骤 1：填充存储库**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 2：提交并推送您的更改**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html) | 云管理员 | 
| 验证 AFT 引导管道。 | **步骤 1：查看管道**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 2：审批 Terraform 计划结果**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 3：等待部署**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html)**步骤 4：检查已创建的资源**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline.html) | 云管理员 | 

## 故障排除
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
|  CloudFormation 模板中包含的自定义 Lambda 函数在部署期间失败。 | 查看 Amazon CloudWatch 日志中是否有 Lambda 函数以识别错误。这些日志提供了详细信息，可以帮助查明具体问题。确认 Lambda 函数具有必要的权限并且环境变量已正确设置。 | 
| 由于权限不足，您在创建或管理资源时遇到故障。 | 查看附加到 Lambda 函数的 IAM 角色和策略 CodeBuild，以及部署中涉及的其他服务。确认他们是否具有必要的权限 如果存在权限问题，请调整 IAM 策略，授予所需的访问权限。 | 
| 您使用的 CloudFormation 模板版本已过时，且版本较新 AWS 服务 或 Terraform。 | 定期更新 CloudFormation 模板以使其与最新版本 AWS 和 Terraform 版本兼容。请查看发布说明或文档，了解任何版本特定更改或要求。 | 
| 您将在部署期间达到 AWS 服务 配额。 | 在部署管道之前，请检查 S3 存储桶、IAM 角色和 Lambda 函数等资源的 AWS 服务 配额。如果需要，可以请求增加配额。有关更多信息，请参阅 AWS 网站上的[AWS 服务 配额](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)。 | 
| 由于 CloudFormation 模板中的输入参数不正确，您会遇到错误。 | 仔细检查所有输入参数是否有拼写错误或值不正确。确认资源标识符（例如账户 IDs 和区域名称）准确无误。 | 

## 相关资源
<a name="implement-account-factory-for-terraform-aft-by-using-a-bootstrap-pipeline-resources"></a>

要成功实施此模式，请查看以下资源。这些资源提供了额外的信息和指引，因此对于通过使用 CloudFormation来设置和管理 AFT 而言，这些信息和指引可能非常重要。

**AWS****文档：**
+ [AWS Control Tower 《用户指南》](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)提供了有关设置和管理的详细信息 AWS Control Tower。
+ [CloudFormation 文档](https://docs.aws.amazon.com/cloudformation/index.html)提供了对 CloudFormation 模板、堆栈和资源管理的见解。

**IAM 策略和最佳实践：**
+ [IAM 中的安全最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)解释了如何使用 IAM 角色和策略来帮助保护 AWS 资源。

**Terraform 开启： AWS**
+ [Terraform P AWS rovider 文档](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)提供了有关将 Terraform 与配合使用的全面信息。 AWS

**AWS 服务 配额：**
+ AWS 服务 q@@ [u](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html) otas 提供有关如何查看 AWS 服务 配额以及如何请求增加配额的信息。