

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

# 云基础
<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 自动标记中转网关连接](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*

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

此模式说明了如何在授权用户提交请求 AWS 账户 时使用 AWS解决方案[上的着陆区加速器](https://aws.amazon.com/solutions/implementations/landing-zone-accelerator-on-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) [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_install)
+ 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 账户。此限制可确保账户创建工作流已成功完成，而无需在并行运行期间争夺资源。

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

**目标架构**

下图显示了使用着陆区加速器自动创建新 AWS 账户 版本的高级架构。 AWS AWS Step Functions 协调自动化。Step Functions 工作流程中的每项任务都由一个或多个 AWS Lambda 函数执行。

![\[使用 AWS 登录区加速器自动创建新账户的工作流。\]](http://docs.aws.amazon.com/zh_cn/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. 账户创建 Orchestrator 工作流从 AWS Step Functions中启动。

1. 该工作流更新源代码存储库中的 `account-config.yaml` 文件。它还会在 AWS 管道上启动着陆区加速器并检查管道的状态。此管道创建并设置新账户。有关其工作原理的更多信息，请参阅 AWS登录区加速器的[架构概述](https://docs.aws.amazon.com/solutions/latest/landing-zone-accelerator-on-aws/architecture-overview.html)。

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 Parameter Store](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 工作流图表*。

**Microft Entra ID 应用程序**

如果您选择与 Microsoft Entra ID 集成，则在部署此模式时将创建以下两个应用程序：
+ 链接到 IAM Identity Center 并确保 Microsoft Entra ID 组在 IAM Identity Center 可用的应用程序。在本示例中，这个 Microsoft Entra ID 应用程序命名为 `LZA2`。
+ [允许 Lambda 函数与微软 Entra ID 通信并调用微软 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) 可帮助您创建、发布、维护、监控和保护任何规模的 RES WebSocket APIs T、HTTP。在此模式中，您可以使用 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 账户目的地的事件总线。此解决方案使用的[EventBridge 规则](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rules.html)是，如果 Step Functions 工作流程状态更改为`Failed``Timed-out`、或，则启动 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 S3) Simple Service 中的数据、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 Parameter Store](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](https://learn.microsoft.com/en-us/graph/graph-explorer/graph-explorer-overview) 可 APIs帮助你访问微软云服务（例如微软 Entra 和微软 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) — 此层包括用于担任角色和检查进度的模块。 AWS Service Catalog
+ [boto3](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_layer/boto3) — 此层包含[适用于 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 在 Parameter Store 中创建参数。每个参数都以前缀 `/account/tags/` 开头。
+ [AttachPermissionSet](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/AttachPermissionSet)— 此函数向 IAM 身份中心群组添加权限集。
+ [Azure ADGroup 同步](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/AzureADGroupSync) — 此功能将目标 Microsoft Entra ID 组同步到 IAM 身份中心。
+ [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)— 此功能可创建不由着陆区加速器管理的 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) — 此功能向正在等待账户创建完成的用户发送电子邮件。
+ [验证 ADGroup SyncTo SSO](https://github.com/aws-samples/lza-account-creation-workflow/tree/main/app/lambda_src/stepfunction/ValidateAdGroupSyncToSSO) — 此功能检查指定的 Microsoft Entra ID 组是否与 IAM 身份中心同步。
+ [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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 准备开启着陆区域加速器 AWS 以进行自定义。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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_cn/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_cn/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 
| 准备部署 `config-log-validation-role` 角色。 | 现在，您可以自定义解决方案，以便在日志存档账户中部署 `config-log-validation-role` IAM 角色。此角色允许 `ValidateResources` Lambda 函数访问 Amazon S3 存储桶以获取日志记录和访问 AWS Config 规则。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 

### （可选）从 Microsoft Entra ID 获取数据
<a name="optional-get-data-from-microsoft-entra-id"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建允许 Lambda 函数与 Microsoft Entra ID 通信的应用程序。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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_cn/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_cn/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_cn/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 

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


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 克隆源代码。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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_cn/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_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 确定要使用哪些参数。 | 选择在运行启动 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_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 为 awscurl 设置变量。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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_cn/prescriptive-guidance/latest/patterns/automate-account-creation-lza.html) | AWS DevOps | 

### （可选）清理解决方案
<a name="optional-clean-up-the-solution"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 从 Amazon S3 存储桶中移除对象。 | 移除以下 Amazon S3 存储桶中的所有对象：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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_cn/prescriptive-guidance/latest/patterns/images/pattern-img/d31abfaa-6854-4923-b896-3b817de9f4d9/images/d93aa7bf-1144-4f25-9488-aacc534a7813.png)


**参数**

下面是运行启动 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)`****（字符串）-新的 root 用户的电子邮件地址 AWS 账户。
**注意**  
如果不使用此参数，则将使用 `configs/deploy-config.yaml` 文件中的值 `rootEmailPrefix` 和 `rootEmailDomain` 生成电子邮件地址。如果未提供电子邮件地址，则使用以下格式生成电子邮件地址：`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*

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

这种模式概述了一种自动化方法，用于维护多个帐户的全面 AWS 资源清单，以及 AWS 区域. 它旨在帮助基础设施和安全工程师改善其资源管理实践。它用于跟踪资源变化，Amazon Athena 用于查询，Amazon Quick Sight 用于跟踪 AWS Config 交互式仪表板。您可以通过部署 AWS CloudFormation 堆栈来实现此解决方案。

此解决方案与[使用 Amazon Athena 和 Amazon Qu AWS ick Sight 可视化 AWS Config 数据（博](https://aws.amazon.com/blogs/mt/visualizing-aws-config-data-using-amazon-athena-and-amazon-quicksight/)客文章）中介绍的解决方案类似。此模式扩展了该解决方案，以满足以下常见要求并提供以下主要优势：
+ **聚焦合规性** – 这种方法可以帮助您满足监管要求，例如 [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 S3) [S](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html) ervice 存储桶，用于存储来自聚合器的数据 AWS Config 
  + Amazon Quick [订阅](https://docs.aws.amazon.com/quicksight/latest/user/signing-up.html)
  + Quick Sight 和亚马逊 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
+ Quick 访问权限

**限制**
+ 解决方案依赖于 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)页面，然后选择该服务的链接。

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

下图显示了收集、组织、分析和可视化组织中多个账户的配置和合规性数据的简化流程。 AWS 

![\[收集并可视化整个组织中的配置和合规性数据。\]](http://docs.aws.amazon.com/zh_cn/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. 向 Amazon S3 存储桶添加新 AWS Config 数据会调用一个 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 账户 区域收集 AWS Config 配置和合规性数据。
+ [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 自动运行手册，该操作手册设置 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 组织的配置和合规性数据。有关更多信息，请参阅文档中的[多账户多区域数据聚合](https://docs.aws.amazon.com/config/latest/developerguide/aggregate-data.html)。 AWS Config 
+ [在部署此解决方案之前，我们建议您查看 A [mazon S3 [AWS Config](https://aws.amazon.com/config/pricing/)](https://aws.amazon.com/s3/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 下载 CloudFormation 模板。 | 下载 Config-Visualizat [ion QuickSight-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_cn/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_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理员、云管理员、 DevOps 工程师 | 

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


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 找到您的快捷用户名。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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_cn/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_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | AWS 管理员、云管理员、 DevOps 工程师 | 

### 在快速视图中可视化数据
<a name="visualize-data-in-qsight"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 刷新数据。 | 要根据您的特定要求安排数据集刷新，请按照[刷新 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)说明进行操作。 | 快速套件管理员 | 
| 创建控制面板。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-aws-resource-inventory.html) | 快速套件管理员 | 

### （可选）清理
<a name="optional-clean-up"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除 Systems Manager 自动化创建的资源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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 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 快速社区学习中心](https://community.amazonquicksight.com/c/learning-center/10/none)
+ [Amazon Quick 社区画廊](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*

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

此模式描述了如何构建与 MongoDB Atlas 集群集集成的 AWS 着陆区。该基础设施使用 Terraform 脚本自动部署。

结构良好的多账户 AWS 环境（称为 landing [zon](https://docs.aws.amazon.com/prescriptive-guidance/latest/strategy-migration/aws-landing-zone.html) e）可提供可扩展性和安全性，尤其适合企业。它充当快速部署工作负载和应用程序的基础，有助于确保人们对安全和基础设施的信心。构建登录区需要仔细考虑技术和业务因素，包括账户结构、网络、安全和访问管理。这些考虑因素应与贵组织未来的发展和业务目标保持一致。

此模式的使用案例包括下述内容。
+ **企业 SaaS 和 PaaS 平台：**运行的多租户软件即服务 (SaaS) 应用程序和平台即服务 (PaaS) 平台 AWS 可以使用此设置来帮助提供对 MongoDB Atlas 的安全、私密访问，而无需通过公共互联网暴露数据。
+ **高度监管的行业**：银行、金融服务、医疗保健和政府工作负荷要求严格遵守健康保险便携性和责任法案 (HIPAA)、支付卡行业数据安全标准 (PCI DSS)、系统和组织控制2 () 和《通用数据保护条例》(GDPRSOC2) 等标准将受益于：
  + 通过加密的私有连接 AWS PrivateLink
  + MongoDB 副本集的多可用区高可用性
+ **安全 AI/ML 工作负载**：Amazon Bedrock、Amazon AI 或自定义 A SageMaker I 模型中的训练或推理管道可以安全地在 MongoDB Atlas 中获取和存储数据。 PrivateLink
+ **灾难恢复和业务连续性**：多可用区设计可确保单个可用区故障不会中断工作负载。跨可用区的 Atlas 副本集可确保自动实现故障转移。这对于金融科技（fintech）应用程序、数字银行或医疗保健监控等始终在线的服务至关重要。

## 先决条件和限制
<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)，然后选择相应服务的链接。

## 架构
<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_cn/prescriptive-guidance/latest/patterns/images/pattern-img/72d335b9-b5b1-4fe2-9972-65edbec60ab1/images/82a8cc98-6f22-4e28-a236-57a809930055.png)


此架构包含以下内容：

**VPC**
+ 单个虚拟私有云（VPC）跨越三个可用区。
+ VPC 细分为与每个可用区对齐的子网。这些子网分发工作负载以实现高可用性。

**互联网访问**
+ 互联网网关为有需要的资源（例如应用程序或堡垒主机）提供出站互联网连接。
+ 公有子网可以包含 NAT 网关，它允许私有子网工作负载下载更新、补丁和其他必需的软件包，而无需将其直接暴露于公共互联网。

**私有子网和路由表**
+ 应用程序组件、微服务或其他敏感资源通常位于私有子网中。
+ 专用路由表控制流量。将私有子网的出站流量定向到 NAT 网关，以实现安全、仅限出站的互联网访问。
+ 来自互联网的入站请求流经公有子网中的弹性负载均衡器或堡垒主机（如果使用），然后相应地路由到私有子网资源。

**通过 MongoDB Atlas 进行连接 PrivateLink**
+ 该架构使用 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/) 是一款基础设施即代码 (IaC) 工具 HashiCorp ，可帮助您创建和管理云和本地资源。在这种模式中，您可以使用 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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 确定关键利益相关者。 | 确定参与您的登录区项目的所有关键利益相关者和团队成员。这可能包括以下角色：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/build-aws-landing-zone-that-includes-mongodb-atlas.html) | 迁移主管 | 
| 创建结构蓝图。 | 创建一份蓝图，概述您 AWS 和 MongoDB Atlas 启用的着陆区的所需结构。 | 迁移主管 | 
| 创建架构计划。 | 与您的应用程序架构师合作，分析需求并设计可容错、有弹性的架构。此模式提供了一个入门架构模板供您参考。您可以自定义此模板以满足组织的安全和基础设施需求。 | 云架构师 | 
| 规划设置和部署。 | 与所有利益相关者一起确定架构将如何部署、如何实施安全措施以及任何其他方面，以确保与组织和请求团队的利益保持一致。 | 迁移主管、 DevOps 工程师、数据库管理员 | 

### 设置 MongoDB Atlas 环境
<a name="set-up-the-mongodb-atlas-environment"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 克隆存储库。 | 运行以下命令从[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_cn/prescriptive-guidance/latest/patterns/build-aws-landing-zone-that-includes-mongodb-atlas.html) | 数据库管理员 | 
| 生成 Atlas 组织级 API 密钥。 | 要在 Atlas 中生成组织级 API 密钥，请按照 [MongoDB 文档](https://www.mongodb.com/docs/atlas/configure-api-access/#grant-programmatic-access-to-an-organization)中的说明操作。 | 数据库管理员 | 
| 在中创建密钥 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/)中的说明操作。 | 数据库管理员 | 

### 设置 AWS 环境
<a name="set-up-the-aws-environments"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 修改 Terraform 脚本。 | 在存储 GitHub 库的本地副本中，更新 [modules/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 和密钥，请按照 re AWS : 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）文档](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)中的说明，创建 S3 存储桶来存储 Terraform 部署状态。 | DevOps 工程师 | 
| 更新用于存储的 S3 存储桶。 | 更新本地版本 [environments/development/main.tf](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/83e0b52cc4a8c12b24b54edeecbae496880d3615/environments/development/main.tf#L16) 中的 S3 存储桶信息，使其与您在上一步中创建的存储桶的名称和区域相匹配，并指定 key prefix。例如：<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.tfv](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/main/environments/development/terraform.tfvars) ars 文件中设置变量值。按照 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_cn/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 网关、路由和路由表的设置以满足组织的需求。有关详细信息，请参阅存储库的[自述文件](https://github.com/mongodb-partners/AWS-MongoDB-Atlas-Landing-Zone/blob/main/README.md#vpc-configurations)。 GitHub  | DevOps 工程师 | 
| 标记资源。 | 在 Terraform 脚本部署 AWS 资源时，您可以标记资源以对其进行监控。有关示例，请参阅 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` 文件夹复制到 `environments` 下的新文件夹（例如，`prod` 或 `staging`）。然后，您可以使用新值更新 `terraform.tfvars` 文件。 | DevOps 工程师 | 

### 部署登录区
<a name="deploy-the-landing-zone"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 初始化 Terraform 工作目录。 | 要初始化工作目录并下载必要的软件包，请运行以下命令：<pre>terraform init</pre> | DevOps 工程师 | 
| 创建执行计划。 | 要创建执行计划并可视化 Terraform 将对您的基础设施所做的更改，请运行以下命令：<pre>terraform plan</pre> | DevOps 工程师 | 
| 部署更改。 | 要按照代码中所述对基础设施实施更改，请运行以下命令：<pre>terraform apply</pre> | DevOps 工程师 | 
| 验证部署。 | 验证 Terraform 在您的基础设施中创建或修改的组件。要测试设置，请在 VPC 中或连接到 VPC 中配置计算资源（例如，Amazon EC2 实例或 AWS Lambda 函数）。 | DevOps 工程师，应用程序开发人员 | 

### 删除资源
<a name="remove-resources"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 清理。 | 完成测试后，运行以下命令来销毁 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 文档）
+ [对 landing zone 配置的期望](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-configure.html)（AWS Control Tower 文档）
+ landin@@ [g zone 更新最佳实践](https://docs.aws.amazon.com/controltower/latest/userguide/lz-update-best-practices.html)（AWS Control Tower 文档）

**设置 MongoDB 地图集和环境 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 文档）

**部署登录区**
+ [AWS上的 Terraform](https://docs.aws.amazon.com/whitepapers/latest/cicd_for_5g_networks_on_aws/terraform.html)（* AWS上的 5G 网络的 CI/CD* 白皮书）
+ [带有 Terraform 的 MongoDB Atlas](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*

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

在 AWS 虚拟私有云 (VPC) 中，VPC 流日志功能可以为操作和安全故障排除提供有用的数据。但是，在多账户环境中使用 VPC 流日志存在限制。具体而言，不支持 Amazon Logs 中的跨账户流 CloudWatch 日志。相反，您可以通过使用相应存储桶策略配置 Amazon Simple Storage Service（Amazon S3）存储桶来集中管理日志。

**注意**  
此模式讨论了将流日志发送到集中位置的要求。但是，如果您还希望在成员账户中本地提供日志，则可以为每个 VPC 创建多个流日志。无法访问日志存档账户的用户可以查看流量日志以进行问题排查。或者，您可以为向日志发送日志的每个 VPC 配置一个流 CloudWatch 日志。然后，您可以使用 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 组织

**限制**

如果您使用 AWS Key Management Service (AWS KMS) 托管密钥`aws/s3`加密您的中央存储桶，它将不会收到来自其他账户的日志。相反，您将会看到一个 `Unsuccessful` 错误代码 400，其中包含一条消息，例如针对您的指定 `ResourceId` 的消息 `"LogDestination: <bucketName> is undeliverable"`。这是因为账户的 AWS 托管密钥无法在账户之间共享。解决方案是使用 Amazon S3 托管加密 (SSE-S3) 或您可以与成员账户共享的 AWS KMS 客户托管密钥。

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

**目标架构**

在下图中，为每个 VPC 部署了两个流日志。一个向本地日志组发送 CloudWatch 日志。另一个将日志发送到集中式日志账户中的 S3 存储桶。存储桶策略允许日志传输服务将日志写入存储桶。

**注意**  
自 2023 年 11 月起， AWS 现在支持 a [ws: SourceOrg ID 条件密钥](https://aws.amazon.com/about-aws/whats-new/2023/11/organization-wide-iam-condition-keys-restrict-aws-service-to-service-requests/)。此条件允许您拒绝向 AWS Organizations 组织以外的账户写入中央存储桶。

![\[从每个 VPC，一个流日志向 S3 存储桶发送日志 CloudWatch ，另一个流日志向 S3 存储桶发送日志。\]](http://docs.aws.amazon.com/zh_cn/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 Account Factory for 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 Lo](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html) gs 可帮助您集中所有系统和应用程序的日志， AWS 服务 这样您就可以监控它们并安全地将其存档。
+ [Amazon Simple Storage Service（Amazon S3）](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)是一项基于云的对象存储服务，可帮助您存储、保护和检索任意数量的数据。
+ [Amazon Virtual Private Cloud（亚马逊 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 日志。

```
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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 确定加密策略并为中央 S3 存储桶创建策略。 | 中央存储桶不支持 AWS KMS `aws/s3`密钥，因此您必须使用 SSE-S3 或 AWS KMS 客户管理的密钥。如果您使用 AWS KMS 密钥，则密钥策略必须允许成员账户使用该密钥。 | 合规 | 
| 创建中央流日志存储桶。 | 创建将向其发送流日志的中央存储桶，然后应用您在上一步中选择的加密策略。这应该在日志存档或类似用途的账户中。从[其他信息](#configure-vpc-flow-logs-for-centralization-across-aws-accounts-additional)部分获取存储桶策略，并在您使用您的环境特定值更新占位符后，将其应用于您的中央存储桶。 | 常规 AWS | 
| 配置 VPC 流日志以将日志发送到中央流日志存储桶。 | 向要从中收集数据的每个 VPC 添加流日志。实现此目的的最具可扩展性的方法是使用Iac工具，例如AFT或 AWS Cloud Development Kit (AWS CDK). 例如，您可以创建一个在流日志旁边部署 VPC 的 Terraform 模块。如有必要，您可以手动添加流日志。 | 网络管理员 | 
| 将 VPC 流日志配置为发送到本地 CloudWatch 日志。 | （可选）如果您希望流日志在生成日志的账户中可见，请创建另一个流日志，将数据发送到本地账户中的 CloudWatch 日志。或者，您可以将数据发送到本地账户中特定于账户的 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*

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

AWS IAM Identity Center 通过提供一个用于管理对云应用程序的单点登录访问 AWS 账户 和云应用程序的集中式中心来增强 AWS Identity and Access Management (IAM)。但是，随着组织的发展，手动管理 IAM Identity Center [权限集](https://docs.aws.amazon.com/singlesignon/latest/userguide/permissionsetsconcept.html)可能变得越来越复杂和容易出错。这种复杂性可能导致潜在的安全漏洞和管理开销。

这个解决方案可助你使用基于原生 AWS 服务的持续集成和持续交付（CI/CD）管道，通过基础设施即代码（IaC）管理权限集。它可以将权限集分配机制与 AWS Control Tower 生命周期事件或 Acco [unt 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。
+ IAM 身份中心委 AWS 账户 托管理员接收解决方案。有关更多信息，请参阅 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 S3) Service 存储桶和亚马逊 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)，然后选择相应服务的链接。

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

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

![\[使用 Terraform 管理 AWS 权限集的组件和工作流。\]](http://docs.aws.amazon.com/zh_cn/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 的主代码使用官方的 T AWS `permission-sets` erraform 模块。该模块可以根据权限集模板动态管理 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)使 AWS 资源和服务（例如 CodePipeline）能够连接到外部代码存储库，例如 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/) 是一款基础设施即代码 (IaC) 工具 HashiCorp ，可帮助您创建和管理云和本地资源。

**代码存储库**

此模式的代码可在 AWS 示例组织中的 [sample-terraform-aws-permission-sets-](https://github.com/aws-samples/sample-terraform-aws-permission-sets-pipeline) pipeline 存储库 GitHub 中找到。

## 最佳实践
<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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 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_cn/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_cn/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_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建专用的存储库。 | 此任务假设你正在使用 GitHub。创建专用的存储库来存储 Terraform 主代码和权限集模板 JSON 文件。 | DevOps 工程师 | 
| 准备权限集代码。 | 有关如何架构以下文件的信息，请参阅解决方案存储库中的[示例代码](https://github.com/aws-samples/sample-terraform-aws-permission-sets-pipeline/tree/main/samples/basic)：├── main.tf├── outputs.tf├── providers.jinja└── templates复制内容，保留 `providers.jinja` 值，然后对其他文件进行必要的调整。例如，将权限集模板文件添加到 `templates` 中，或者在 `main.tf` 文件中锁定 `aws-ia/permission-sets/aws` 模块的版本。 | DevOps 工程师 | 
| 提交变更。 | 提交变更并将变更推送到之前创建的存储库。例如，保存存储库名称及其 GitHub 组织`myorg/aws-ps-pipeline`。 | DevOps 工程师 | 

### 准备部署代码
<a name="prepare-the-deployment-code"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 下载内容。 | 从解决方案[存储库](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_cn/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>有关其他变量选项的信息，请参阅此模式存储库中的 v [ariables.tf](https://github.com/aws-samples/sample-terraform-aws-permission-sets-pipeline/blob/main/variables.tf) 文件。 GitHub  | 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 Provider 配置。 | 在 `providers.tf` 文件中，将占位符替换为您自己的信息。使用 AWS Control Tower 主区域，为提供商提供先前创建的跨账户 IAM 角色的 ARN。`event-source-account`<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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 选择 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_cn/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_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 通过 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_cn/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_cn/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_cn/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 身份中心文档）

**其他资源**
+ [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 自动标记中转网关连接
<a name="tag-transit-gateway-attachments-automatically-using-aws-organizations"></a>

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

## Summary
<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 账户 界共享。然而，当您跨账户边界创建中转网关连接时，创建的连接将没有“名称”标签。这样可能会使识别这些连接变得耗时。 

该解决方案提供了一种自动机制，用于为 [AWS Organizations](https://aws.amazon.com/organizations/) 管理的组织内的账户收集有关每个中转网关连接的信息。此过程包括从中转网关路由表中查找[无类别域间路由](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing)（CIDR）范围。随后，该解决方案会将 `<CIDR-range>-<AccountName>` 形式的“名称”标签应用于含有中转网关的那个账户内的连接。

该解决方案可以与解决方案库中的[无服务器公交网络 Orchestrator](https://aws.amazon.com/solutions/implementations/serverless-transit-network-orchestrator/) 等解决方案一起使用。 AWS 无服务器中转网络编排工具支持大规模自动创建中转网关连接。

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

**先决条件**
+ 活跃的 AWS 账户
+ 包含所有相关账户的 AWS Organizations 组织
+ 访问组织根目录下的组织管理账户以创建必需 AWS Identity and Access Management (IAM) 角色
+ 一个共享网络成员账户，其中包含一个或多个与组织共享并具有连接的中转网关

## 架构
<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_cn/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/) 函数，这些函数将执行底层任务。

解决方案从中获取账户名后，Step Function AWS Organizations s 状态机将获取所有 Transit Gateway 附件 IDs。系统将按区域来并行处理这些连接。这一处理包含查找每个连接的 CIDR 范围。CIDR 范围是通过在区域内的中转网关路由表中搜索匹配的中转网关连接 ID 来获取的。如果所有必需的信息均可用，该解决方案会将名称标签应用于附件。该解决方案不会覆盖任何现有名称标签。

该解决方案按照 [Amazon EventBridge](https://aws.amazon.com/eventbridge/) 事件控制的时间表运行。该事件每天早上 6:00 UTC 启动解决方案。

**目标技术堆栈**
+ Amazon EventBridge
+ AWS Lambda
+ AWS Organizations
+ AWS Transit Gateway
+ Amazon Virtual Private Cloud（Amazon VPC）
+ AWS X-Ray

**目标架构**

下图显示了该解决方案的架构和工作流。

![\[跨共享网络和组织管理账户的九步流程。\]](http://docs.aws.amazon.com/zh_cn/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. 状态机调用 Lambda 函数 `tgw-tagger-organizations-account-query`。

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. 名称标签是在共享网络账户中为中转网关连接创建的。

**自动化和扩缩**

该解决方案并行处理每个区域以减少运行的总持续时间。

## 工具
<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（亚马逊 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) 会收集您的应用程序所服务的请求的相关数据，并提供了一些工具，供您查看、筛选和了解这些数据，以便发现问题和优化机会。

**代码**

该解决方案的源代码可在 Tr [ansit 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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 收集必要的先决条件信息。 | 要配置从 Lambda 函数对 API AWS Organizations 的跨账户访问权限，您需要组织管理账户的账户 ID。****这两个 CloudFormation 堆栈的创建顺序很重要。您必须首先将资源部署到共享网络账户中。在将资源部署到组织的管理账户之前，共享网络账户中的角色必须已经存在。有关详情，请参阅 [AWS 文档](https://docs.amazonaws.cn/en_us/IAM/latest/UserGuide/id_roles_create_for-user.html)。 | DevOps 工程师 | 
| 启动主解决方案堆栈的 CloudFormation 模板。 | 主解决方案堆栈的模板将部署 IAM 角色、Step Functions 工作流程、Lambda 函数和亚马逊 CloudWatch 事件。打开共享网络账户 AWS 管理控制台 的，然后打开:&CFN 控制台。 使用 `tgw-attachment-tagger-main-stack.yaml` 模板和下面这些值来创建堆栈： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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 堆栈达到 CRE **ATE\$1** COMPLETE 状态。此过程花费的时间应当不到 1 分钟。打开 Step Functions 控制台，验证是否创建了一台名为 **tgw-attachment-tagger-state-machine 的新状态机**。 | DevOps 工程师 | 

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


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 收集必要的先决条件信息。 | 要配置从 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_cn/prescriptive-guidance/latest/patterns/tag-transit-gateway-attachments-automatically-using-aws-organizations.html)对于其他堆栈创建选项，请用默认值。 | DevOps 工程师 | 
| 确认已成功启动解决方案。 | 等待 CloudFormation 堆栈达到 CRE **ATE\$1** COMPLETE 状态。此过程花费的时间应当不到 1 分钟。打开 AWS Identity and Access Management (IAM) 控制台，确认已创建名为 **tgw-attachment-tagger-organization-query-** role 的新角色。 | DevOps 工程师 | 

### 验证解决方案
<a name="verify-the-solution"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 运行状态机。 | 打开共享网络账户的 Step Functions 控制台，然后在导航窗格选择**状态机**。选择状态机 **tgw-attachment-tagger-state-机器**，然后选择**开始执行**。 由于解决方案不使用此状态机输入，因此您可使用默认值。<pre>{<br />    "Comment": "Insert your JSON here"<br />}</pre>选择**开始执行**。 | DevOps 工程师 | 
| 观察状态机直至完成。 | 在打开的新页面，您可观看状态机的运行。持续时间将取决于待处理中转网关连接的数量。在此页面，您可检查状态机的每个步骤。您可以查看状态机中的各种任务，并点击指向 Lambda 函数的 CloudWatch 日志链接。对于在地图中并行运行的任务，您可使用**索引**下拉列表来查看每个区域的具体实现。 | DevOps 工程师 | 
| 验证中转网关连接标签。 | 打开共享网络账户的 VPC 控制台，然后选择**中转网关连接**。 在控制台上，为满足标准的连接提供名称标签（连接将传播到中转网关路由表，并且资源所有者是组织的成员）。 | DevOps 工程师 | 
| 验证 CloudWatch 事件是否已启动。 | 等待 CloudWatch 事件启动。预定时间为 UTC 时间 06:00。 然后打开共享网络账户的 Step Functions 控制台，然后在导航窗格选择**状态机**。选择状态机 **tgw-attachment-tagger-state-机器**。验证解决方案是否在 06:00 UTC 运行。 | 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**
+ [使用 AFT 自动为新 AWS 账户 用户分配 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)