Skip to content

feat(cli): allow customizing role session name via CDK_ROLE_SESSION_NAME#1672

Merged
mrgrain merged 3 commits into
aws:mainfrom
UTKARSH698:feat-customizable-role-session-name
Jun 26, 2026
Merged

feat(cli): allow customizing role session name via CDK_ROLE_SESSION_NAME#1672
mrgrain merged 3 commits into
aws:mainfrom
UTKARSH698:feat-customizable-role-session-name

Conversation

@UTKARSH698

@UTKARSH698 UTKARSH698 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Closes #1120

When the CDK CLI assumes a role (e.g. cdk-hnb659fds-deploy-role), it currently hard-codes the STS role session name to aws-cdk-<username>. When deployments run from a shared CI/CD pipeline this makes CloudTrail attribution unhelpful, since every deployment shows the same machine user.

This adds an opt-in CDK_ROLE_SESSION_NAME environment variable that overrides the session name when set, falling back to the existing aws-cdk-<username> default when unset. The variable name follows the CDK_ env var naming convention.

Notes

  • This revives the approach from feat(cli): allow customizing role session name (#21012) aws-cdk#22631, which passed its build but was auto-closed for author inactivity rather than rejected on merit.
  • The change lives in toolkit-lib'''s SdkProvider.withAssumedRole; reading an env var here is consistent with the surrounding awscli-compatible.ts, which already reads AWS_PROFILE, AWS_REGION, etc.

Testing

  • Added a unit test asserting that CDK_ROLE_SESSION_NAME overrides the default session name, alongside the existing default/sanitization tests.

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

…ON_NAME

When the CLI assumes a role (e.g. cdk-hnb659fds-deploy-role) it hard-codes
the session name to `aws-cdk-<username>`, which makes CloudTrail attribution
unhelpful when deployments run from a shared CI/CD pipeline. Honor an optional
`AWS_CDK_ROLE_SESSION_NAME` environment variable to override it, falling back
to the existing default when unset.

Closes aws#1120

@mrgrain mrgrain left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

CDK env vars must start with CDK_.

What is the reason to use an env var over a cli flag? Or both? (You might be right, but I would like to see weighing up the options here.)

Align with the CDK_ env var naming convention per maintainer review.
auto-merge was automatically disabled June 25, 2026 11:05

Head branch was pushed to by a user without write access

@UTKARSH698 UTKARSH698 changed the title feat(cli): allow customizing role session name via AWS_CDK_ROLE_SESSION_NAME feat(cli): allow customizing role session name via CDK_ROLE_SESSION_NAME Jun 25, 2026
@UTKARSH698

Copy link
Copy Markdown
Contributor Author

Thanks for the review @mrgrain.

Naming: Renamed AWS_CDK_ROLE_SESSION_NAME -> CDK_ROLE_SESSION_NAME to follow the CDK_ convention. Updated the code, the unit test, and the README env-var section (pushed in 0025193).

Env var vs CLI flag: A few reasons I leaned to an env var here, though I'm happy to add a flag too if you'd prefer:

  • The role session name is ambient, per-environment context ("which pipeline/runner is assuming this role"), set once for the environment rather than per command. That maps naturally onto an env var, the same way CDK_DEFAULT_ACCOUNT / CDK_DEFAULT_REGION already work.
  • The main motivation is CloudTrail attribution in CI/CD, where it should apply to every cdk invocation (deploy, diff, bootstrap, ...). With an env var you set it once in the job; a flag would have to be threaded onto every command.
  • It also keeps parity with toolkit-lib's existing env-driven auth config (AWS_PROFILE, AWS_REGION, etc. via awscli-compatible.ts).

A CLI flag would be the better fit for per-invocation overrides. If you'd like both, I'm glad to add --role-session-name with the flag taking precedence over the env var. Let me know which direction you prefer and I'll update.

@mrgrain
mrgrain temporarily deployed to integ-approval June 26, 2026 08:20 — with GitHub Actions Inactive
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.13%. Comparing base (6dc2f75) to head (6805d87).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1672      +/-   ##
==========================================
- Coverage   89.14%   89.13%   -0.02%     
==========================================
  Files          77       77              
  Lines       11539    11539              
  Branches     1605     1606       +1     
==========================================
- Hits        10287    10285       -2     
- Misses       1221     1223       +2     
  Partials       31       31              
Flag Coverage Δ
suite.unit 89.13% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mrgrain
mrgrain added this pull request to the merge queue Jun 26, 2026
Merged via the queue into aws:main with commit 9943606 Jun 26, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allow customizing role session name

3 participants