

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

# 使用 Step Functions 和 Lambda 代理函数在 AWS 账户上启动 CodeBuild 项目
<a name="launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function"></a>

*Richard Milner-Watts 和 Amit Anjarlekar，Amazon Web Services*

## Summary
<a name="launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function-summary"></a>

此模式演示了如何使用 AWS Step Functions 和 AWS Lambda 代理函数跨多个 AWS 账户异步启动 AWS CodeBuild 项目。你可以使用模式的示例 Step Functions 状态机来测试你的 CodeBuild 项目是否成功。

CodeBuild 帮助您在完全托管的运行时环境中使用 AWS 命令行界面 (AWS CLI) Line Interface 启动操作任务。您可以通过覆盖环境变量来更改 CodeBuild 项目在运行时的行为。此外，您还可以使用 CodeBuild 来管理工作流程。有关更多信息，请参阅 AWS Workshop 网站上的[服务目录工具](https://service-catalog-tools-workshop.com/tools.html)和在 AWS 数据库博客上使用 AWS [在 Amazon RDS for PostgreSQL 中安排作业，并在 CodeBuild AWS 数据库博客上使用 EventBridge](https://aws.amazon.com/blogs/database/schedule-jobs-in-amazon-rds-for-postgresql-using-aws-codebuild-and-amazon-eventbridge/)亚马逊安排作业。

## 先决条件和限制
<a name="launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function-prereqs"></a>

**先决条件**
+ 两个活跃的 AWS 账户：一个用于通过 Step Functions 调用 Lambda 代理函数的源账户和一个用于构建远程示例项目的目标账户 CodeBuild 

**限制**
+ 此模式不能用于在账户之间复制[构件](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-codebuild-project-artifacts.html)。

## 架构
<a name="launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function-architecture"></a>

下图显示此模式构建的架构。

![\[跨多个 AWS 账户启动 CodeBuild 项目的架构图\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/809a5716-56e5-477c-aac6-02243675a2f2/images/857ba3ae-eb9a-4d6b-b73e-e596f41c8cb8.png)


下图显示了如下工作流：

1. Step Functions 状态机解析提供的输入映射，并为您定义的每个账户、区域和项目调用 Lambda 代理函数（`codebuild-proxy-lambda`）。

1. Lambda 代理函数使用 AWS Security Token Service（AWS STS）代入 IAM 代理角色 (`codebuild-proxy-role`)，而该角色会与目标账户中 IAM 策略 (`codebuild-proxy-policy`) 关联。

1. 使用代入的角色，Lambda 函数启动 CodeBuild 项目并返回 CodeBuild 任务 ID。Step Functions 状态机循环并轮询 CodeBuild 作业，直到收到成功或失败状态。

状态机逻辑如下图所示。

![\[Step Functions 状态机的工作流\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/809a5716-56e5-477c-aac6-02243675a2f2/images/4729bbfc-79ad-455d-a85a-b96cce00f432.png)


**技术堆栈**
+ AWS CloudFormation
+ CodeBuild
+ IAM
+ Lambda
+ Step Functions
+ X-Ray

## 工具
<a name="launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function-tools"></a>
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可帮助您设置 AWS 资源，快速一致地配置这些资源，并在 AWS 账户和区域的整个生命周期中对其进行管理。
+ [AWS CloudFormation Designer](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/working-with-templates-cfn-designer-json-editor.html) 提供了一个集成的 JSON 和 YAML 编辑器，可帮助您查看和编辑 CloudFormation 模板。
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一项完全托管的构建服务，可帮助您编译源代码、运行单元测试和生成可随时部署的项目。
+ [AWS Identity and Access Management (AWS IAM) ](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)通过控制验证和授权使用您 AWS 资源的用户，帮助您安全地管理对您 AWS 资源的访问。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，而无需预置或管理服务器。它仅在需要时运行您的代码，并且能自动扩缩，因此您只需为使用的计算时间付费。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html) 是一项无服务器编排服务，可让您搭配使用 AWS Lambda 函数和其他 Amazon Web Services 来构建业务关键型应用程序。
+ [AWS X-Ray](https://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html) – 帮助您收集您的应用程序所服务的请求的相关数据，并提供用于查看、筛选和获取数据洞察力的工具，以确定问题和发现优化机会。

**代码**

此模式的示例代码可在 GitHub[跨账户 CodeBuild 代理](https://github.com/aws-samples/cross-account-codebuild-proxy)存储库中找到。此模式使用 AWS Lambda Powertools for Python 库来提供日志和跟踪功能。有关此库及其实用程序的更多信息，请参阅 [Powertools for AWS Lambda (Python)](https://docs.powertools.aws.dev/lambda/python/latest/)。

## 最佳实践
<a name="launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function-best-practices"></a>

1. 调整 Step Function 状态机中的等待时间值，以最大限度地减少对任务状态的轮询请求。使用 CodeBuild 项目的预期执行时间。

1. 在 Step Functions 中调整地图的`MaxConcurrency`属性以控制可以并行运行的 CodeBuild 项目数量。

1. 如有必要，请查看示例代码是否已准备就绪可供生产。考虑解决方案可能记录哪些数据，以及默认的 Amazon CloudWatch 加密是否足够。

## 操作说明
<a name="launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function-epics"></a>

### 在源账户中创建 Lambda 代理函数和关联 IAM 角色
<a name="create-the-lambda-proxy-function-and-associated-iam-role-in-the-source-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 记录 AWS 账户 IDs。 | 需要 AWS 账户 IDs 才能设置跨账户的访问权限。记录您的源账户和目标账户的 Amazon Web Services account ID。有关更多信息，请参阅 IAM 文档中的[查找您 Amazon Web Services account ID](https://docs.aws.amazon.com/IAM/latest/UserGuide/console_account-alias.html#FindingYourAWSId)。 | AWS DevOps | 
| 下载 AWS CloudFormation 模板。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.html)在 AWS CloudFormation 模板中，`<SourceAccountId>`是源账户的 AWS 账户 ID，`<TargetAccountId>`也是目标账户的 AWS 账户 ID。 | AWS DevOps | 
| 创建并部署 AWS CloudFormation 堆栈。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.html)在目标账户中创建任何资源之前，您必须为代理 Lambda 函数创建 AWS CloudFormation 堆栈。当您在目标账户中创建信任策略时，IAM 角色会从角色名称转换至内部标识符。所以 IAM 角色必须已经存在。 | AWS DevOps | 
| 确认代理函数和状态机已创建。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.html) | AWS DevOps | 

### 在目标账户中创建 IAM 角色并启动示例 CodeBuild 项目
<a name="create-an-iam-role-in-the-target-account-and-launch-a-sample-codebuild-project"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建并部署 AWS CloudFormation 堆栈。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.html) | AWS DevOps | 
| 验证示例 CodeBuild 项目的创建。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.html) | AWS DevOps | 

### 测试跨账户 Lambda 代理函数
<a name="test-the-cross-account-lambda-proxy-function"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 启动状态机。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.html) | AWS DevOps | 
| 验证环境变量。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.html) | AWS DevOps | 

## 问题排查
<a name="launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| Step Functions 的执行时间比预期时间要长。 | 在 Step Function 状态机中调整地图的`MaxConcurrency`属性以控制可以并行运行多少 CodeBuild 项目。 | 
|  CodeBuild 任务的执行时间比预期的要长。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/launch-a-codebuild-project-across-aws-accounts-using-step-functions-and-a-lambda-proxy-function.html) | 