

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

# 雲端基礎
<a name="cloudfoundations-pattern-list"></a>

**Topics**
+ [在 上使用登陸區域加速器自動建立帳戶 AWS](automate-account-creation-lza.md)
+ [跨多個帳戶和區域自動清查 AWS 資源](automate-aws-resource-inventory.md)
+ [建置包含 MongoDB Atlas 的 AWS 登陸區域](build-aws-landing-zone-that-includes-mongodb-atlas.md)
+ [設定 VPC 流程日誌以進行跨 的集中化 AWS 帳戶](configure-vpc-flow-logs-for-centralization-across-aws-accounts.md)
+ [使用 Terraform 動態管理 AWS 許可集](manage-aws-permission-sets-dynamically-by-using-terraform.md)
+ [使用 AWS Organizations 自動標記 Transit Gateway 連接](tag-transit-gateway-attachments-automatically-using-aws-organizations.md)
+ [更多模式](cloudfoundations-more-patterns-pattern-list.md)

# 在 上使用登陸區域加速器自動建立帳戶 AWS
<a name="automate-account-creation-lza"></a>

*Justin Kuskowski、Joe Behrens 和 Nathan Scott，Amazon Web Services*

## 總結
<a name="automate-account-creation-lza-summary"></a>

此模式說明如何在 解決方案[上使用登陸區域加速器 AWS](https://aws.amazon.com/solutions/implementations/landing-zone-accelerator-on-aws/)，在授權使用者提交請求 AWS 帳戶 時自動部署新的 。它使用 AWS Step Functions 來協調許多 AWS Lambda 函數。Lambda 函數會將帳戶資訊新增至 Git 儲存庫、啟動 AWS CodePipeline 管道，並驗證是否已佈建必要的 AWS 資源。程序完成時，使用者會收到帳戶已建立的通知。

或者，您可以整合 Microsoft Entra ID 群組，並在帳戶建立程序期間指派 AWS IAM Identity Center 許可集。如果您的組織使用 Microsoft Entra ID 做為身分來源，此選用功能可協助您自動管理和設定新帳戶的存取權。

## 先決條件和限制
<a name="automate-account-creation-lza-prereqs"></a>

**先決條件**
+ 在 中存取管理帳戶 AWS Organizations
+ AWS Cloud Development Kit (AWS CDK) 2.118.0 版或更新版本，[已安裝](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_install)並[設定](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_configure)
+ Python 3.9 版或更新版本，[已安裝](https://www.python.org/downloads/)
+ AWS Command Line Interface (AWS CLI) 2.13.19 版或更新版本，[已安裝](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
+ Docker 24.0.6 版或更新版本，[已安裝](https://docs.docker.com/get-started/get-docker/)
+  AWS 解決方案上的登陸區域加速器，[部署](https://docs.aws.amazon.com/solutions/latest/landing-zone-accelerator-on-aws/deploy-the-solution.html)在管理帳戶中
+ （選用） Microsoft Entra ID 和 IAM Identity Center，[整合](https://docs.aws.amazon.com/singlesignon/latest/userguide/idp-microsoft-entra.html)

**限制**

帳戶建立工作流程支援循序執行來部署單一 AWS 帳戶。此限制可確保帳戶建立工作流程已成功完成，而無需在平行執行期間競爭資源。

## Architecture
<a name="automate-account-creation-lza-architecture"></a>

**目標架構**

下圖顯示 AWS 帳戶 使用 Landing Zone Accelerator on. AWS AWS Step Functions orchestrates 自動化自動化建立新 的高階架構。Step Functions 工作流程中的每個任務都由一或多個 AWS Lambda 函數執行。

![\[使用 AWS 上的登陸區域加速器自動化建立新帳戶的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/d31abfaa-6854-4923-b896-3b817de9f4d9/images/dfd6503d-a4ed-43df-82d4-082f8153d473.png)


該圖顯示以下工作流程：

1. 使用者透過執行 Python 指令碼或使用 Amazon API Gateway 來請求帳戶。

1. Account Creation Orchestrator 工作流程從 開始 AWS Step Functions。

1. 工作流程會更新來源碼儲存庫中的 `account-config.yaml` 檔案。它也會啟動 AWS 管道上的登陸區域加速器，並檢查管道的狀態。此管道會建立和設定新帳戶。如需此功能運作方式的詳細資訊，請參閱 上登陸區域加速器的[架構概觀](https://docs.aws.amazon.com/solutions/latest/landing-zone-accelerator-on-aws/architecture-overview.html) AWS。

1. （選用） 當管道完成時，工作流程會檢查群組是否存在於 Microsoft Entra ID 中。如果群組不存在於 Microsoft Entra ID 中，工作流程會將群組新增至 Microsoft Entra ID。

1. 工作流程會執行登陸區域加速器在 AWS 解決方案上無法執行的其他步驟。預設步驟包括：
   + 在 AWS Identity and Access Management (IAM) 中建立[帳戶別名](https://docs.aws.amazon.com/IAM/latest/UserGuide/console-account-alias.html) 
   + 在 中將[標籤](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_tagging.html)連接至帳戶 AWS Organizations
   + 根據指派給帳戶的標籤在[AWS Systems Manager 參數存放](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)區中建立參數

1. （選用） 工作流程會將一或多個[許可集](https://docs.aws.amazon.com/singlesignon/latest/userguide/permissionsetsconcept.html)指派給您先前指定的 Microsoft Entra ID 群組。許可集允許群組中的使用者存取新帳戶，並允許他們執行您設定的動作。

1.  AWS Lambda 函數會執行 QA 和驗證測試。它會驗證資源建立、檢查標籤是否已建立，並驗證是否已部署安全資源。

1. 工作流程會釋出帳戶，並使用 Amazon Simple Email Service (Amazon SES) 通知使用者程序已成功完成。

如需 Step Functions 工作流程的詳細資訊，請參閱此模式[額外資訊](#automate-account-creation-lza-additional)區段中的 *Step Functions 工作流程圖表*。

**Microsoft Entra ID 應用程式**

如果您選擇與 Microsoft Entra ID 整合，請在部署此模式時建立下列兩個應用程式：
+ 連結至 IAM Identity Center 的應用程式，並確保 Microsoft Entra ID 群組可在 IAM Identity Center 中使用。在此範例中，此 Microsoft Entra ID 應用程式名為 `LZA2`。
+ 允許 Lambda 函數與 Microsoft Entra ID 通訊並呼叫 [Microsoft Graph APIs](https://learn.microsoft.com/en-us/graph/identity-network-access-overview)的應用程式。在此模式中，此應用程式名為 `create_aws_account`。

這些應用程式會收集用來同步 Microsoft Entra ID 群組和指派許可集的資料。

## 工具
<a name="automate-account-creation-lza-tools"></a>

**AWS 服務**
+ [Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) 可協助您建立、發佈、維護、監控和保護任何規模的 REST、HTTP 和 WebSocket APIs。在此模式中，您可以使用 API Gateway 來檢查 AWS 帳戶 名稱的可用性、啟動 AWS Step Functions 工作流程，以及檢查 Step Functions 執行的狀態。
+ [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/v2/guide/home.html) 是一種軟體開發架構，可協助您在程式碼中定義和佈建 AWS 雲端 基礎設施。
+ [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html) 可協助您設定和管理 AWS 多帳戶環境，並遵循規範最佳實務。
+ [Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html) 是一種無伺服器事件匯流排服務，可協助您將應用程式與來自各種來源的即時資料連線。例如， AWS Lambda 函數、使用 API 目的地的 HTTP 呼叫端點，或其他事件匯流排 AWS 帳戶。如果 Step Functions 工作流程狀態變更為 `Failed`、 或 `Timed-out`，此解決方案會使用 [EventBridge 規則](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rules.html)來啟動 Lambda 函數`Aborted`。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html) 可協助您集中管理所有 AWS 帳戶 和雲端應用程式的單一登入 (SSO) 存取。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可協助您建立和控制密碼編譯金鑰，以協助保護您的資料。在此模式中， AWS KMS 金鑰用於加密資料，例如存放在 Amazon Simple Storage Service (Amazon S3) 中的資料、Lambda 環境變數，以及 Step Functions 中的資料。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可協助您將多個 合併 AWS 帳戶 到您建立並集中管理的組織。
+ [Amazon Simple Email Service (Amazon SES)](https://docs.aws.amazon.com/ses/latest/dg/Welcome.html) 可協助您使用自己的電子郵件地址和網域來傳送和接收電子郵件。成功建立新帳戶後，您會透過 Amazon SES 收到通知。
+ [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可協助您協調和管理發佈者和用戶端之間的訊息交換，包括 Web 伺服器和電子郵件地址。如果在帳戶建立過程中發生錯誤，Amazon SNS 會傳送通知到您設定的電子郵件地址。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html) 是一種無伺服器協同運作服務，可協助您結合 AWS Lambda 函數和其他 AWS 服務 來建置業務關鍵型應用程式。
+ [AWS Systems Manager 參數存放區](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)為組態資料管理和秘密管理提供安全的階層式儲存。

**其他工具**
+ [awscurl](https://pypi.org/project/awscurl/0.6/) 會將簽署 AWS API 請求的程序自動化，並協助您以標準 curl 命令的形式提出請求。
+ [Microsoft Entra ID](https://learn.microsoft.com/en-us/entra/fundamentals/whatis)，先前稱為 *Azure Active Directory*，是一種雲端型身分和存取管理服務。
+ [Microsoft Graph APIs](https://learn.microsoft.com/en-us/graph/graph-explorer/graph-explorer-overview)可協助您存取 Microsoft 雲端服務中的資料和智慧，例如 Microsoft Entra 和 Microsoft 365。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [lza-account-creation-workflow](https://github.com/aws-samples/lza-account-creation-workflow) 儲存庫中使用。

[lambda\$1layer](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_layer) 目錄包含下列層，在多個 Lambda 函數中參考：
+ [account\$1creation\$1helper](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_layer/account_creation_helper) – 此 layer 包含擔任角色和檢查進度的模組 AWS Service Catalog。
+ [boto3](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_layer/boto3) – 此 layer 包含 [適用於 Python (Boto3) 的 AWS SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html)模組，以確保 AWS Lambda 具有最新版本。
+ [identity\$1center\$1helper](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_layer/identity_center_helper) – 此層支援呼叫 IAM Identity Center。

[lambda\$1src](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src) 目錄包含下列 Lambda 函數：
+ [AccountTagToSsmParameter](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/event/AccountTagToSsmParameter) – 此函數會使用連接至帳戶的標籤， AWS Organizations 以在參數存放區中建立參數。每個參數都以 `/account/tags/`字首開頭。
+ [AttachPermissionSet](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/AttachPermissionSet) – 此函數會將許可集新增至 IAM Identity Center 群組。
+ [AzureADGroupSync](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/AzureADGroupSync) – 此函數會將目標 Microsoft Entra ID 群組同步至 IAM Identity Center。
+ [CheckForRunningProcesses](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/CheckForRunningProcesses) – 此函數會檢查`AWSAccelerator-Pipeline`管道目前是否正在執行。如果管道正在執行，則函數會延遲 AWS Step Functions 工作流程。
+ [CreateAccount](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/CreateAccount) – 此函數使用 AWS Service Catalog 和 AWS Control Tower 建立新的 AWS 帳戶。
+ [CreateAdditionalResources](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/CreateAdditionalResources) – 此函數會建立非由 Landing Zone Accelerator 或 管理 AWS 的資源 AWS CloudFormation，例如帳戶別名和 AWS Service Catalog 標籤。
+ [GetAccountStatus](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/GetAccountStatus) – 此函數會掃描 中佈建的產品 AWS Service Catalog ，以判斷帳戶建立程序是否已完成。
+ [GetExecutionStatus](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/api/GetExecutionStatus) – 此函數會擷取執行中或已完成 AWS Step Functions 執行的狀態。
+ [NameAvailability](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/api/NameAvailability) – 此函數會檢查 AWS 帳戶 名稱是否已存在於其中 AWS Organizations。
+ [ReturnResponse](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/ReturnResponse) – 如果帳戶建立成功，此函數會傳回新帳戶的 ID。如果帳戶建立失敗，則會傳回錯誤訊息。
+ [RunStepFunction](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/api/RunStepFunction) – 此函數會執行建立帳戶的 AWS Step Functions 工作流程。
+ [SendEmailWithSES](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/SendEmailWithSES) – 此函數會傳送電子郵件給等待帳戶建立完成的使用者。
+ [ValidateADGroupSyncToSSO](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/ValidateAdGroupSyncToSSO) – 此函數會檢查指定的 Microsoft Entra ID 群組是否與 IAM Identity Center 同步。
+ [ValidateResources](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/ValidateResources) – 此函數會驗證所有 AWS Control Tower 自訂是否已成功執行。

## 最佳實務
<a name="automate-account-creation-lza-best-practices"></a>

我們建議採用下列命名慣例 AWS CDK： 
+ 使用`p`字首啟動所有參數。
+ 使用`c`字首啟動所有條件。
+ 使用 `r`字首啟動所有資源。
+ 使用 `o`字首啟動所有輸出。

## 史詩
<a name="automate-account-creation-lza-epics"></a>

### 部署用於驗證和標記的 IAM 角色
<a name="deploy-the-iam-roles-for-validation-and-tagging"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在 上準備登陸區域加速器 AWS 以進行自訂。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 
| 準備部署`lza-account-creation-validation`角色。 | 現在，您可以自訂解決方案，在管理帳戶以外的所有帳戶中部署 `lza-account-creation-validation` IAM 角色。此角色提供 `ValidateResources` Lambda 函數對新帳戶的唯讀存取權。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 
| 準備部署`account-tagging-to-ssm-parameter-role`角色。 | 現在，您可以自訂解決方案，在管理帳戶以外的所有帳戶中部署 `account-tagging-to-ssm-parameter-role` IAM 角色。此角色用於在 AWS Systems Manager 參數存放區中建立參數。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 
| 準備部署`config-log-validation-role`角色。 | 現在，您可以自訂解決方案，在日誌封存帳戶中部署 `config-log-validation-role` IAM 角色。此角色允許 `ValidateResources` Lambda 函數存取用於記錄和存取 AWS Config 規則的 Amazon S3 儲存貯體。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 

### （選用） 從 Microsoft Entra ID 取得資料
<a name="optional-get-data-from-microsoft-entra-id"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立允許 Lambda 函數與 Microsoft Entra ID 通訊的應用程式。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | Microsoft Entra ID | 
| 擷取`create_aws_account`應用程式的值。 | 現在，您可以擷取`create_aws_account`應用程式所需的值。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | Microsoft Entra ID | 
| 建立將 Microsoft Entra ID 與 IAM Identity Center 整合的應用程式。 | 在 Microsoft Entra ID 管理中心，註冊`LZA2`應用程式。如需說明，請參閱 Microsoft 文件中的[註冊應用程式](https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app)。 | Microsoft Entra ID | 
| 擷取`LZA2`應用程式的值。 | 現在，您可以擷取`LZA2`應用程式所需的值。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | Microsoft Entra ID | 
| 建立秘密。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 

### 部署解決方案
<a name="deploy-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製原始程式碼。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | DevOps 工程師 | 
| 更新 `deploy-config.yaml` 檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 
| 在您的 AWS 環境中部署解決方案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html)此解決方案使用 Amazon S3 儲存貯體來存放此解決方案的原始程式碼。您可以使用 [upload\$1to\$1source\$1bucket.py](https://github.com/aws-samples/gen-ai-trivia/blob/main/scripts/upload_to_source_bucket.py) 指令碼來建立原始碼的封存，並上傳更新的版本。 | AWS DevOps | 

### 選項 1 – 使用 Python 建立帳戶
<a name="option-1-create-an-account-using-python"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 識別要使用的引數。 | 選擇當您執行啟動 Step Functions 工作流程的 Python 指令碼時要使用的引數。如需引數的完整清單，請參閱此模式的其他資訊[其他資訊](#automate-account-creation-lza-additional)一節。 | AWS DevOps、Python | 
| 啟動 Python 指令碼。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | DevOps 工程師，Python | 

### 選項 2 – 使用 API Gateway 和 awscurl 建立帳戶
<a name="option-2-create-an-account-using-api-gateway-and-awscurl"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 awscurl 的變數。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 
| 檢查名稱可用性。 | 輸入下列命令來驗證名稱是否可供 使用 AWS 帳戶。`<AWS_ACCOUNT_NAME>` 將 取代為目標帳戶的名稱：<pre>awscurl --service execute-api \<br />    --region ${AWS_REGION} \<br />    --access_key ${AWS_ACCESS_KEY_ID} \<br />    --secret_key ${AWS_SECRET_ACCESS_KEY} \<br />    --security_token ${AWS_SESSION_TOKEN} \<br />    -X POST ${API_GATEWAY_ENDPOINT}check_name?account_name=<AWS_ACCOUNT_NAME></pre> | AWS DevOps | 
| 執行帳戶建立工作流程。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 

### （選用） 清除解決方案
<a name="optional-clean-up-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 從 Amazon S3 儲存貯體移除物件。 | 移除下列 Amazon S3 儲存貯體中的任何物件：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 
| 刪除 CloudFormation 堆疊。 | 輸入下列命令來刪除 CloudFormation 堆疊：<pre>aws cloudformation delete-stack \<br />  --stack-name lza-account-creation-workflow-application<br />aws cloudformation wait stack-delete-complete \<br />  --stack-name lza-account-creation-workflow-application</pre> | AWS DevOps | 
| 刪除管道。 | 輸入下列命令來刪除`lza-account-creation-workflow-pipeline`管道：<pre>cdk destroy lza-account-creation-workflow-pipeline --force</pre> | AWS DevOps | 

## 相關資源
<a name="automate-account-creation-lza-resources"></a>
+ [上的登陸區域加速器 AWS](https://aws.amazon.com/solutions/implementations/landing-zone-accelerator-on-aws/) (AWS 解決方案程式庫）
+ [疑難排解 AWS CDK 常見問題 ](https://docs.aws.amazon.com/cdk/v2/guide/troubleshooting.html)(AWS CDK 文件）

## 其他資訊
<a name="automate-account-creation-lza-additional"></a>

**Step Functions 工作流程圖表**

下圖顯示 Step Functions 工作流程中的狀態。

![\[Step Functions 工作流程中的狀態。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/d31abfaa-6854-4923-b896-3b817de9f4d9/images/d93aa7bf-1144-4f25-9488-aacc534a7813.png)


**Arguments (引數)**

以下是您在執行啟動 Step Functions 工作流程的 Python 指令碼時可以使用的引數。

下列是必要引數：
+ `account-name (-a)` （字串） – 新 的名稱 AWS 帳戶。
+ `support-dl (-s)` （字串） – 帳戶建立程序完成時收到通知的電子郵件地址。
+ `managed-org-unit (-m)` （字串） – 將包含新帳戶的受管[組織單位 (OU)](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html#organizationalunit)。

下列引數為選用：
+ `ad-integration (-ad)` （字串字典） – Microsoft Entra ID 群組和指派的許可集。以下是如何使用此引數的範例：

  ```
  --ad-integration "{\"<PermissionSetName>\": \"<EntraIdGroupName>\"}"
  ```
+ `account-email (-e)`** **（字串） – 新 根使用者的電子郵件地址 AWS 帳戶。
**注意**  
如果未使用此引數，則會使用 `rootEmailDomain` 檔案中的值 `rootEmailPrefix`和 產生電子郵件地址`configs/deploy-config.yaml`。如果未提供電子郵件地址，則會使用下列格式產生電子郵件地址：`rootEmailPrefix+accountName@rootEmailDomain`。
+ `region (-r)` （字串） – 已部署 Step Functions 工作流程 AWS 區域 的 。預設值為 `us-east-1`。
+ `force-update (-f)` （字串布林值） – 輸入 `true` 強制 AWS Service Catalog 更新佈建的產品。
+ `bypass-creation (-b)` （字串布林值） – 輸入 `true` 以略過將帳戶新增至 `accounts-config.yaml` 檔案，並略過執行`AWSAccelerator-Pipeline`管道。此引數通常用於測試帳戶建立工作流程程序，或在`Landing Zone Accelerator`管道發生錯誤時執行其餘的 Step Functions 步驟。
+ `tags (-t)` （字串） – 您要新增至 的其他標籤 AWS 帳戶。根據預設，會新增下列標籤：`account-name`、 `support-dl`和 `purpose`。以下是如何使用此引數的範例：

  ```
  --tags TEST1=VALUE1 TEST2=VALUE2
  ```

# 跨多個帳戶和區域自動清查 AWS 資源
<a name="automate-aws-resource-inventory"></a>

*Matej Macek，Amazon Web Services*

## 摘要
<a name="automate-aws-resource-inventory-summary"></a>

此模式概述自動化方法，以維護跨多個帳戶和 的完整 AWS 資源庫存 AWS 區域。它旨在協助基礎設施和安全工程師改善其資源管理實務。它使用 AWS Config 來追蹤資源變更、用於查詢的 Amazon Athena，以及用於互動式儀表板的 Amazon Quick Sight。您可以透過部署 AWS CloudFormation 堆疊來實作此解決方案。

此解決方案類似於[使用 Amazon Athena 和 Amazon Quick Sight 視覺化 AWS Config 資料中所呈現的解決方案 ](https://aws.amazon.com/blogs/mt/visualizing-aws-config-data-using-amazon-athena-and-amazon-quicksight/)(AWS 部落格文章）。此模式擴展了該解決方案，以解決下列常見需求，並提供下列主要優點：
+ 以**合規為重**心 – 此方法可協助您符合法規要求，例如 [PCI DSS](https://www.pcisecuritystandards.org/)、[NIST SP 800-53](https://csrc.nist.gov/pubs/sp/800/53/r5/upd1/final)、[ISO/IEC 27001](https://www.iso.org/standard/27001)、[HIPAA](https://www.hhs.gov/programs/hipaa/index.html)、[GDPR](https://gdpr.eu/) 和其他要求正確資產庫存的法規要求。
+ **自訂架構** – 它提供為各種 AWS 資源建立 Quick Sight 儀表板的基礎，讓您可以根據特定需求自訂解決方案。
+ **使用者驅動的增強**功能 – 此方法納入來自真實世界使用案例的意見回饋，並處理對更全面解決方案的請求。

基礎設施、安全和財務團隊通常會在動態、多帳戶或多區域環境中面臨可見性和協作挑戰。此解決方案旨在解決這些挑戰，並大幅減少建立和維護資源庫存所需的時間和精力。結果是資源的集中檢視，可協助您改善資源配置決策、識別和降低風險、最佳化成本，以及改善整體可見性和協同合作。此方法可彌補概念性解決方案與實際實作需求之間的差距，以達成安全性、合規性和營運目的。

## 先決條件和限制
<a name="automate-aws-resource-inventory-prereqs"></a>

**先決條件**
+ 下列作用中 AWS 帳戶：
  + *管理帳戶* - 用於計費、建立帳戶和控制整個組織的存取的集中式帳戶
  + *稽核帳戶* – 用於安全監控、合規檢查和偏離通知的集中式中樞
  + *日誌封存帳戶* – 用於儲存和分析收集的資料的集中式帳戶
+ 在稽核帳戶中， AWS Config [彙總工具](https://docs.aws.amazon.com/config/latest/developerguide/aggregate-data.html)會從目標帳戶和區域收集和彙總組態資料
+ 在日誌封存帳戶中，設定下列項目：
  + Amazon Simple Storage Service (Amazon S3) AWS Config 儲存[貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)體，您可以在其中存放來自彙總工具的資料
  + Amazon Quick [訂閱](https://docs.aws.amazon.com/quicksight/latest/user/signing-up.html)
  + Quick Sight 與 Amazon Athena 之間的[授權連線](https://docs.aws.amazon.com/quicksight/latest/user/athena.html) 
  + 透過 Athena 查詢存取 Amazon S3 儲存貯體的[許可](https://docs.aws.amazon.com/athena/latest/ug/s3-permissions.html) 
+ 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/cli-chap-configure.html)
+ 部署佈建下列資源之 CloudFormation 堆疊的許可：
  +  AWS Lambda 函數
  + Amazon S3 通知組態
  + Athena 資料庫、資料表和檢視
  + Quick Sight 資料集和資料來源
+ 在 中執行自動化的許可 AWS Systems Manager
+ 快速存取的許可

**限制**
+ 解決方案倚賴 AWS Config。 AWS Config 通常在偵測到變更後立即記錄資源的組態變更，或依您指定的頻率記錄。不過，這需要盡最大努力，有時可能需要更長的時間。
+ 此解決方案只會追蹤 [AWS Config 支援的資源類型](https://docs.aws.amazon.com/config/latest/developerguide/resource-config-reference.html)。
+ 解決方案不會追蹤其他雲端提供者或內部部署環境的資源庫存。
+ 有些 AWS 服務 不適用於所有 AWS 區域。如需區域可用性，請參閱 AWS 文件中的[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)頁面，然後選擇服務的連結。

## Architecture
<a name="automate-aws-resource-inventory-architecture"></a>

下圖顯示簡化的流程，用於收集、組織、分析和視覺化 AWS 組織中多個帳戶的組態和合規資料。

![\[收集和視覺化整個組織的組態和合規資料。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/67a9667a-da19-4dcb-a2fe-62bc94a0541b/images/c9245de1-ac85-4a9e-a0c0-dbcc27a8bb5d.png)


該圖顯示以下工作流程：

1.  AWS Config 彙總器會定期收集目標帳戶和區域中資源的組態和合規資料，然後將資料交付至日誌封存帳戶中的 Amazon S3 儲存貯體。

1. 將新 AWS Config 資料新增至 Amazon S3 儲存貯體會叫用 AWS Lambda 函數。

1. Lambda 函數透過使用對應於每個快照檔案的區域和日期的值來設定索引鍵來分割資料。這有助於 AWS Glue 有效率地查詢和處理組態和合規資料。

1. Amazon Athena 使用 AWS Glue [結構描述](https://docs.aws.amazon.com/glue/latest/dg/schema-registry.html)，針對存放在 Amazon S3 儲存貯體中的資料執行 SQL 查詢。它利用來自 的結構描述中繼資料 AWS Glue 來了解資料的結構。

1. Athena [中的檢視](https://docs.aws.amazon.com/athena/latest/ug/views.html)會定義和擷取目標資料集。

1. Quick Sight 中的[儀表板](https://docs.aws.amazon.com/quicksight/latest/user/using-dashboards.html)可協助您視覺化和分析資料集。

## 工具
<a name="automate-aws-resource-inventory-tools"></a>

**AWS 服務**
+ [Amazon Athena](https://docs.aws.amazon.com/athena/latest/ug/what-is.html) 是一種互動式查詢服務，可協助您使用標準 SQL 直接在 Amazon S3 中分析資料。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 資源、快速且一致地佈建資源，以及在整個 AWS 帳戶 和 生命週期中管理資源 AWS 區域。
+ [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) 提供 中資源的詳細檢視 AWS 帳戶 及其設定方式。它可協助您識別資源彼此之間的關係，以及其組態如何隨著時間而改變。 AWS Config [彙整工具](https://docs.aws.amazon.com/config/latest/developerguide/aggregate-data.html)會從多個 和 區域收集 AWS Config 組態 AWS 帳戶 和合規資料。
+ [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) 是一種全受管的擷取、轉換和載入 (ETL) 服務。它可協助您可靠地分類、清理、擴充和移動資料存放區和資料串流之間的資料。此模式使用 AWS Glue [資料目錄](https://docs.aws.amazon.com/glue/latest/dg/components-overview.html#data-catalog-intro)和[結構描述登錄](https://docs.aws.amazon.com/glue/latest/dg/schema-registry.html)檔。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可協助您將多個 合併 AWS 帳戶 到您建立並集中管理的組織。
+ [Amazon Quick Sight](https://docs.aws.amazon.com/quicksuite/latest/userguide/quick-bi.html) 是一項商業智慧 (BI) 服務，可協助您透過互動式視覺化、儀表板和報告，將原始資料轉換為有意義的洞見。Quick Sight 是 Amazon Quick 的核心元件。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。
+ [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) 可協助您管理在 中執行的應用程式和基礎設施 AWS 雲端。它可簡化應用程式和資源管理、縮短偵測和解決操作問題的時間，並協助您大規模安全地管理 AWS 資源。[AWS Systems Manager 自動化](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-automation.html)可簡化許多 的常見維護、部署和修復任務 AWS 服務。

**程式碼儲存庫**

此模式的 AWS CloudFormation 範本可在[AWS Config 視覺化](https://github.com/aws-samples/aws-management-and-governance-samples/blob/master/AWSConfig/AWS-Config-Visualization/README.md) GitHub 儲存庫中使用。此 CloudFormation 範本會部署 AWS Systems Manager 自動化 Runbook，該 Runbook AWS Config 設定為與 Amazon Athena 搭配使用。此自動化 AWS Glue 準備與指定的 Amazon S3 儲存貯體連線、在 Amazon Athena 中建立檢視，以及設定儀表板視覺化的 Quick Sight。

## 最佳實務
<a name="automate-aws-resource-inventory-best-practices"></a>
+ 建議您遵循 AWS 規範指引中的[設定和管理安全、多帳戶 AWS 環境 AWS Control Tower](https://docs.aws.amazon.com/prescriptive-guidance/latest/migration-aws-environment/welcome.html)的最佳實務。
+ 我們建議您建立 AWS Config 彙總工具，以收集整個 AWS 組織的組態和合規資料。如需詳細資訊，請參閱 AWS Config 文件中的[多帳戶多區域資料彙總](https://docs.aws.amazon.com/config/latest/developerguide/aggregate-data.html)。
+ 部署此解決方案之前，建議您檢閱 [Amazon S3](https://aws.amazon.com/s3/pricing/)、、[AWS Config](https://aws.amazon.com/config/pricing/)[Athena](https://aws.amazon.com/athena/pricing/) 和 [Quick](https://aws.amazon.com/quicksight/pricing/) 的目前定價資訊。

## 史詩
<a name="automate-aws-resource-inventory-epics"></a>

### 部署 CloudFormation 堆疊
<a name="deploy-the-cfnshort-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 下載 CloudFormation 範本。 | 下載 [Config-QuickSight-Visualization-SSM-Automation.yaml](https://github.com/aws-samples/aws-management-and-governance-samples/blob/master/AWSConfig/AWS-Config-Visualization/cft/Config-QuickSight-Visualization-SSM-Automation.yaml) CloudFormation 範本。 | AWS 管理員、雲端管理員、DevOps 工程師 | 
| 修改 CloudFormation 範本。 | 只有在您使用 [AWS Control Tower](https://aws.amazon.com/controltower/)並由 AWS Config 管理時，才完成此步驟 AWS Control Tower。您需要修改 CloudFormation 範本。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | DevOps 工程師、AWS 管理員 | 
| 建立 CloudFormation 堆疊。 | 遵循[從 CloudFormation 主控台建立堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)中的指示。注意下列事項：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 

### 在 Systems Manager 中執行自動化
<a name="run-the-automation-in-sys"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 尋找您的快速使用者名稱。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 
| 尋找交付管道名稱和 Amazon S3 儲存貯體名稱。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 
| 在 Systems Manager 中執行自動化。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 

### 在 Quick Sight 中視覺化資料
<a name="visualize-data-in-qsight"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 重新整理資料。 | 若要根據您的特定需求排程資料集重新整理，請遵循[重新整理 SPICE 資料](https://docs.aws.amazon.com/quicksight/latest/user/refreshing-imported-data.html)中的指示。 | AWS 管理員、DevOps 工程師、雲端管理員 | 
| 建立 分析。 | 若要在 Quick Sight 中建立可協助您視覺化資源的儀表板，請遵循在 [Quick Sight 中開始分析](https://docs.aws.amazon.com/quicksuite/latest/userguide/creating-an-analysis.html)中的指示。 | Quick Suite 管理員 | 
| 建立儀表板。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | Quick Suite 管理員 | 

### (選用) 清除
<a name="optional-clean-up"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除 Systems Manager 自動化建立的資源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理員、雲端管理員、DevOps 工程師 | 
| 刪除 CloudFormation 堆疊。 | 若要刪除`Config-QuickSight-Visualization-SSM-Automation`堆疊中的資源，請遵循[從 CloudFormation 主控台刪除堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html)中的指示。 | AWS 管理員、雲端管理員、DevOps 工程師 | 

## 疑難排解
<a name="automate-aws-resource-inventory-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| Amazon Quick 正在嘗試連線至 `us-east-1` AWS 區域，但不允許在該區域中建立資源。 | 服務控制政策限制您在此區域中訂閱 Amazon Quick。在服務控制政策中，手動指定目標 AWS 區域。`<REGION_ID>` 將 取代為適當的區域識別符：<pre>https://<REGION_ID>.quicksight.aws.amazon.com/sn/start/dashboards</pre>以下是範例：<pre>https://eu-central-1.quicksight.aws.amazon.com/sn/start/dashboards</pre> | 
| 在 Amazon Athena 中，您遇到下列訊息：`Before you run your first query, you need to set up a query result location in Amazon S3.` | 請確定您已準備好要存放 Amazon Athena 查詢結果的 Amazon S3 儲存貯體。 Amazon Athena 然後遵循[使用 Amazon Athena 主控台指定查詢結果位置](https://docs.aws.amazon.com/athena/latest/ug/query-results-specify-location-console.html)中的指示。 | 

## 相關資源
<a name="automate-aws-resource-inventory-resources"></a>

**AWS 文件**
+ [AWS Config 文件](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html)
+ [Amazon Quick 文件](https://docs.aws.amazon.com/quicksuite/latest/userguide/what-is.html)

**AWS 部落格文章**
+ [使用 自動化 AWS Config 資料視覺化 AWS Systems Manager](https://aws.amazon.com/blogs/mt/automate-aws-config-data-visualization-with-aws-systems-manager/)
+ [如何使用 定期記錄資源組態變更 AWS Config](https://aws.amazon.com/blogs/mt/how-to-record-resource-configuration-changes-periodically-with-aws-config/)

**其他資源**
+ [Amazon Quick Community Learning Center](https://community.amazonquicksight.com/c/learning-center/10/none)
+ [Amazon Quick Community Gallery](https://community.amazonquicksight.com/c/gallery/44)

# 建置包含 MongoDB Atlas 的 AWS 登陸區域
<a name="build-aws-landing-zone-that-includes-mongodb-atlas"></a>

*Igor Alekseev，Amazon Web Services*

*Anuj Panchal，MongoDB*

## 總結
<a name="build-aws-landing-zone-that-includes-mongodb-atlas-summary"></a>

此模式說明如何建置與 MongoDB Atlas 叢集整合的 AWS 登陸區域。基礎設施會使用 Terraform 指令碼自動部署。

結構良好的多帳戶 AWS 環境稱為[登陸區域](https://docs.aws.amazon.com/prescriptive-guidance/latest/strategy-migration/aws-landing-zone.html)，可提供可擴展性和安全性，特別是企業。它可作為工作負載和應用程式快速部署的基礎，並有助於確保對安全性和基礎設施的信心。建置登陸區域需要仔細考慮技術和商業因素，包括帳戶結構、聯網、安全和存取管理。這些考量事項應與組織的未來成長和業務目標保持一致。

此模式的使用案例包括下列項目。
+ **企業 SaaS 和 PaaS 平台：**在 上執行的多租戶軟體即服務 (SaaS) 應用程式和平台即服務 (PaaS) 平台 AWS 可以使用此設定，協助提供對 MongoDB Atlas 的安全私有存取，而無需透過公有網際網路公開資料。
+ **高度監管的產業**：銀行、金融服務、醫療保健和政府工作負載需要嚴格遵守健康保險流通與責任法案 (HIPAA)、支付卡產業資料安全標準 (PCI DSS)、系統和組織控制 2 (SOC2) 和一般資料保護法規 (GDPR) 等標準，受益於：
  + 透過 加密的私有連線 AWS PrivateLink
  + MongoDB 複本集的異地同步備份高可用性
+ **安全的 AI/ML 工作負載**：Amazon Bedrock、Amazon SageMaker AI 或自訂 AI 模型中的訓練或推論管道，可以透過 PrivateLink 安全地擷取和存放 MongoDB Atlas 中的資料。
+ **災難復原和業務持續性**：多可用區設計可確保單一可用區域故障不會中斷工作負載。跨可用區域的 Atlas 複本集可確保自動容錯移轉。這對金融技術 （金融技術） 應用程式、數位銀行或醫療保健監控等全年無休的服務至關重要。

## 先決條件和限制
<a name="build-aws-landing-zone-that-includes-mongodb-atlas-prereqs"></a>

**先決條件**
+ 組織擁有者可存取 MongoDB Atlas，因此您可以建立 Atlas API 金鑰。如需此需求的相關資訊，請參閱 MongoDB 文件中的[管理組織存取](https://www.mongodb.com/docs/atlas/tutorial/manage-organizations/)。
+ 作用中的 [AWS 帳戶](https://aws.amazon.com/resources/create-account/)。
+ [Terraform](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli)，已安裝並設定。
+ 使用 MongoDB 6.0 版或更新版本建立的 MongoDB Atlas 叢集。
+ 熟悉 MongoDB 和 MongoDB Atlas。如需詳細資訊，請參閱 [MongoDB Atlas 文件](https://www.mongodb.com/docs/atlas/)。

**限制**
+ 有些 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)，然後選擇服務的連結。

## Architecture
<a name="build-aws-landing-zone-that-includes-mongodb-atlas-architecture"></a>

下列參考架構圖說明與 MongoDB Atlas 私有端點整合之 AWS 登陸區域的部署設定。此參考架構示範如何建立與 MongoDB Atlas 整合的安全、可擴展且高可用性的 AWS 登陸區域。透過結合多可用區部署、最低權限安全控制和私有連線等 AWS 最佳實務，此設計可讓組織為現代應用程式佈建強大的環境。

![\[與 MongoDB Atlas 整合的 AWS 登陸區域的異地同步備份架構。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/72d335b9-b5b1-4fe2-9972-65edbec60ab1/images/82a8cc98-6f22-4e28-a236-57a809930055.png)


此架構包含下列項目：

**VPC**
+ 單一虛擬私有雲端 (VPC) 跨越三個可用區域。
+ VPC 會細分為與每個可用區域對齊的子網路。這些子網路會分配工作負載以獲得高可用性。

**網際網路存取**
+ 網際網路閘道可為所需的資源提供傳出網際網路連線，例如應用程式或堡壘主機。
+ 公有子網路可以容納 NAT 閘道，允許私有子網路工作負載下載更新、修補程式和其他必要的套件，而不會直接公開到公有網際網路。

**私有子網路和路由表**
+ 應用程式元件、微服務或其他敏感資源通常位於私有子網路中。
+ 專用路由表控制流量流程。將來自私有子網路的傳出流量路由到 NAT 閘道，以實現安全、僅限輸出的網際網路存取。
+ 來自網際網路的傳入請求會流經公有子網路中的彈性負載平衡器或堡壘主機 （如果使用的話），然後適當地路由至私有子網路資源。

**透過 PrivateLink 的 MongoDB Atlas 連線**
+ 此架構使用 PrivateLink （透過 VPC 端點） 安全地連線至 MongoDB Atlas，而不會將您的資料暴露至公有網際網路。
+ 請求會保留在 AWS 骨幹網路上。傳輸中的資料受益於 PrivateLink 加密，且絕不會透過公有網際網路路由。
+ MongoDB Atlas 專用 VPC 託管您的主要和次要節點，並為受管資料庫叢集提供安全、隔離的環境。

**多可用區域部署**
+ 關鍵基礎設施元件 （例如 NAT 閘道和應用程式子網路） 會分佈在至少三個可用區域。如果可用區域發生中斷，此架構可確保剩餘可用區域中的工作負載保持運作。
+ 根據預設，MongoDB Atlas 透過複本集提供高可用性，並確保您的資料庫層保持容錯能力。關鍵基礎設施會分散到至少三個可用區域，以提供彈性。

## 工具
<a name="build-aws-landing-zone-that-includes-mongodb-atlas-tools"></a>

**AWS 服務**
+ [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) 可協助您以 API 呼叫取代程式碼中的硬式編碼登入資料，包括密碼，以程式設計方式擷取秘密。

**其他產品和工具**
+ [MongoDB Atlas](https://www.mongodb.com/atlas) 是全受管資料庫即服務 (DbaaS)，用於在雲端中部署和管理 MongoDB 資料庫。
+ [Terraform](https://www.terraform.io/) 是 HashiCorp 的基礎設施即程式碼 (IaC) 工具，可協助您建立和管理雲端和內部部署資源。在此模式中，您會使用 Terraform 執行指令碼，以協助在 AWS 和 MongoDB Atlas 上部署所需的資源。

**程式碼儲存庫**

此模式的程式碼可在 [AWS 和 MongoDB Atlas 登陸區域](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone) GitHub 儲存庫中使用。

## 史詩
<a name="build-aws-landing-zone-that-includes-mongodb-atlas-epics"></a>

### 完成探索和評估
<a name="complete-discovery-and-assessment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 識別關鍵利益相關者。 | 識別參與登陸區域專案的所有主要利益相關者和團隊成員。這可能包括下列角色：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/build-aws-landing-zone-that-includes-mongodb-atlas.html) | 遷移潛在客戶 | 
| 建立結構藍圖。 | 建立藍圖，概述 AWS 和啟用 MongoDB Atlas 的登陸區域所需的結構。 | 遷移潛在客戶 | 
| 建立架構計劃。 | 與您的應用程式架構師合作，以分析需求並設計容錯且具彈性的架構。此模式提供入門架構範本供您參考。您可以自訂此範本，以滿足組織的安全和基礎設施需求。 | 雲端架構師 | 
| 規劃設定和部署。 | 與所有利益相關者一起決定如何部署架構、如何實作安全措施，以及任何其他層面，以確保與組織和請求團隊的利益保持一致。 | Migration Lead，DevOps 工程師，DBA | 

### 設定 MongoDB Atlas 環境
<a name="set-up-the-mongodb-atlas-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製儲存庫。 | 執行 命令，從 [GitHub 儲存庫](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone)複製程式碼：<pre>git clone https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone</pre> | 應用程式開發人員、DevOps 工程師 | 
| 取得您的 Atlas 組織 ID。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/build-aws-landing-zone-that-includes-mongodb-atlas.html) | DBA | 
| 產生 Atlas 組織層級 API 金鑰。 | 若要在 Atlas 中產生組織層級 API 金鑰，請遵循 [MongoDB 文件](https://www.mongodb.com/docs/atlas/configure-api-access/#grant-programmatic-access-to-an-organization)中的指示。 | DBA | 
| 在 中建立秘密 AWS Secrets Manager。 | 將上一個步驟中產生的 MongoDB Atlas API 金鑰儲存為 Secrets Manager 中的金鑰值秘密。如需說明，請參閱 [Secrets Manager 文件](https://docs.aws.amazon.com/secretsmanager/latest/userguide/create_secret.html)。 | DevOps 工程師 | 
| 選取 Atlas 叢集層。 | 若要選取正確的 Atlas 叢集層，請遵循 [MongoDB 文件](https://www.mongodb.com/docs/atlas/sizing-tier-selection/)中的指示。 | DBA | 

### 設定 AWS 環境
<a name="set-up-the-aws-environments"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 修改 Terraform 指令碼。 | 在 GitHub 儲存庫的本機副本中，更新 [module/mongodb-atlas/main.tf](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/3748350730ec2ac7ab64662d536b67b4840c667c/modules/mongodb-atlas/main.tf#L12) 檔案 （第 12 行） 中的秘密名稱，因此 Terraform 可以在部署期間從 Secrets Manager 擷取登入資料。 | DevOps 工程師 | 
| 建立 AWS 存取金鑰 ID 和私密金鑰。 | 若要建立您的 AWS 存取金鑰 ID 和私密金鑰，請遵循 AWS re：Post 文章中的指示[如何建立 AWS 存取金鑰？](https://repost.aws/knowledge-center/create-access-key)最佳實務是指派具有所需最低權限的政策，但在此案例中，請選取`AdministratorAccess`政策。建立存取金鑰後，請檢閱 [IAM 中的安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)，以了解管理存取金鑰的最佳實務。 | DevOps 工程師 | 
| 配置彈性 IP 地址。 | 配置至少兩個彈性 IP IDs。如需說明，請參閱 [Amazon Virtual Private Cloud (Amazon VPC) 文件](https://docs.aws.amazon.com/vpc/latest/userguide/WorkWithEIPs.html)。 | DevOps 工程師 | 
| 建立 S3 儲存貯體。 | 遵循 Amazon Simple Storage Service (Amazon S3) 文件中的指示，建立 S3 儲存貯體以存放 Terraform 部署的狀態。 [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html) | DevOps 工程師 | 
| 更新 S3 儲存貯體以進行儲存。 | 更新本機版本 [environments/development/main.tf](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/83e0b52cc4a8c12b24b54edeecbae496880d3615/environments/development/main.tf#L16) 中的 S3 儲存貯體資訊，以符合您在上一個步驟中建立的儲存貯體名稱和區域，並指定金鑰字首。例如：<pre>terraform {<br />       ...<br />  backend "s3" {<br />    bucket = "startup-name-product-terraform"<br />    key    = "network/dev"<br />    region = "ap-southeast-1"<br />  }<br />}</pre>在此範例中，您可以將 Terraform 設定為使用金鑰字首`network/dev`來組織 Terraform 狀態檔案。您可以將 值變更為 `prod`或 `staging`，以符合您要建立的環境。如需使用多個環境的資訊，請參閱本節中的最後一個步驟。如需 Amazon S3 金鑰字首的詳細資訊，請參閱 Amazon S3 文件中的[使用字首組織物件](https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-prefixes.html)。 | DevOps 工程師 | 
| 設定 Terraform 變數。 | 範例登陸區域使用 [Terraform 變數定義檔案來定義輸入變數](https://www.terraform.io/docs/language/values/variables.html#variable-definitions-tfvars-files)值。變數檔案位於 [environments/development/variables.tf](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/main/environments/development/variables.tf)。您可以在 [environments/development/terraform.tfvars](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/main/environments/development/terraform.tfvars) 檔案中設定變數值。如 GitHub 儲存庫的[讀我檔案](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/main/README.md#terraform-variables)所述設定這些變數。 | DevOps 工程師 | 
| 設定環境變數。 | 如果您打算在本機電腦上執行 Terraform 指令碼，請設定下列環境變數：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/build-aws-landing-zone-that-includes-mongodb-atlas.html)如需設定環境變數的詳細資訊，請參閱 [AWS Command Line Interface (AWS CLI) 文件。](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html#envvars-set) | DevOps 工程師 | 
| 檢查 VPC 組態。 | 若要遵循 建議的最佳實務 AWS，請在 Terraform 指令碼中設定 VPC 和子網路 CIDRs、NAT 閘道、路由和路由表的設定，以符合組織的需求。如需詳細資訊，請參閱 GitHub 儲存庫的[讀我檔案](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/main/README.md#vpc-configurations)。 | DevOps 工程師 | 
| 標記 資源。 | 您可以標記 AWS 資源，以便在 Terraform 指令碼部署它們時對其進行監控。如需範例，請參閱 GitHub 儲存庫的[讀我檔案](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/main/README.md#resource-taggings)。如需透過標籤監控資源的成本、用量等資訊，請參閱 AWS Billing 文件中的[啟用使用者定義的成本分配標籤](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/activating-tags.html)。 | DevOps 工程師 | 
| 使用多個環境。 | GitHub 儲存庫提供`development`環境資料夾。您也可以在環境資料夾中新增自己的環境。若要新增環境，請將 `development` 資料夾複製到 下的新資料夾 （例如， `prod`或 `staging`)`environments`。然後，您可以使用新值更新`terraform.tfvars`檔案。 | DevOps 工程師 | 

### 部署登陸區域
<a name="deploy-the-landing-zone"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 初始化 Terraform 工作目錄。 | 若要初始化工作目錄並下載必要的套件，請執行 命令：<pre>terraform init</pre> | DevOps 工程師 | 
| 建立執行計畫。 | 若要建立執行計畫並將 Terraform 對基礎設施所做的變更視覺化，請執行 命令：<pre>terraform plan</pre> | DevOps 工程師 | 
| 部署變更。 | 若要如程式碼所述實作基礎設施的變更，請執行 命令：<pre>terraform apply</pre> | DevOps 工程師 | 
| 驗證部署。 | 驗證 Terraform 在基礎設施中建立或修改的元件。若要測試設定，請在 中佈建運算資源 （例如 Amazon EC2 執行個體或 AWS Lambda 函數） 或連接到 VPC。 | DevOps 工程師，應用程式開發人員 | 

### 移除資源
<a name="remove-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 清除。 | 完成測試後，請執行下列命令來銷毀 Terraform 部署在基礎設施中的資源：<pre>terraform destroy</pre> | DevOps 工程師 | 

## 相關資源
<a name="build-aws-landing-zone-that-includes-mongodb-atlas-resources"></a>

**探索和評估**
+ [登陸區域設定的管理秘訣 ](https://docs.aws.amazon.com/controltower/latest/userguide/tips-for-admin-setup.html)(AWS Control Tower 文件）
+ [對登陸區域組態的期望](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-configure.html) (AWS Control Tower 文件）
+ [登陸區域更新的最佳實務 ](https://docs.aws.amazon.com/controltower/latest/userguide/lz-update-best-practices.html)(AWS Control Tower 文件）

**設定 MongoDB Atlas 和 AWS 環境**
+ [取得 MongoDB Atlas](https://aws.amazon.com/marketplace/pp/prodview-pp445qepfdy34) (AWS Marketplace)
+ [記憶體](https://docs.atlas.mongodb.com/sizing-tier-selection/#memory) (MongoDB Atlas 文件）
+ [使用 Atlas 範例資料集調整大小範例](https://www.mongodb.com/docs/atlas/sizing-tier-selection/#example--the-service-sample-data-sets) (MongoDB Atlas 文件）
+ [行動應用程式的大小範例 ](https://www.mongodb.com/docs/atlas/sizing-tier-selection/#example--mobile-app)(MongoDB Atlas 文件）
+ [網路流量](https://docs.atlas.mongodb.com/sizing-tier-selection/#network-traffic) (MongoDB Atlas 文件）
+ [叢集自動擴展 ](https://www.mongodb.com/docs/atlas/sizing-tier-selection/#cluster-auto-scaling)(MongoDB Atlas 文件）
+ [Atlas 大小調整範本](https://www.mongodb.com/docs/atlas/sizing-tier-selection/) (MongoDB Atlas 文件）
+ [設定網路對等互連 ](https://docs.atlas.mongodb.com/security-vpc-peering/)(MongoDB Atlas 文件）
+ [Atlas 中的私有端點](https://docs.atlas.mongodb.com/security-private-endpoint/) (MongoDB Atlas 文件）
+ [用戶端欄位層級加密 ](https://docs.mongodb.com/manual/core/security-client-side-encryption)(MongoDB 資料庫文件）
+ [自動加密](https://docs.mongodb.com/manual/core/security-automatic-client-side-encryption) (MongoDB 資料庫文件）
+ [選取叢集層](https://www.mongodb.com/docs/atlas/manage-clusters/#select-cluster-tier) (MongoDB Atlas 文件）

**部署登陸區域**
+ [上的 Terraform AWS](https://docs.aws.amazon.com/whitepapers/latest/cicd_for_5g_networks_on_aws/terraform.html) （白皮書*上適用於 5G 網路的 CI/CD AWS*)
+ [MongoDB Atlas 搭配 Terraform](https://www.mongodb.com/developer/products/atlas/mongodb-atlas-with-terraform/) (MongoDB 文件）

# 設定 VPC 流程日誌以進行跨 的集中化 AWS 帳戶
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts"></a>

*Benjamin Morris 和 Aman Kaur Gandhi，Amazon Web Services*

## 總結
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts-summary"></a>

在 AWS 虛擬私有雲端 (VPC) 中，VPC 流程日誌功能可以提供有用的資料，以進行操作和安全性疑難排解。不過，在多帳戶環境中使用 VPC 流程日誌會有限制。具體而言，不支援來自 Amazon CloudWatch Logs 的跨帳戶流程日誌。反之，您可以使用適當的儲存貯體政策設定 Amazon Simple Storage Service (Amazon S3) 儲存貯體，以集中管理日誌。

**注意**  
此模式討論將流程日誌傳送至集中位置的需求。不過，如果您也希望日誌可在成員帳戶中於本機使用，您可以為每個 VPC 建立多個流程日誌。無法存取 Log Archive 帳戶的使用者可以看到流量日誌以進行疑難排解。或者，您可以為將日誌傳送至 CloudWatch Logs 的每個 VPC 設定單一流程日誌。然後，您可以使用 Amazon Data Firehose 訂閱篩選條件，將日誌轉送至 S3 儲存貯體。如需詳細資訊，請參閱[相關資源](#configure-vpc-flow-logs-for-centralization-across-aws-accounts-resources)一節。

## 先決條件和限制
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts-prereqs"></a>

**先決條件 **
+ 作用中 AWS 帳戶
+ 具有 帳戶 AWS Organizations 的組織，用於集中日誌 （例如，Log Archive)

**限制 **

如果您使用 AWS Key Management Service (AWS KMS) 受管金鑰`aws/s3`來加密您的中央儲存貯體，則不會收到來自不同帳戶的日誌。反之，您會看到`Unsuccessful`錯誤代碼 400，其中包含訊息，例如指定 `"LogDestination: <bucketName> is undeliverable"` 的 `ResourceId`。這是因為帳戶的 AWS 受管金鑰無法跨帳戶共用。解決方案是使用 Amazon S3 受管加密 (SSE-S3) 或可與成員帳戶共用 AWS KMS 的客戶受管金鑰。

## Architecture
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts-architecture"></a>

**目標架構**

在下圖中，每個 VPC 部署了兩個流程日誌。一個 會將日誌傳送至本機 CloudWatch Logs 群組。另一個 會將日誌傳送至集中式日誌帳戶中的 S3 儲存貯體。儲存貯體政策允許日誌交付服務將日誌寫入儲存貯體。

**注意**  
截至 2023 年 11 月， AWS 現在支援 [aws：SourceOrgID 條件金鑰](https://aws.amazon.com/about-aws/whats-new/2023/11/organization-wide-iam-condition-keys-restrict-aws-service-to-service-requests/)。此條件可讓您拒絕寫入 AWS Organizations 組織外部帳戶的集中式儲存貯體。

![\[每個 VPC 都會有一個流程日誌將日誌傳送至 CloudWatch，另一個則將日誌傳送至 S3 儲存貯體。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/718c29f4-a035-47ab-9c58-bd7d5c1ca77e/images/0b502d82-a6ce-4832-b854-99181d2ed834.png)


**自動化和擴展**

每個 VPC 都設定為將日誌傳送至中央記錄帳戶中的 S3 儲存貯體。使用下列其中一個自動化解決方案，以協助確保流程日誌設定正確：
+ [CloudFormation StackSets](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)
+ [AWS Control Tower Terraform 帳戶工廠 (AFT)](https://docs.aws.amazon.com/controltower/latest/userguide/taf-account-provisioning.html)
+ [具有修復的 AWS Config 規則](https://aws.amazon.com/blogs/mt/how-to-enable-vpc-flow-logs-automatically-using-aws-config-rules/)

## 工具
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts-tools"></a>

**工具**
+ [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html) 可協助您集中所有系統、應用程式的日誌， AWS 服務 以便您可以監控日誌並將其安全地存檔。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可協助您在已定義的虛擬網路中啟動 AWS 資源。此虛擬網路與您在自己的資料中心中操作的傳統網路相似，且具備使用 AWS可擴展基礎設施的優勢。此模式使用 [VPC 流程日誌](https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs.html)功能來擷取進出 VPC 網路介面之 IP 流量的相關資訊。

## 最佳實務
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts-best-practices"></a>

使用基礎設施做為程式碼 (IaC) 可大幅簡化 VPC 流程日誌部署程序。抽象 VPC 部署定義以包含流程日誌資源建構，會自動使用流程日誌部署您的 VPCs。這會在下一節中示範。

**集中式流程日誌**

將集中式流程日誌新增至 HashiCorp Terraform 中 VPC 模組的範例語法：此程式碼會建立流程日誌，將日誌從 VPC 傳送至集中式 S3 儲存貯體。請注意，此模式不包含 S3 儲存貯體的建立。如需建議的儲存貯體政策陳述式，請參閱[其他資訊](#configure-vpc-flow-logs-for-centralization-across-aws-accounts-additional)一節。

```
variable "vpc_id" { type = string }
locals { custom_log_format_v5 = "$${version} $${account-id} $${interface-id} $${srcaddr} $${dstaddr} $${srcport} $${dstport} $${protocol} $${packets} $${bytes} $${start} $${end} $${action} $${log-status} $${vpc-id} $${subnet-id} $${instance-id} $${tcp-flags} $${type} $${pkt-srcaddr} $${pkt-dstaddr} $${region} $${az-id} $${sublocation-type} $${sublocation-id} $${pkt-src-aws-service} $${pkt-dst-aws-service} $${flow-direction} $${traffic-path}" }
resource "aws_flow_log" "centralized_flow_log" {
  log_destination      = "arn:aws:s3:::centralized-vpc-flow-logs-<log_archive_account_id>" # Optionally, a prefix can be added after the ARN.
  log_destination_type = "s3"
  traffic_type         = "ALL"
  vpc_id               = var.vpc_id
  log_format           = local.custom_log_format_v5 # If you want fields from VPC Flow Logs v3+, you will need to create a custom log format.
}
```

如需自訂日誌格式的詳細資訊，請參閱 [Amazon VPC 文件](https://docs.aws.amazon.com/vpc/latest/userguide/flow-log-records.html#flow-logs-custom)。

**本機流程日誌**

將本機流程日誌新增至 Terraform 中具有必要許可的 VPC 模組的範例語法：此程式碼會建立流程日誌，將日誌從 VPC 傳送至本機 CloudWatch Logs 群組。

```
data "aws_region" "current" {}
variable "vpc_id" { type = string }
resource "aws_iam_role" "local_flow_log_role" {
  name = "flow-logs-policy-${var.vpc_id}"
  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [{
      "Effect": "Allow",
      "Principal": {"Service": "vpc-flow-logs.amazonaws.com"},
      "Action": "sts:AssumeRole"
  }]
}
EOF
}
resource "aws_iam_role_policy" "logs_permissions" {
  name = "flow-logs-policy-${var.vpc_id}"
  role = aws_iam_role.local_flow_log_role.id
  policy = <<EOF
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [{
      "Action": ["logs:CreateLog*", "logs:PutLogEvents", "logs:DescribeLog*", "logs:DeleteLogDelivery"],
      "Effect": "Allow",
      "Resource": "arn:aws:logs:${data.aws_region.current.name}:*:log-group:vpc-flow-logs*"
  }]
}
EOF
}
resource "aws_cloudwatch_log_group" "local_flow_logs" {
  name              = "vpc-flow-logs/${var.vpc_id}"
  retention_in_days = 30
}
resource "aws_flow_log" "local_flow_log" {
  iam_role_arn    = aws_iam_role.local_flow_log_role.arn
  log_destination = aws_cloudwatch_log_group.local_flow_logs.arn
  traffic_type    = "ALL"
  vpc_id          = var.vpc_id
}
```

## 史詩
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts-epics"></a>

### 部署 VPC 流程日誌基礎設施
<a name="deploy-vpc-flow-logs-infrastructure"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 決定加密策略，並建立中央 S3 儲存貯體的政策。 | 中央儲存貯體不支援 AWS KMS `aws/s3`金鑰，因此您必須使用 SSE-S3 或 AWS KMS 客戶受管金鑰。如果您使用 AWS KMS 金鑰，金鑰政策必須允許成員帳戶使用 金鑰。 | 合規 | 
| 建立中央流程日誌儲存貯體。 | 建立流程日誌將傳送到的中央儲存貯體，並套用您在上一個步驟中選擇的加密策略。這應該位於 Log Archive 或類似用途的帳戶中。從[其他資訊](#configure-vpc-flow-logs-for-centralization-across-aws-accounts-additional)區段取得儲存貯體政策，並在使用環境特定值更新預留位置後將其套用至您的中央儲存貯體。 | 一般 AWS | 
| 設定 VPC 流程日誌將日誌傳送至中央流程日誌儲存貯體。 | 將流程日誌新增至您要從中收集資料的每個 VPC。最可擴展的方式是使用 AFT 或 等 IaC 工具 AWS Cloud Development Kit (AWS CDK)。例如，您可以建立 Terraform 模組，將 VPC 與流程日誌一起部署。如有必要，您可以手動新增流程日誌。 | 網路管理員 | 
| 設定要傳送至本機 CloudWatch Logs 的 VPC 流程日誌。 | （選用） 如果您希望流程日誌顯示在產生日誌的帳戶中，請建立另一個流程日誌，將資料傳送至本機帳戶中的 CloudWatch Logs。或者，您可以將資料傳送至本機帳戶中的帳戶特定 S3 儲存貯體。 | 一般 AWS | 

## 相關資源
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts-resources"></a>
+ [如何使用集中式流程日誌資料來促進資料分析和滿足安全需求 ](https://aws.amazon.com/blogs/security/how-to-facilitate-data-analysis-and-fulfill-security-requirements-by-using-centralized-flow-log-data/)(AWS 部落格文章）
+ [如何使用 AWS Config 規則自動啟用 VPC 流程日誌 ](https://aws.amazon.com/blogs/mt/how-to-enable-vpc-flow-logs-automatically-using-aws-config-rules/)(AWS 部落格文章）

## 其他資訊
<a name="configure-vpc-flow-logs-for-centralization-across-aws-accounts-additional"></a>

**儲存貯體政策**

在您新增預留位置名稱的值之後，此儲存貯體政策範例可以套用至流量日誌的中央 S3 儲存貯體。

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AWSLogDeliveryWrite",
            "Effect": "Allow",
            "Principal": {
                "Service": "delivery.logs.amazonaws.com"
            },
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::<BUCKET_NAME>/*",
            "Condition": {
                "StringEquals": {
                    "s3:x-amz-acl": "bucket-owner-full-control",
                    "aws:SourceOrgID": "<ORG_ID>"
                }
            }
        },
        {
            "Sid": "AWSLogDeliveryCheck",
            "Effect": "Allow",
            "Principal": {
                "Service": "delivery.logs.amazonaws.com"
            },
            "Action": "s3:GetBucketAcl",
            "Resource": "arn:aws:s3:::<BUCKET_NAME>",
            "Condition": {
                "StringEquals": {
                    "aws:SourceOrgID": "<ORG_ID>"
                }
            }
        },
        {
            "Sid": "DenyUnencryptedTraffic",
            "Effect": "Deny",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::<BUCKET_NAME>/*",
                "arn:aws:s3:::<BUCKET_NAME>"
            ],
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                }
            }
        }
    ]
}
```

# 使用 Terraform 動態管理 AWS 許可集
<a name="manage-aws-permission-sets-dynamically-by-using-terraform"></a>

*Vinicius Elias 和 Marcos Vinicius Pinto Jordao，Amazon Web Services*

## 摘要
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-summary"></a>

AWS IAM Identity Center enhances AWS Identity and Access Management (IAM) 提供集中式中樞，用於管理 AWS 帳戶 和雲端應用程式的單一登入存取。不過，隨著組織的成長，手動管理 IAM Identity Center [許可集](https://docs.aws.amazon.com/singlesignon/latest/userguide/permissionsetsconcept.html)可能會變得越來越複雜且容易出錯。這種複雜性可能會導致潛在的安全漏洞和管理開銷。

此解決方案可讓您使用以原生建置的持續整合和持續交付 (CI/CD) 管道，透過基礎設施將許可集管理為程式碼 (IaC) AWS 服務。它可讓許可集指派機制與 AWS Control Tower 生命週期事件或 [Account Factory for Terraform (AFT)](https://docs.aws.amazon.com/controltower/latest/userguide/aft-overview.html) 環境無縫整合。此方法為新的和現有的 提供動態身分組態 AWS 帳戶。

Amazon EventBridge 規則會監控 AWS 帳戶 建立和更新，這有助於您的身分組態與您的組織結構保持同步。在 或 AFT 中建立 AWS Control Tower 或更新帳戶之後，就會觸發管道。它使用許可集定義和指派規則來評估一組 JSON 檔案。然後，管道會套用並同步所有帳戶的設定。

此方法提供下列優勢：
+ **一致性** – 消除整個 AWS 組織的手動組態偏離
+ 可**稽核性** – 維護所有身分管理變更的完整歷史記錄
+ **可擴展性** – 隨著 AWS 環境的成長，自動套用組態
+ **安全性** – 減少許可指派中的人為錯誤
+ **合規** – 透過記錄的變更和指派規則，協助滿足法規要求

## 先決條件和限制
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-prereqs"></a>
+ 具有 AWS Control Tower 和 AWS Organizations 設定的多帳戶環境。或者，您可以搭配 使用 AFT AWS Control Tower。
+  AWS 帳戶 接收解決方案的 IAM Identity Center 委派管理員。如需詳細資訊，請參閱 IAM Identity Center 文件中的[委派管理](https://docs.aws.amazon.com/singlesignon/latest/userguide/delegated-admin.html)。
+ 處理主要程式碼的版本控制系統 (VCS) 儲存庫。如需範例，請參閱解決方案的 GitHub [儲存庫](https://github.com/aws-samples/sample-terraform-aws-permission-sets-pipeline/tree/main/samples/basic)。
+ Terraform 後端管理的必要 AWS 資源，例如 Amazon Simple Storage Service (Amazon S3) 儲存貯體和 Amazon DynamoDB 資料表。

**限制**
+ 管道使用 AWS 原生資源和開放原始碼 Terraform。管道尚未準備好呼叫第三方生態系統。
+ 有些 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)，然後選擇服務的連結。

## Architecture
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-architecture"></a>

下圖顯示此模式的元件和工作流程。

![\[使用 Terraform 管理 AWS 許可集的元件和工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/69dc79c7-b4cd-4ad0-b0d2-d58cf0c7adaa/images/649e299c-1142-405a-8982-4a6b2e595d53.png)


**AWS Control Tower 事件流程**

解決方案從整合來自 AWS Control Tower 或 AFT 的事件開始。在實作時間，會透過變數定義，選擇一項或另一項服務。無論使用何種方法，只要建立或更新帳戶，就會觸發管道。管道會協調存放在許可集管理儲存庫中的政策。

以下是 AWS Control Tower 生命週期事件：
+ `CreateManagedAccount` – 建立新帳戶時
+ `UpdateManagedAccount` – 更新現有帳戶時

**事件路由**

EventBridge 做為中央事件處理服務，擷取 AWS Control Tower 帳戶中產生的事件。事件發生時，EventBridge 會以智慧方式將事件路由到解決方案帳戶中的集中式事件匯流排。 AWS Control Tower 生命週期事件遵循不同的路由模式。如果 AFT 定義為事件來源，則 AFT 管理帳戶會處理事件，而不是 AWS Control Tower 帳戶。此事件驅動型架構可自動回應組織變更，無需手動介入。

**AFT 整合程序**

當 AWS Control Tower 生命週期事件到達 AFT 管理帳戶時，它們會自動觸發多個 AFT 內部的下游程序。AFT 帳戶自訂工作流程完成後，它會將訊息發佈至專用 `aft-notifications` Amazon Simple Notification Service (Amazon SNS) 主題。該主題會觸發此解決方案實作的 `aft-new-account-forward-event` AWS Lambda 函數。Lambda 函數會將事件傳送至解決方案帳戶事件匯流排，用於啟動管道。

**基礎設施即程式碼管道**

解決方案管道以全自動化的部署機制運作。 AWS CodePipeline 服務會持續監控儲存庫的變更。偵測到新的遞交時，會自動啟動部署工作流程，並啟動包含驗證和執行階段的循序程序。系統會執行 Terraform `plan`操作來識別提議的變更，接著執行 Terraform `apply`命令以在 AWS 環境中實作這些變更。值得注意的是，管道在沒有任何手動核准閘道的情況下執行。此方法可讓您快速部署基礎設施變更，同時透過管道日誌和 Terraform 狀態檔案維持可稽核性。

管道會利用 AWS CodeBuild ，在具有適當許可的受控環境中執行 Terraform 操作。透過此 IaC 方法，管道可以執行全面的許可管理操作，包括：
+ 建立新的許可集。
+ 更新現有的許可集。
+ 移除不必要的許可集。
+ 管理組織中帳戶和群組之間這些許可的指派 AWS 。

為了維持基礎設施一致性並防止發生衝突的變更，解決方案會使用 Amazon S3 儲存貯體和專用 Amazon DynamoDB 資料表實作 Terraform 後端狀態管理系統。此方法為 Terraform 狀態檔案和狀態鎖定機制提供持久性儲存位置，以防止同時修改相同的資源。

主要 Terraform 程式碼使用官方 AWS `permission-sets` Terraform 模組。此模組可以根據許可集範本，在 IAM Identity Center 中動態管理許可集。

**來源控制管理**

許可集範本 (JSON 檔案） 位於外部版本控制系統中，例如 GitHub，為身分管理組態提供集中式儲存庫。此方法為許可集定義建立單一事實來源，同時透過標準程式碼檢閱實務進行協作開發。授權使用者可以在組織變更管理程序之後，將變更遞交至這些範本。這些遞交可做為自動化部署管道的主要觸發條件，啟動基礎設施更新程序。

如需如何使用儲存庫中的 JSON 檔案設定許可集的範例，請參閱[其他資訊](#manage-aws-permission-sets-dynamically-by-using-terraform-additional)。

## 工具
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-tools"></a>

**AWS 服務**
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一種全受管建置服務，可協助您編譯原始程式碼、執行單元測試，並產生準備好部署的成品。
+ [AWS CodeConnections](https://docs.aws.amazon.com/dtconsole/latest/userguide/welcome-connections.html) 可讓 CodePipeline 等 AWS 資源和服務連線至外部程式碼儲存庫，例如 GitHub。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可協助您快速建模和設定軟體版本的不同階段，並自動化持續發行軟體變更所需的步驟。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您 AWS 服務 透過命令列 Shell 中的命令與 互動。
+ [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html) 可協助您設定和管理 AWS 多帳戶環境，並遵循規範最佳實務。
+ [Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) 是一項全受管 NoSQL 資料庫服務，可提供快速、可預期且可擴展的效能。
+ [Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html) 是一種無伺服器事件匯流排服務，可協助您將應用程式與來自各種來源的即時資料連線。例如， AWS Lambda 函數、使用 API 目的地的 HTTP 調用端點，或其他事件匯流排 AWS 帳戶。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html) 可協助您集中管理所有 AWS 帳戶 和雲端應用程式的單一登入 (SSO) 存取。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可協助您將多個 合併 AWS 帳戶 到您建立並集中管理的組織。
+ [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可協助您協調和管理發佈者和用戶端之間的訊息交換，包括 Web 伺服器和電子郵件地址。它可啟用帳戶管理事件的推播通知，確保相關各方知道系統中的重要變更或動作。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。

**其他工具**
+ [Terraform](https://www.terraform.io/) 是 HashiCorp 的基礎設施即程式碼 (IaC) 工具，可協助您建立和管理雲端和內部部署資源。

**程式碼儲存庫**

此模式的程式碼可在 AWS sample[sample-terraform-aws-permission-sets-pipeline](https://github.com/aws-samples/sample-terraform-aws-permission-sets-pipeline)儲存庫的 GitHub 上的 Samples 組織中取得。

## 最佳實務
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-best-practices"></a>
+ 一律鎖定用於在生產環境中執行程式碼的 Terraform 模組和提供者版本。
+ 使用靜態程式碼分析工具，例如 [Checkov](https://www.checkov.io/)，掃描您的程式碼，然後解決安全問題。
+ 遵循最低權限原則，並授予執行任務所需的最低許可。如需詳細資訊，請參閱 IAM 文件中的[授予最低權限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#grant-least-priv)和[安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

## 史詩
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-epics"></a>

### 建立先決條件 （選用）
<a name="create-the-prerequisites-optional"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 Terraform 後端資源。 | 如果您尚未建立 Terraform 後端 AWS 資源，請使用下列步驟來建立 Amazon S3 儲存貯體 (`s3-tf-backend-{ACCOUNT_ID}`) 和 DynamoDB 資料表 ()`ddb-tf-backend`。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html)<pre>aws s3api create-bucket --bucket s3-tf-backend-{ACCOUNT_ID}<br />aws s3api put-bucket-versioning --bucket s3-tf-backend-{ACCOUNT_ID} --versioning-configuration Status=Enabled<br />aws dynamodb create-table --table-name ddb-tf-backend --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1</pre> | AWS 管理員 | 
| 建立跨帳戶角色。 | 您必須在 `event-source-account` Terraform AWS 提供者組態中提供跨帳戶 IAM 角色。如果您尚未建立此角色，請使用下列步驟來建立角色：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html)<pre>aws iam create-role \<br />    --role-name CrossAccountRole \<br />    --assume-role-policy-document '{<br />        "Version": "2012-10-17",		 	 	 <br />        "Statement": [<br />            {<br />                "Effect": "Allow",<br />                "Principal": {<br />                    "AWS": "arn:aws:iam::{ACCOUNT_ID}:root"<br />                },<br />                "Action": "sts:AssumeRole"<br />            }<br />        ]<br />    }'</pre>[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html)<pre>aws iam attach-role-policy \<br />    --role-name CrossAccountRole \<br />    --policy-arn arn:aws:iam::aws:policy/AdministratorAccess</pre>此範例使用 AWS 受管 IAM 政策 [AdministratorAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AdministratorAccess.html)。如果您願意，可以使用更具體的政策。 | AWS 管理員 | 

### 準備許可集儲存庫
<a name="prepare-the-permission-set-repository"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立專用儲存庫。 | 此任務假設您使用的是 GitHub。建立專用儲存庫來存放主要 Terraform 程式碼和許可集範本 JSON 檔案。 | DevOps 工程師 | 
| 準備許可集程式碼。 | 如需如何建構下列檔案的資訊，請參閱解決方案儲存庫上的[範例程式碼](https://github.com/aws-samples/sample-terraform-aws-permission-sets-pipeline/tree/main/samples/basic)：├ – main.tf：//├ – outputs.tf：//├ – provider.jinja└ — 範本複製內容、保留`providers.jinja`值，並對其他檔案進行必要的調整。例如，將許可集範本檔案新增至 檔案，`templates`或將`aws-ia/permission-sets/aws`模組版本釘選在 `main.tf` 檔案中。 | DevOps 工程師 | 
| 遞交您的變更。 | 遞交變更並推送至您先前建立的儲存庫。儲存儲存庫名稱及其 GitHub 組織，例如 `myorg/aws-ps-pipeline`。 | DevOps 工程師 | 

### 準備部署程式碼
<a name="prepare-the-deployment-code"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 下載內容。 | 從解決方案[儲存庫](https://github.com/aws-samples/sample-terraform-aws-permission-sets-pipeline)下載 （複製） 內容。 | DevOps 工程師 | 
| 履行變數。 | 建立`terraform.tfvars`檔案並新增下列必要變數：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html)<pre>repository_name                 = "myorg/aws-ps-pipeline"<br />branch_name                     = "main"<br />vcs_provider                    = "github"<br />account_lifecycle_events_source = "CT"</pre>如需其他變數選項的詳細資訊，請參閱此模式的 GitHub 儲存庫中的 [variables.tf](https://github.com/aws-samples/sample-terraform-aws-permission-sets-pipeline/blob/main/variables.tf) 檔案。 | DevOps 工程師 | 
| 調整 Terraform 後端組態。 | 在 `backend.tf` 檔案中，將預留位置取代為您自己的值。使用 AWS Control Tower 首頁 AWS 區域，並提供先前建立的 Amazon S3 儲存貯體和 DynamoDB 資料表的名稱。<pre>terraform {<br />  required_version = ">=1.6"<br />  backend "s3" {<br />    region         = "{region}"<br />    bucket         = "{bucket_name}"<br />    key            = "terraform.tfstate"<br />    dynamodb_table = "{table_name}"<br />    encrypt        = "true"<br />  }<br />}</pre>如果您願意，可以使用自己的 Terraform 後端組態。 | DevOps 工程師 | 
| 調整 Terraform 提供者組態。 | 在 `providers.tf` 檔案中，將預留位置取代為您自己的資訊。使用 AWS Control Tower 主區域，並提供先前為`event-source-account`提供者建立的跨帳戶 IAM 角色的 ARN。<pre>provider "aws" {<br />  region = "{region}"<br />}<br /><br />provider "aws" {<br />  alias  = "event-source-account"<br />  region = "{region}"<br />  assume_role {<br />    role_arn = "{role_arn}"<br />  }<br />}<br /></pre> | DevOps 工程師 | 

### 手動部署解決方案
<a name="deploy-the-solution-manually"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 選取 AWS 帳戶。 | 我們建議您在 IAM Identity Center 委派管理員帳戶中部署解決方案。不過，您也可以在 AWS Organizations 管理帳戶中部署它。若要登入與 IAM Identity Center 執行個體位於相同區域中的所選帳戶，請使用 AWS CLI。請確定您使用的 IAM 角色具有許可，可擔任先前步驟中為`event-source-account`提供者指定的角色。此外，此角色必須能夠存取 Terraform 後端組態中使用的 AWS 資源。 | AWS 管理員 | 
| 手動執行 Terraform。 | 若要初始化、規劃和套用組態，請依所示順序執行下列 Terraform 命令：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html) | DevOps 工程師 | 
| 檢查部署結果。 | 在 IAM Identity Center 委派管理員帳戶中，檢查`aws-ps-pipeline`管道是否已建立。同時檢查是否有處於**待定**狀態的 AWS CodeConnections 連線。 | AWS DevOps | 
| 完成 CodeConnections 組態。 | 若要完成 CodeConnections 組態，請使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html)管道現在應該可以存取許可集儲存庫。如需詳細說明，請參閱開發人員工具主控台文件中的[更新待定連線](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-update.html)。 | AWS DevOps | 

### 選擇管道執行流程以測試解決方案
<a name="choose-a-pipeline-execution-flow-to-test-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 依 AWS Control Tower 或 AFT 更新執行管道。 | 使用 AWS Control Tower 或 AFT 建立或變更帳戶後 （取決於您選擇的生命週期事件類型），管道就會啟動。 | AWS 管理員 | 
| 變更程式碼以執行管道。 | 在您變更程式碼並將其遞交至`main`分支之後，管道就會啟動。 | AWS DevOps | 
| 手動執行管道。 | 若要手動啟動管道，請使用 中的[發行變更](https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-rerun-manually.html)功能 AWS CodePipeline。 | AWS DevOps | 

## 疑難排解
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 存取遭拒 | 確認您具有部署解決方案所需的許可。 | 
| CodeConnections 問題 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html) | 
| 管道執行問題 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html) | 
| 許可集部署問題 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/manage-aws-permission-sets-dynamically-by-using-terraform.html) | 

## 相關資源
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-resources"></a>

**AWS 服務 文件**
+ [AWS IAM Identity Center 使用者指南](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html)
+ [AWS 帳戶 使用許可集管理](https://docs.aws.amazon.com/singlesignon/latest/userguide/permissionsetsconcept.html) (IAM Identity Center 文件）

**其他資源**
+ [AWS 許可集模組 ](https://registry.terraform.io/modules/aws-ia/permission-sets/aws/latest)(Terraform)

## 其他資訊
<a name="manage-aws-permission-sets-dynamically-by-using-terraform-additional"></a>

**具有範例許可集的 JSON 檔案**

下列範例示範如何使用儲存庫中的 JSON 檔案來設定許可集：

```
{
  "Name": "ps-billing", // Permission set identifier
  "Comment": "Sample permission set for billing access", // Comment to document the purpose of the permission set
  "Description": "Billing access in AWS", // Detailed description
  "SessionDuration": "PT4H", // Session duration = 4 hours (ISO 8601 format)
  "ManagedPolicies": [ // List of AWS IAM managed policies
    "arn:aws:iam::aws:policy/job-function/Billing",
    "arn:aws:iam::aws:policy/job-function/SupportUser",
    "arn:aws:iam::aws:policy/AWSSupportAccess",
    "arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"
  ],
  "CustomerPolicies": [], // References to IAM policies previously created
  "CustomPolicy": {}, // Inline IAM policy defined directly in the permission set
  "PermissionBoundary": {  // AWS or customer managed IAM policy to be used as boundary
    "ManagedPolicy": "",
    "CustomerPolicy": ""
  },
  "Assignments": [ // Define the assignment rules
    {
      "all_accounts": true, // Apply to ALL active AWS accounts in organization
      "principal": "G_BILLING_USERS", // Group/user name in Identity Center
      "type": "GROUP", // Can be "GROUP" or "USER"
      "account_id": [], // List of AWS account ID (empty since all_accounts=true)
      "account_ou": [], // List of AWS Organizational Unit IDs with target AWS accounts
      "account_tag": [] // List of tags (key:value) to match AWS Organization accounts tags
    }
  ]
}
```

如需詳細資訊，請參閱 Terraform 網站上的[AWS 許可集模組](https://registry.terraform.io/modules/aws-ia/permission-sets/aws/latest#json-file-templates)文件中的 JSON 結構描述。

**提示**
+ 您可以使用 Terraform [匯入區塊](https://developer.hashicorp.com/terraform/language/import)，將現有的許可集匯入解決方案。
+ 您可以使用 AFT 在委派帳戶中實作 AWS 許可集管道。如需詳細資訊，請參閱 [AFT 藍圖](https://awslabs.github.io/aft-blueprints/index.html)。

# 使用 AWS Organizations 自動標記 Transit Gateway 連接
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations"></a>

*Richard Milner-Watts、Haris Bin Ayub 和 John Capps，Amazon Web Services*

## 總結
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-summary"></a>

在 Amazon Web Services (AWS) 上，您可以使用 [AWS Resource Access Manager](https://aws.amazon.com/ram/) 跨[AWS Transit Gateway](https://aws.amazon.com/transit-gateway/) AWS 帳戶 邊界共用。不過，當您跨帳戶邊界建立 Transit Gateway 連接時，會建立沒有名稱標籤的連接。這可能會讓識別連接耗時。 

此解決方案提供自動化機制，可收集由 管理之組織內帳戶的每個 Transit Gateway 連接的相關資訊[AWS Organizations](https://aws.amazon.com/organizations/)。程序包括從 Transit Gateway [路由表中查詢無類別網域間](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing)路由 (CIDR) 範圍。解決方案接著會將 形式的 Name 標籤`<CIDR-range>-<AccountName>`套用至擁有傳輸閘道之帳戶內的附件。

此解決方案可與解決方案程式庫中的 [Serverless Transit Network Orchestrator](https://aws.amazon.com/solutions/implementations/serverless-transit-network-orchestrator/) 等 AWS 解決方案搭配使用。Serverless Transit Network Orchestrator 可大規模自動建立 Transit Gateway 附件。

## 先決條件和限制
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 包含所有相關帳戶 AWS Organizations 的組織
+ 在組織的根目錄下存取組織管理帳戶，以建立 required AWS Identity and Access Management (IAM) 角色
+ 共用網路成員帳戶，其中包含與組織共用並具有附件的一或多個傳輸閘道

## Architecture
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-architecture"></a>

的下列螢幕擷取畫面 AWS 管理主控台 顯示沒有相關聯名稱標籤的 Transit Gateway 附件範例，以及具有此解決方案產生之名稱標籤的兩個 Transit Gateway 附件範例。產生的名稱標籤結構為 `<CIDR-range>-<AccountName>`。

![\[主控台顯示不含名稱標籤的附件，以及兩個含名稱標籤的附件。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/4b10dfec-43be-4337-9945-c64df921934a/images/7e7d4a47-f07a-4708-8022-a1d22855bb5d.png)


 

此解決方案使用 [AWS CloudFormation](https://aws.amazon.com/cloudformation/) 部署[AWS Step Functions](https://aws.amazon.com/step-functions/)工作流程，管理所有已設定之 Transit Gateway Name 標籤的建立 AWS 區域。工作流程會叫用 [AWS Lambda](https://aws.amazon.com/lambda/)函數，以執行基礎任務。

解決方案從中取得帳戶名稱後 AWS Organizations，Step Functions 狀態機器會取得所有 Transit Gateway 連接 IDs。這些是由區域平行處理。此處理包括查詢每個附件的 CIDR 範圍。CIDR 範圍是透過搜尋區域內的 Transit Gateway 路由表來取得相符的 Transit Gateway 連接 ID。如果所有必要資訊都可用，解決方案會將名稱標籤套用至附件。解決方案不會覆寫任何現有的名稱標籤。

解決方案會按照由 [Amazon EventBridge](https://aws.amazon.com/eventbridge/) 事件控制的排程執行。事件會在 UTC 每天上午 6：00 啟動解決方案。

**目標技術堆疊**
+ Amazon EventBridge
+ AWS Lambda
+ AWS Organizations
+ AWS Transit Gateway
+ Amazon Virtual Private Cloud (Amazon VPC)
+ AWS X-Ray

**目標架構**

下圖顯示解決方案架構和工作流程。

![\[跨共用聯網和組織管理帳戶的九個步驟程序。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/4b10dfec-43be-4337-9945-c64df921934a/images/873cc89f-c6e3-43cd-94ed-59b6ea2b8d49.png)


1. 排程事件會啟動規則。

1. EventBridge 規則會啟動 Step Functions 狀態機器。

1. 狀態機器會叫用 `tgw-tagger-organizations-account-query` Lambda 函數。

1. `tgw-tagger-organizations-account-query` Lambda 函數會擔任組織管理帳戶中的角色。

1. `tgw-tagger-organizations-account-query` Lambda 函數會呼叫 Organizations API 來傳回 AWS 帳戶 中繼資料。

1. 狀態機器會叫用 `tgw-tagger-attachment-query` Lambda 函數。

1. 對於每個區域，狀態機器會並行叫用 `tgw-tagger-rtb-query` Lambda 函數來讀取每個附件的 CIDR 範圍。

1. 對於每個區域，狀態機器會平行叫用 `tgw-tagger-attachment-tagger`****Lambda 函數。

1. 系統會為共用網路帳戶中的 Transit Gateway 附件建立名稱標籤。

**自動化和擴展**

解決方案會平行處理每個區域，以減少執行的總持續時間。

## 工具
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-tools"></a>

**AWS 服務**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 透過將基礎設施視為程式碼，提供建立相關 AWS 和第三方資源集合模型、快速一致地佈建它們，以及在整個生命週期中管理它們的方法。
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 可協助您 AWS 即時監控 AWS 資源的指標，以及您執行的應用程式。
+ [Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html) 是一種無伺服器事件匯流排服務，可用來將應用程式與來自各種來源的資料連線。EventBridge 會收到事件、環境變更的指標，並套用規則將事件路由至目標。規則會根據事件的結構、稱為事件模式或排程，將事件與目標配對。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一種運算服務，支援執行程式碼，無需佈建或管理伺服器。Lambda 只會在需要時執行程式碼，並自動擴展，從每天幾個請求擴展到每秒數千個請求。您只需為使用的運算時間支付費用。程式碼未執行時無須付費。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 隨著資源的成長和擴展， 可協助您集中管理和控管您的環境 AWS 。使用 Organizations，您可以透過程式設計方式建立新的資源 AWS 帳戶 並配置資源、將 帳戶分組以組織您的工作流程、將政策套用到帳戶或群組以進行控管，以及為所有帳戶使用單一付款方式來簡化計費。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html) 是一種低程式碼視覺化工作流程服務，用於協調 AWS 服務、自動化業務流程和建置無伺服器應用程式。工作流程會管理故障、重試、平行化、服務整合和可觀測性，讓開發人員可以專注於更高價值的商業邏輯。
+ [AWS Transit Gateway](https://aws.amazon.com/transit-gateway/) 透過中央中樞連接 VPCs 和內部部署網路。這可簡化您的網路，並結束複雜的互連關係。它充當雲端路由器，因此每個新連線只會進行一次。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 是一種在您定義的邏輯隔離虛擬網路中啟動 AWS 資源的服務。
+ [AWS X-Ray](https://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html) 會收集應用程式提供的請求相關資料，並提供可用來檢視、篩選和深入了解該資料的工具，以識別問題和最佳化的機會。

**Code**

此解決方案的原始程式碼可在 [Transit Gateway Attachment Tagger](https://github.com/aws-samples/tgw-attachment-tagger) GitHub 儲存庫中使用。儲存庫包含下列檔案：
+ `tgw-attachment-tagger-main-stack.yaml` 會在共用網路帳戶中建立所有資源以支援此解決方案。
+ `tgw-attachment-tagger-organizations-stack.yaml`****在組織的管理帳戶中建立角色。

## 史詩
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-epics"></a>

### 部署主要解決方案堆疊
<a name="deploy-the-main-solution-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 收集必要的先決條件資訊。 | 若要設定從 Lambda 函數到 AWS Organizations API 的跨帳戶存取權，您需要組織管理帳戶的帳戶 ID。****建立兩個 CloudFormation 堆疊的順序很重要。您必須先將資源部署到共用網路帳戶。在將資源部署到組織的管理帳戶中之前，共用網路帳戶中的角色必須已存在。如需詳細資訊，請參閱 [AWS 文件](https://docs.amazonaws.cn/en_us/IAM/latest/UserGuide/id_roles_create_for-user.html)。 | DevOps 工程師 | 
| 啟動主要解決方案堆疊的 CloudFormation 範本。 | 主要解決方案堆疊的範本將部署 IAM 角色、Step Functions 工作流程、Lambda 函數和 Amazon CloudWatch 事件。開啟 AWS 管理主控台 共用網路帳戶的 ，然後開啟 ：&CFN 主控台。 使用`tgw-attachment-tagger-main-stack.yaml` 範本和下列值建立堆疊： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/tag-transit-gateway-attachments-automatically-using-aws-organizations.html)如需啟動 CloudFormation 堆疊的詳細資訊，請參閱 [AWS 文件](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)。 | DevOps 工程師 | 
| 確認解決方案已成功啟動。 | 等待 CloudFormation 堆疊達到 **CREATE\$1COMPLETE** 狀態。這應該需要不到一分鐘的時間。開啟 Step Functions 主控台，並確認已建立名為 **tgw-attachment-tagger-state-machine** 的新狀態機器。 | DevOps 工程師 | 

### 部署 AWS Organizations 堆疊
<a name="deploy-the-aws-organizations-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 收集必要的先決條件資訊。 | 若要設定從 Lambda 函數到 AWS Organizations API 的跨帳戶存取權，您需要共用網路帳戶的帳戶 ID。 | DevOps 工程師 | 
| 啟動 Organizations 堆疊的 CloudFormation 範本 | AWS Organizations 堆疊的範本將在組織的管理帳戶中部署 IAM 角色。 存取組織管理帳戶的 AWS 主控台。然後開啟 CloudFormation 主控台。 使用`tgw-attachment-tagger-organizations-stack.yaml` 範本和下列值建立堆疊：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/tag-transit-gateway-attachments-automatically-using-aws-organizations.html)對於其他堆疊建立選項，請使用預設值。 | DevOps 工程師 | 
| 確認解決方案已成功啟動。 | 等待 CloudFormation 堆疊達到 **CREATE\$1COMPLETE** 狀態。這應該需要不到一分鐘的時間。開啟 AWS Identity and Access Management (IAM) 主控台，並確認已建立名為 **tgw-attachment-tagger-organization-query-role** 的新角色。 | DevOps 工程師 | 

### 驗證解決方案
<a name="verify-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行狀態機器。 | 開啟共用網路帳戶的 Step Functions 主控台，然後在導覽窗格中選擇**狀態機器**。選取狀態機器 **tgw-attachment-tagger-state-machine**，然後選擇**開始執行**。 由於解決方案不會使用此狀態機器的輸入，因此您可以使用預設值。<pre>{<br />    "Comment": "Insert your JSON here"<br />}</pre>選擇 **Start Execution (開始執行)**。 | DevOps 工程師 | 
| 觀看狀態機器直到完成。 | 在開啟的新頁面上，您可以觀看狀態機器執行。持續時間取決於要處理的 Transit Gateway 附件數量。在此頁面上，您可以檢查狀態機器的每個步驟。您可以在狀態機器中檢視各種任務，並遵循 Lambda 函數的 CloudWatch 日誌連結。對於在映射中平行執行的任務，您可以使用**索引**下拉式清單來檢視每個區域的特定實作。 | DevOps 工程師 | 
| 驗證 Transit Gateway 連接標籤。 | 開啟共用網路帳戶的 VPC 主控台，然後選擇**傳輸閘道附件**。 在 主控台上，為符合條件的附件提供名稱標籤 （附件會傳播到 Transit Gateway 路由表，而資源擁有者是組織的成員）。 | DevOps 工程師 | 
| 驗證 CloudWatch 事件啟動。 | 等待 CloudWatch 事件啟動。這是排程為 06：00 UTC。 然後開啟共用網路帳戶的 Step Functions 主控台，然後在導覽窗格中選擇**狀態機器**。選取狀態機器 **tgw-attachment-tagger-state-machine**。確認解決方案在 UTC 的 06：00 執行。 | DevOps 工程師 | 

## 相關資源
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations-resources"></a>
+ [AWS Organizations](https://aws.amazon.com/organizations/)
+ [AWS Resource Access Manager](https://aws.amazon.com/ram/)
+ [無伺服器傳輸網路協調器](https://aws.amazon.com/solutions/implementations/serverless-transit-network-orchestrator/)
+ [建立 IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html)
+ [在 AWS CloudFormation 主控台上建立堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)

# 更多模式
<a name="cloudfoundations-more-patterns-pattern-list"></a>

**Topics**
+ [AWS 帳戶 使用 AFT 自動化新 的 Amazon VPC IPAM IPv4 CIDR 配置](automate-amazon-vpc-ipam-ipv4-cidr-allocations-for-new-aws-accounts-by-using-aft.md)
+ [使用 Account Factory for Terraform 管理多個帳戶的許可集](govern-permission-sets-aft.md)
+ [使用 GitHub 動作根據 AWS CloudFormation 範本佈建 AWS Service Catalog 產品](provision-aws-service-catalog-products-using-github-actions.md)
+ [透過部署角色販賣機解決方案來佈建最低權限的 IAM 角色](provision-least-privilege-iam-roles-by-deploying-a-role-vending-machine-solution.md)