

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

# 使用 AFT 自动为新 AWS 账户 用户分配 Amazon VPC IPAM IPv4 CIDR
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft"></a>

*Kien Pham 和 Alex Pazik，Amazon Web Services*

## Summary
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-summary"></a>

此模式展示了如何使用适用于 T [erraform 的 Account Factor AWS 账户 y (AFT) 自动为新AWS Control Tower 用户](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html)分配 Amazon VPC IP 地址管理器 (IPAM) IPv4 CIDR。这是通过账户级自定义来完成的，该自定义使用该模块将 IPAM 中的 IPv4 CIDR 块分配给新的虚拟私有云 (VPC)。`aft-account-customizations`

借助 IPAM，您可以大规模组织、分配、监控和审计 IP 地址，从而轻松规划、跟踪和监控工作负载的 IP 地址。 AWS 您可以[创建 IPAM 和 IPAM](https://docs.aws.amazon.com/vpc/latest/ipam/create-ipam.html) 池，用于在账户出售过程中向新 VPC 分配 IPv4 CIDR 块。

## 先决条件和限制
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-prereqs"></a>

**先决条件**
+ 在支持[AWS 区域](https://docs.aws.amazon.com/controltower/latest/userguide/region-how.html)中 AWS Control Tower 启用 AWS 账户 且已部署 AFT 的情况下处于活动状态
+ 支持的[版本控制系统 (VCS) 提供商](https://github.com/aws-ia/terraform-aws-control_tower_account_factory?tab=readme-ov-file#input_vcs_provider) BitBucket，例如 GitHub、和 GitHub Enterprise
+ [安装](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli)了 Terraform 命令行界面（CLI）
+ 一个运行时环境，您可以在其中运行安装 AFT 的 Terraform 模块
+ AWS Command Line Interface (AWS CLI) [已安装](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)并[配置](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-quickstart.html)

**限制**
+ 有些 AWS 服务 并非全部可用 AWS 区域。有关区域可用性，请参阅[按区域划分的AWS 服务](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。有关特定端点，请参阅[服务端点和配额](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然后选择相应服务的链接。

**产品版本**
+ AWS Control Tower l@@ [anding zon](https://docs.aws.amazon.com/controltower/latest/userguide/2022-all.html#version-3.0) e 版本 3.0 或更高版本，低于 4.0 版本
+ [AFT](https://github.com/aws-ia/terraform-aws-control_tower_account_factory) 1.13.0 或更高版本，低于 2.0.0 版
+ Terraform OSS 1.2.0 或更高版本，低于 2.0.0 版
+ [Terraform Prov AWS id](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) er (`terraform-provider-aws`) 版本 5.11.0 或更高版本，早于 6.0.0 版本
+ [Terraform module for IPAM](https://github.com/aws-ia/terraform-aws-ipam)（`aws-ia/ipam/aws`）版本 2.1.0 或更高版本

## 架构
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-architecture"></a>

下图显示了此模式的工作流和组件。

![\[创建 Amazon VPC IPAM IPv4 CIDR 分配的工作流程。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/986cfc7d-058b-4490-9029-6cd1eadd1dd2/images/f90b84dd-0420-460e-ac0f-9f22b4a9fdc4.png)


该工作流包含以下主要任务：

1. **触发器更改** — 对 Terraform 和 IPAM 自定义的更改已提交到存储库并推送。 GitHub 此任务会自动触发 AWS CodeBuild 管道。

1. **自动构建**-在内部 CodeBuild触发多个构建项目 AWS Step Functions。

1. **应用自定义** — Step Functions 与 CodeBuild 之协调以计划和应用 Terraform 的更改。此任务使用 AFT Terraform 模块来协调 IPAM 池 IP 分配给卖方账户。 AWS 

## 工具
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-tools"></a>

**AWS 服务**
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一项完全托管式构建服务，可编译源代码、运行单元测试和生成部署就绪的构件。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可帮助您快速对软件发布过程的不同阶段进行建模和配置，并自动执行持续发布软件变更所需步骤。
+ [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)协调其他几种能力 [AWS 服务](https://docs.aws.amazon.com/controltower/latest/userguide/integrated-services.html)，包括 AWS Organizations AWS Service Catalog、和。 AWS IAM Identity Center它可以帮助您按照规范性最佳实践设置和管理 AWS 多账户环境。
+ [Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) 是一项完全托管式 NoSQL 数据库服务，可提供快速、可预测、可扩展的性能。
+ [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 服务。
+ [AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html)帮助您集中管理已获批准的 IT 服务目录。 AWS最终用户可在遵循组织设定约束的情况下快速部署他们所需已获得批准的 IT 服务。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html)是一项无服务器编排服务，可帮助您组合 AWS Lambda 功能和其他功能 AWS 服务 来构建关键业务应用程序。
+ [Amazon Virtual Private Cloud（亚马逊 VPC）](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html)可帮助您将 AWS 资源启动到您定义的虚拟网络中。该虚拟网络类似于您在数据中心中运行的传统网络，并具有使用 AWS的可扩展基础设施的优势。Amazon VPC IP 地址管理器 (IPAM) 是一项 VPC 功能，可让您更轻松地规划、跟踪和监控工作负载的 IP 地址。 AWS 

**其他工具**
+ [GitHub](https://docs.github.com/)是一个开发者平台，开发人员可以使用它来创建、存储、管理和共享他们的代码。
+ [HashiCorp Terraform](https://www.terraform.io/) 是一款基础设施即代码 (IaC) 工具，可帮助您创建和管理云和本地资源。其中包括计算实例、存储和联网等低级组件；以及 DNS 条目和软件即服务（SaaS）功能等高级组件。
+ [Python](https://www.python.org/) 是一种通用的计算机编程语言。您可以使用它在 [AWS 云](https://aws.amazon.com/developer/language/python/) 上构建应用程序、自动执行任务和开发服务。

**代码存储库**
+ 此模式的代码可在 Account [Factory for Terraform 存储库中找 GitHub AWS Control Tower 到](https://github.com/aws-ia/terraform-aws-control_tower_account_factory)。

## 最佳实践
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-best-practices"></a>

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

## 操作说明
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-epics"></a>

### 设置和配置您的 AWS 环境
<a name="set-up-and-configure-your-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)。 | 云管理员 | 
| 部署 Ac AWS Control Tower count Factory for Terraform (AFT)。 | 在新的专用 AFT 管理账户中设置 AFT。有关更多信息，请参阅 C [配置并在文档中启动 Terraform 的 Ac AWS Control Tower count Factory](https://docs.aws.amazon.com/controltower/latest/userguide/aft-getting-started.html#aft-configure-and-launch)。 AWS Control Tower  | 云管理员 | 
| 完成 AFT 部署后操作。 | AFT 基础设施部署完成后，完成 AWS Control Tower 文档中[部署后步骤中的步骤](https://docs.aws.amazon.com/controltower/latest/userguide/aft-post-deployment.html)。 | 云管理员 | 

### 创建 IPAM
<a name="create-ipam"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 委派 IPAM 管理员。 | 要在您的 AWS 组织中委派 IPAM 管理员帐户，请使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html)或者，您可以使用 AWS CLI 并运行以下命令：<pre>aws ec2 enable-ipam-organization-admin-account \<br />    --delegated-admin-account-id 012345678901</pre>有关更多信息，请参阅 Amazon V [PC 文档中的 IPAM 与 AWS 组织中的账户集成](https://docs.aws.amazon.com/vpc/latest/ipam/enable-integ-ipam.html)，以及命令参考中的 [enable-ipam-organization-admin-acco](https://docs.aws.amazon.com/cli/latest/reference/ec2/enable-ipam-organization-admin-account.html) unt。 AWS CLI  要继续使用 IPAM，您必须登录委托管理员账户。下一步中指定的 SSO 配置文件或 AWS 环境变量必须允许您登录该账户并授予创建 IPAM 顶级池和区域池的权限。 | AWS 管理员 | 
| 创建 IPAM 顶级池和区域池。 | 此模式的 GitHub 存储库包含一个 Terraform 模板，您可以使用该模板来创建 IPAM 顶级池和区域池。然后，您可以使用 AWS Resource Access Manager () 与组织、组织单位 (OU) 或其他资源共享资源池。 AWS 账户AWS RAM使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html)记下创建后输出的资源池 ID。提交账户申请时您将需要此 ID。如果忘记了资源池 ID，可以稍后从 AWS 管理控制台中获取。 确保创建的池 CIDRs 不会与工作区域中的任何其他池重叠。您可以在没有 CIDR 的情况下创建池，但是在为该池预置 CIDR 之前，您将无法使用该池进行分配。您可以随时通过编辑池 CIDRs 来将其添加到池中。 | AWS 管理员 | 

### 集成 IPAM 与 AFT
<a name="integrate-ipam-with-aft"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 开始创建账户自定义。 | 要开始自定义新账户，请在终端上运行以下命令：<pre># Default name for customization repo<br />cd aft-account-customizations # Replace with your actual repo name if different than the default<br />mkdir -p APG-AFT-IPAM/terraform # Replace APG-AFT-IPAM with your desired customization name<br />cd APG-AFT-IPAM/terraform</pre> | DevOps 工程师 | 
| 创建 `aft-providers.jinja` 文件。 | 向 `aft-providers.jinja` 文件中添加动态代码，指定要使用的 Terraform 后端和提供程序。使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程师 | 
| 创建 `backend.jinja` 文件。 | 向 `backend.jinja` 文件中添加动态代码，指定要使用的 Terraform 后端和提供程序。使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程师 | 
| 创建 `main.tf` 文件。 | 创建一个新`main.tf`文件并添加用于定义两个数据源的代码，这些数据源从 AWS Systems Manager (`aws_ssm`) 中检索两个值并创建 VPC。使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程师 | 
| 创建 `variables.tf` 文件。 | 创建一个声明 Terraform 模块所用变量的 `variables.tf` 文件。使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程师 | 
| 创建 `terraform.tfvars` 文件。 | 创建一个定义传递给该 `terraform.tfvars` 文件的变量值的 `main.tf` 文件。使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程师 | 
| 创建 `outputs.tf` 文件。 | 创建一个新`outputs.tf`文件，其中会显示一些值。 CodeBuild使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程师 | 
| 提交自定义。 | 要将新自定义内容提交到账户自定义存储库，请运行以下命令：<pre># Assumes you are still in the /terraform directory<br />cd .. # Skip if you are in the account customization root directory (APG-AFT-IPAM)<br />git add .<br />git commit -m "APG customization"<br />git push origin</pre> | DevOps 工程师 | 
| 应用自定义。 | 向 `account-requests.tf` 文件中添加代码，请求使用新创建的账户自定义项开设新账户。自定义字段在销售账户中创建 Systems Manager 参数，这些参数是使用正确的 IPAM 分配的 CIDR 创建 VPC 所必需的。 IPv4 使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | AWS DevOps | 
| 验证自定义。 | 登录新分配的账户，并验证自定义设置已成功应用。使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.html) | DevOps 工程师 | 

## 问题排查
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
|  由于权限不足，您在创建或管理资源时遇到故障。 |  查看附加到 Step Functions 的 AWS Identity and Access Management (IAM) 角色和策略 CodeBuild，以及部署中涉及的其他服务。确认他们是否具有必要的权限 如果存在权限问题，请调整 IAM 策略，授予所需的访问权限。 | 
|  您将在部署期间达到 AWS 服务 配额。 |  在部署管道之前，请检查诸如亚马逊简单存储服务 (Amazon S3) 存储桶、IAM 角色和函数等资源的 AWS 服务 配额。 AWS Lambda 如有必要，可请求增加到该配额。有关更多信息，请参阅《AWS 一般参考》**中的[AWS 服务 配额](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)。 | 

## 相关资源
<a name="automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft-resources"></a>

**AWS 服务 文档**
+ [AWS Control Tower 用户指南](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)
+ [IPAM 的工作原理](https://docs.aws.amazon.com/vpc/latest/ipam/how-it-works-ipam.html)
+ [ IAM 安全最佳实操](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)
+ [AWS 服务 配额](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)

**其他资源**
+ [Terraform AWS 提供者文档](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)