

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

# AWS 帳戶 使用 AFT 自動化新 的 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*

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

此模式說明如何 AWS 帳戶 使用 [AWS Control Tower Account Factory for Terraform (AFT) 自動化新 的 Amazon VPC IP Address Manager (IPAM)](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html) IPv4 CIDR 配置。這是使用帳戶層級自訂來完成的，該自訂會使用 `aft-account-customizations`模組，將 IPv4 CIDR 區塊從 IPAM 配置到新的虛擬私有雲端 (VPC)。

使用 IPAM，您可以大規模組織、指派、監控和稽核 IP 地址，讓您輕鬆地規劃、追蹤和監控 AWS 工作負載的 IP 地址。您可以[建立 IPAM](https://docs.aws.amazon.com/vpc/latest/ipam/create-ipam.html) 和 IPAM 集區，以在帳戶販賣程序期間用來將 IPv4 CIDR 區塊配置給新的 VPC。

## 先決條件和限制
<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)和部署的 AFT 中 AWS Control Tower 啟用 AWS 帳戶 的作用中
+ 支援的[版本控制系統 (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 登陸區域](https://docs.aws.amazon.com/controltower/latest/userguide/2022-all.html#version-3.0) 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 AWS Provider](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) (`terraform-provider-aws`) 5.11.0 版或更新版本，早於 6.0.0 版
+ [IPAM () 2.1.0 版或更新版本的 Terraform 模組](https://github.com/aws-ia/terraform-aws-ipam) `aws-ia/ipam/aws`

## Architecture
<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_tw/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 模組來協調 AWS 佈建帳戶的 IPAM 集區 IP 指派。

## 工具
<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 Organizations，[AWS 服務](https://docs.aws.amazon.com/controltower/latest/userguide/integrated-services.html)包括 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 (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可協助您在已定義的虛擬網路中啟動 AWS 資源。此虛擬網路與您在自己的資料中心中操作的傳統網路相似，且具備使用 AWS可擴展基礎設施的優勢。Amazon VPC IP Address Manager (IPAM) 是一種 VPC 功能，可讓您更輕鬆地規劃、追蹤和監控 AWS 工作負載的 IP 地址。

**其他工具**
+ [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/)。

**程式碼儲存庫**
+ 此模式的程式碼可在 GitHub [AWS Control Tower Account Factory for Terraform](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 儲存庫之前，執行 lint、驗證和測試。
+ **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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 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)。 | 雲端管理員 | 
| 部署 AWS Control Tower Account Factory for Terraform (AFT)。 | 在新的專用 AFT 管理帳戶中設定 AFT。如需詳細資訊，請參閱 AWS Control Tower 文件中的 [Configure 和啟動 AWS Control Tower Account Factory for Terraform](https://docs.aws.amazon.com/controltower/latest/userguide/aft-getting-started.html#aft-configure-and-launch)。 | 雲端管理員 | 
| 完成 AFT 部署後。 | AFT 基礎設施部署完成後，請完成 AWS Control Tower 文件中的[部署後步驟](https://docs.aws.amazon.com/controltower/latest/userguide/aft-post-deployment.html)。 | 雲端管理員 | 

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


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 委派 IPAM 管理員。 | 若要委派 AWS 組織中的 IPAM 管理員帳戶，請使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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 VPC 文件》中的將 [IPAM 與 AWS 組織中的帳戶整合](https://docs.aws.amazon.com/vpc/latest/ipam/enable-integ-ipam.html)，以及《 AWS CLI 命令參考》中的 [enable-ipam-organization-admin-account](https://docs.aws.amazon.com/cli/latest/reference/ec2/enable-ipam-organization-admin-account.html)。 若要繼續使用 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_tw/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 之前，您將無法使用集區進行配置。您可以隨時編輯集區，將 CIDR 新增至集區。 | AWS 管理員 | 

### 整合 IPAM 與 AFT
<a name="integrate-ipam-with-aft"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 開始建立帳戶自訂。 | 若要開始新的帳戶自訂，請從終端機執行下列命令：<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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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` 檔案。自訂欄位會在使用正確 IPAM 配置的 IPv4 CIDR 建立 VPC 所需的付費帳戶中建立 Systems Manager 參數。使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/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、CodeBuild 和部署所涉及其他服務的 AWS Identity and Access Management (IAM) 角色和政策。確認他們具有必要的許可。如果有許可問題，請調整 IAM 政策以授予必要的存取權。 | 
|  您在部署期間達到 AWS 服務 配額。 |  部署管道之前，請檢查 資源的配額，例如 Amazon Simple Storage Service AWS 服務 (Amazon S3) 儲存貯體、IAM 角色和 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)