Lesson 4 of 4 Deployment Strategies and Infrastructure as Code

Building Cross-Account CI/CD Pipelines

This lesson explains the cross-account plumbing, roles, encryption keys and artifact stores, of a pipeline that lives in a tooling account and deploys into separate development, test and production accounts.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the cross-account deployment pattern.
  • Explain how one artifact is promoted through stages with approvals.
  • Choose a deployment tool for many accounts.
  • Explain how the tooling account and pipeline are secured.

The cross-account deployment pattern

The pipeline runs in a tooling account and stores its artifacts in an S3 bucket there. Target accounts must read those artifacts, so the bucket is encrypted with a customer managed KMS key whose key policy allows the target accounts' roles to decrypt, and the bucket policy allows them to read. An AWS managed key cannot be used, because its policy cannot be changed to allow other accounts, which is the most common cause of cross-account pipeline failures.

Each target account has a cross-account role that trusts the tooling account, and the pipeline action assumes it to work in that account. For CloudFormation actions, that role passes a separate deployment role to CloudFormation, which holds the permissions to create the resources, so the pipeline itself can only ask CloudFormation to deploy. Actions in other Regions need an artifact bucket and KMS key in each of those Regions, because CodePipeline copies artifacts to the action's Region before running it.

Promotion and approval

One pipeline promotes the same artifact through stages: build once, deploy to dev, run tests, deploy to staging, wait for a manual approval or automated checks, then deploy to production. Rebuilding per environment means production runs something that was never tested. Approval actions can notify through SNS; automated gates can query CloudWatch alarms before proceeding.

Deploying to many accounts

The right deployment tool depends on how many accounts are involved and what is being deployed. CloudFormation StackSets, with service-managed permissions and automatic deployment, place baseline resources in every account of an OU, including accounts created later. CodePipeline, with cross-account actions, deploys an application to a handful of environments with tests and approvals between stages. CDK Pipelines define the pipeline in code alongside the application and create the cross-account roles and KMS keys automatically. For Terraform-based landing zones, Account Factory for Terraform, or Terraform runners that assume roles in each account, do the same job. The following table summarises the choice.

NeedTool
Baseline resources in every account of an OU, including new onesCloudFormation StackSets with service-managed permissions and automatic deployment
Application to a handful of environments with tests between themCodePipeline stages with cross-account actions
Pipelines defined in code with the applicationCDK Pipelines, which create the cross-account roles and keys for you
Terraform-based landing zonesAccount Factory for Terraform, or Terraform runners with assumed roles

StackSets can be administered from a delegated administrator account instead of the management account, and deployment options (concurrent accounts, failure tolerance, Region order) control the blast radius of a bad change.

Securing the pipeline

A pipeline that can deploy to production can change production, so the tooling account is as sensitive as production itself: few people have access, and changes to the pipeline are reviewed like code. External CI systems, such as GitHub Actions, use OIDC federation to assume roles rather than long-term access keys, with trust policies restricted to the specific repository and branch.

The deployment roles in each target account are scoped to what the stacks need, and SCPs ensure that only the pipeline's roles can assume them, so a developer cannot use the deployment role directly. Infrastructure code is checked with cfn-lint and CloudFormation Guard, and container images are scanned, in the pipeline before anything is deployed.

Worked example

A pipeline in a tooling account must deploy CloudFormation stacks to a production account. The deploy stage fails with an access denied error when the production account's role reads the artifact. The artifact bucket uses the default aws/s3 key.

The production role cannot decrypt objects encrypted with the tooling account's AWS managed key, because that key's policy cannot grant other accounts. Re-encrypt artifacts with a customer managed key whose policy allows the production role, configure it as the pipeline's artifact store key, and allow the role in the bucket policy.

Check yourself

Why does a central pipeline deploy into other accounts by assuming roles there?

Show answer

Each target account controls what the pipeline may change through the role's permissions and trust policy, and no long-term credentials leave the tooling account.

Exam Tips

Remember for the exam

  • Cross-account pipelines need a customer managed KMS key for artifacts; AWS managed keys cannot be shared.
  • Pipeline actions assume a role in the target account; CloudFormation uses a separate deployment role.
  • Cross-Region actions need an artifact bucket and key in each Region.
  • Build once and promote the same artifact through environments.
  • Use StackSets with service-managed permissions to deploy baselines to every account in an OU.
  • Prefer OIDC federation over access keys for external CI systems.