Skip to content

feat(pipelines): add assumeRole parameter to CodeBuildStep#38285

Open
umbertocicciaa wants to merge 1 commit into
aws:mainfrom
umbertocicciaa:feat/18272-codebuildstep-assume-role
Open

feat(pipelines): add assumeRole parameter to CodeBuildStep#38285
umbertocicciaa wants to merge 1 commit into
aws:mainfrom
umbertocicciaa:feat/18272-codebuildstep-assume-role

Conversation

@umbertocicciaa

@umbertocicciaa umbertocicciaa commented Jul 11, 2026

Copy link
Copy Markdown

Issue # (if applicable)

Closes #18272

Ai Usage

Reason for this change

pipelines.CodeBuildStep has no built-in way to run its commands under a different IAM role. This matters for cross-account deployment pipelines, where the CodeBuild project's own role can't reach resources in the deployed (target) account, e.g. invoking a Lambda function to run a DB migration, or seeding Cognito users for integration tests. Users currently have to hand-roll this with aws sts assume-role calls inside commands, wiring up the trust policy and sts:AssumeRole permission themselves each time.

Description of changes

Added an assumeRole?: iam.IRole property to CodeBuildStepProps / CodeBuildStep. When set:

  • The CodeBuild project's role is granted sts:AssumeRole on the given role (assumeRole.grant(project.grantPrincipal, 'sts:AssumeRole')).
  • Three commands are prepended to the buildspec's install phase, before any user-supplied installCommands, which configure a named AWS CLI profile (cdk-assume-role) pointed at the role and export it as the active profile:
aws configure --profile cdk-assume-role set role_arn <roleArn>
aws configure --profile cdk-assume-role set credential_source EcsContainer
export AWS_PROFILE=cdk-assume-role

This means all of commands (and any commands after it in the same build) run under assumeRole without the user needing to write any of this themselves.

This follows the exact workaround proposed in the issue, and reuses two patterns already present in the codebase rather than inventing new ones:

  • The role/actionRole plumbing already threaded from CodeBuildStepCodeBuildFactoryPropsCodeBuildFactory.produceAction is extended the same way for assumeRole.
  • The "grant sts:AssumeRole on an optional role" idiom already used in docker-credentials.ts (grantee.grantPrincipal.addToPrincipalPolicy(...)) is reused via IRole.grant().

Files changed:

  • packages/aws-cdk-lib/pipelines/lib/codepipeline/codebuild-step.ts new assumeRole prop.
  • packages/aws-cdk-lib/pipelines/lib/codepipeline/private/codebuild-factory.ts new assumeRole on CodeBuildFactoryProps, threaded through fromCodeBuildStep, grant + buildspec command injection in produceAction.
  • packages/aws-cdk-lib/pipelines/test/codepipeline/codebuild-step.test.ts new unit test.

Describe any new or updated permissions being added

When assumeRole is set, the CodeBuild project's execution role is granted:

{
  "Effect": "Allow",
  "Action": "sts:AssumeRole",
  "Resource": "<assumeRole ARN>"
}

No other permissions are added, and nothing changes for callers who don't set assumeRole (opt-in, default undefined).

Description of how you validated changes

  • Added a unit test (assumeRole grants sts:AssumeRole and injects profile commands) in codebuild-step.test.ts, following the existing style of the neighboring role passed it used for project and code build action test. It asserts:
    1. An AWS::IAM::Policy statement granting sts:AssumeRole on the target role's ARN is attached to the CodeBuild project's role.
    2. The generated AWS::CodeBuild::Project buildspec's install phase contains the aws configure / export AWS_PROFILE commands.
  • Ran the full pipelines unit test suite locally: yarn test pipelines → 211/212 passing (all passing, including the new test), no regressions in the other 21 test suites.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team July 11, 2026 21:45
@github-actions github-actions Bot added effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels Jul 11, 2026

@aws-cdk-automation aws-cdk-automation left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ Features must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

✅ A exemption request has been requested. Please wait for a maintainer's review.

@umbertocicciaa

Copy link
Copy Markdown
Author

The pull request linter fails with the following errors:

❌ Features must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

Require an exception (or am i missing something?)

@aws-cdk-automation aws-cdk-automation added pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. labels Jul 11, 2026
@umbertocicciaa umbertocicciaa marked this pull request as ready for review July 11, 2026 21:58
@aws-cdk-automation aws-cdk-automation added the pr/needs-further-review PR requires additional review from our team specialists due to the scope or complexity of changes. label Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 pr/needs-further-review PR requires additional review from our team specialists due to the scope or complexity of changes. pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(pipelines): add assumeRole parameter to CodeBuildStep

2 participants