Launch a CodeBuild project across AWS accounts using Step Functions and a Lambda proxy function
Created by Richard Milner-Watts (AWS) and Amit Anjarlekar (AWS)
Code repository: Cross-Account CodeBuild Proxy | Environment: Production | Technologies: DevOps; Management & governance; Operations; Serverless |
AWS services: AWS CodeBuild; AWS Lambda; AWS Step Functions; AWS X-Ray; AWS CloudFormation |
Summary
This pattern demonstrates how to asynchronously launch an AWS CodeBuild project across multiple AWS accounts by using AWS Step Functions and an AWS Lambda proxy function. You can use the pattern’s sample Step Functions state machine to test the success of your CodeBuild project.
CodeBuild helps you launch operational tasks using the AWS Command Line Interface (AWS CLI) from a fully-managed runtime environment. You can change the behavior of your CodeBuild project at runtime by overriding environment variables. Additionally, you can use CodeBuild to manage workflows. For more information, see Service Catalog Tools
Prerequisites and limitations
Prerequisites
Two active AWS accounts: a source account for invoking a Lambda proxy function with Step Functions and a target account for building a remote CodeBuild sample project
Limitations
This pattern cannot be used to copy artifacts between accounts.
Architecture
The following diagram shows the architecture that this pattern builds.
The diagram shows the following workflow:
The Step Functions state machine parses the supplied input map and invokes the Lambda proxy function (
codebuild-proxy-lambda
) for each account, Region, and project you defined.The Lambda proxy function uses AWS Security Token Service (AWS STS) to assume an IAM proxy role (
codebuild-proxy-role
), which is associated with an IAM policy (codebuild-proxy-policy
) in the target account.Using the assumed role, the Lambda function launches the CodeBuild project and returns the CodeBuild job ID. The Step Functions state machine loops and polls the CodeBuild job until receiving a success or failure status.
The state machine logic is shown in the following image.
Technology stack
AWS CloudFormation
CodeBuild
IAM
Lambda
Step Functions
X-Ray
Tools
AWS CloudFormation helps you set up AWS resources, provision them quickly and consistently, and manage them throughout their lifecycle across AWS accounts and Regions.
AWS CloudFormation Designer provides an integrated JSON and YAML editor that helps you view and edit CloudFormation templates.
AWS CodeBuild is a fully managed build service that helps you compile source code, run unit tests, and produce artifacts that are ready to deploy.
AWS Identity and Access Management (IAM) helps you securely manage access to your AWS resources by controlling who is authenticated and authorized to use them.
AWS Lambda is a compute service that helps you run code without needing to provision or manage servers. It runs your code only when needed and scales automatically, so you pay only for the compute time that you use.
AWS Step Functions is a serverless orchestration service that helps you combine AWS Lambda functions and other AWS services to build business-critical applications.
AWS X-Ray helps you collect data about the requests that your application serves, and provides tools that you can use to view, filter, and gain insights into that data to identify issues and opportunities for optimization.
Code
The sample code for this pattern is available in the GitHub Cross Account CodeBuild Proxy
Best practices
Adjust the wait time values in the Step Function state machine to minimize polling requests for job status. Use the expected execution time for the CodeBuild project.
Adjust the
MaxConcurrency
property of the map in Step Functions to control how many CodeBuild projects can run in parallel.If required, review the sample code for production readiness. Consider what data might be logged by the solution and whether the default Amazon CloudWatch encryption is sufficient.
Epics
Task | Description | Skills required |
---|---|---|
Record the AWS account IDs. | AWS account IDs are required to set up access across accounts. Record the AWS account ID for your source and target accounts. For more information, see Finding your AWS account ID in the IAM documentation. | AWS DevOps |
Download the AWS CloudFormation templates. |
Note: In the AWS CloudFormation templates, | AWS DevOps |
Create and deploy the AWS CloudFormation stack. |
Note: You must create the AWS CloudFormation stack for the proxy Lambda function before creating any resources in target accounts. When you create a trust policy in a target account, the IAM role is translated from the role name to an internal identifier. This is why the IAM role must already exist. | AWS DevOps |
Confirm the creation of the proxy function and state machine. |
| AWS DevOps |
Task | Description | Skills required |
---|---|---|
Create and deploy the AWS CloudFormation stack. |
| AWS DevOps |
Verify the creation of the sample CodeBuild project. |
| AWS DevOps |
Task | Description | Skills required |
---|---|---|
Launch the state machine. |
| AWS DevOps |
Validate the environment variables. |
| AWS DevOps |
Troubleshooting
Issue | Solution |
---|---|
Step Functions execution is taking longer than expected. | Adjust the |
The execution of the CodeBuild jobs is taking longer than expected. |
|