Lesson 3 of 4 AWS CodePipeline

Designing Cross-Account and Cross-Region Pipelines

Most organisations deploy from a central tooling account into separate test and production accounts, and many deploy to several Regions. This lesson explains the roles, key policies, bucket policies and artifact stores that a cross-account and cross-Region pipeline needs, and why each element is required.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why a central tooling account and per-environment accounts are used for deployment pipelines.
  • Describe the IAM roles, KMS key policy and bucket policy required for a cross-account action.
  • Explain how cross-Region actions use a separate artifact store in each Region.
  • Identify the cause of common permission failures in cross-account pipelines.

Why Pipelines Span Accounts

An AWS account is the strongest isolation boundary on AWS. Placing each environment in its own account limits the impact of mistakes and compromises, simplifies cost allocation and lets each account have different permissions. A common pattern places the pipeline in a tooling account (sometimes called a deployment or shared services account) and deploys to workload accounts for development, test and production. Developers can be given broad access to the development account and little or none to production, while the pipeline is the only identity that changes production.

The Elements of a Cross-Account Action

When a pipeline in the tooling account deploys into a production account, four pieces must be in place. Each one solves a specific access problem.

1. A cross-account role in the target account

The target account contains an IAM role, for example PipelineDeployRole, whose trust policy allows the tooling account (or specifically the pipeline service role) to assume it. The action in the pipeline sets roleArn to this role. CodePipeline assumes the role for the action, so the action's API calls happen in the target account.

2. Permission for the pipeline role to assume it

The pipeline service role in the tooling account needs sts:AssumeRole on the target role's ARN. Both sides of the trust must allow the assumption: the trust policy in the target account and the identity policy in the source account.

3. A customer managed KMS key shared with the target account

Artifacts in the tooling account's bucket are encrypted. The target account must decrypt them, so the pipeline uses a customer managed KMS key whose key policy allows the target account's role to call kms:Decrypt and kms:DescribeKey (and kms:Encrypt, kms:ReEncrypt* and kms:GenerateDataKey* if the action writes output artifacts). The target role's identity policy must also allow use of the key.

4. A bucket policy that allows the target account

The artifact bucket's policy allows the target account's role to get and put objects. Without it, the action can decrypt nothing because it cannot read the object in the first place.

For CloudFormation actions there is usually a fifth element: a CloudFormation service role in the target account, which the deploy role passes to CloudFormation and which has the permissions to create the stack's resources. The deploy role then needs iam:PassRole for that role. Separating the two roles keeps the role that CodePipeline assumes narrow.

The following table summarises where each element lives.

ElementAccountGrants
Pipeline service role with sts:AssumeRoleToolingPermission to assume the target role
Cross-account deploy role (trust policy)TargetTrust of the tooling account's pipeline role
Customer managed KMS key policyToolingDecrypt and encrypt for the target role
Artifact bucket policyToolingGet and put objects for the target role
CloudFormation service roleTargetPermissions to create the stack's resources

The following diagram places the numbered elements in their accounts.

The elements of a cross-account deploy action. In the tooling account, the pipeline service role has sts:AssumeRole on the target role (1 and 2), the customer managed KMS key's policy lets the target role decrypt artifacts (3), and the artifact bucket policy lets it read and write objects (4). In the target account, the deploy role trusts the pipeline role, and for CloudFormation actions it passes a CloudFormation service role that has the permissions to create the stack's resources (5).
The numbers match the elements described above.
Select all that apply

Build a cross-account action

Exam skills 1.1.5 · 2.2.3

Identify everything a pipeline in a tooling account needs to deploy into a production account.

Log in to try this activity Interactive activities are free with an account.

Cross-Region Actions

An action can run in a Region different from the pipeline by setting its region property. Actions read artifacts from an S3 bucket in their own Region, so a cross-Region pipeline defines an artifactStores map with one artifact store per Region instead of a single artifactStore. CodePipeline copies artifacts to the Region where the action runs. Each Region's store needs its own KMS key in that Region if a customer managed key is used, because KMS keys are Regional unless multi-Region keys are used.

A typical global deployment has a production stage with one CloudFormation or CodeDeploy action per Region. Placing the Regions in successive stages, rather than in parallel actions of one stage, implements deployment waves: if the first Region fails, later Regions are not touched.

StackSets as an alternative

CloudFormation StackSets offer another way to deploy one template to many accounts and Regions from a single action. They are covered in the AWS CloudFormation module. Choose StackSets when the same infrastructure is needed in many accounts, such as baseline roles or Config rules; choose per-Region pipeline actions when an application is promoted through stages with tests between Regions.

Troubleshooting Cross-Account Failures

Cross-account pipelines fail in characteristic ways, and the error message usually points to the missing element. The list below links common symptoms to their causes.

  • "not authorized to perform sts:AssumeRole": either the pipeline role lacks sts:AssumeRole, or the target role's trust policy does not trust it.
  • Access denied reading the input artifact: the bucket policy does not allow the target role, or the bucket uses a key the target role cannot use.
  • KMS AccessDeniedException or "the ciphertext refers to a customer master key that does not exist": the artifacts are encrypted with the AWS managed key, which cannot be shared, or the customer managed key policy does not allow the target account.
  • CloudFormation "is not authorized to perform iam:PassRole": the deploy role cannot pass the CloudFormation service role.
  • CloudFormation resource creation fails with access denied: the CloudFormation service role lacks permission for that resource type.
Worked example

A Three-Account Pipeline

An organisation has a tooling account (111111111111), a test account (222222222222) and a production account (333333333333). The pipeline in the tooling account builds a serverless application and deploys it with CloudFormation to test and then production.

  1. Key and bucket

    Create a customer managed KMS key in the tooling account. The key policy and the artifact bucket policy allow the deploy roles of the test and production accounts.

  2. Target roles

    In each target account, create a deploy role trusted by the pipeline role and a CloudFormation execution role with the permissions for the stack resources.

  3. Pipeline role

    Allow the pipeline service role to assume both deploy roles.

  4. Actions

    Set each deploy action's roleArn to the target account's deploy role and its RoleArn configuration to the CloudFormation execution role.

The AWS CDK automates most of this when CDK Pipelines is used with bootstrapped accounts: the bootstrap stack in each target account creates deploy and execution roles that trust the tooling account.

Check yourself

A cross-account deploy action fails with a KMS error although the target role has kms:Decrypt in its identity policy. The pipeline uses the default artifact bucket encryption. What should change?

Show answer

The pipeline must use a customer managed KMS key whose key policy allows the target account. The default AWS managed S3 key cannot be used by another account, regardless of the identity policy.

Exam Tips

Remember for the exam

  • Cross-account actions need: a role in the target account set as the action roleArn, sts:AssumeRole for the pipeline role, a customer managed KMS key shared with the target account and an artifact bucket policy.
  • CloudFormation cross-account deploys usually pass a separate CloudFormation service role, which requires iam:PassRole.
  • Cross-Region actions need an artifact store (and a KMS key) in each Region, defined with artifactStores.
  • Deploy Regions in successive stages to implement waves that stop at the first failing Region.