Skip to content

feat(core): support discriminator option in uniqueResourceName - #36629

Open
jasdeepbhalla wants to merge 11 commits into
aws:mainfrom
jasdeepbhalla:36606
Open

feat(core): support discriminator option in uniqueResourceName#36629
jasdeepbhalla wants to merge 11 commits into
aws:mainfrom
jasdeepbhalla:36606

Conversation

@jasdeepbhalla

Copy link
Copy Markdown
Contributor

Issue

Closes #36606.

Reason for this change

Previously, calling Names.uniqueResourceName() multiple times with the same construct would always return the same name because the hash calculation was solely based on the construct's path. This made it impossible to generate multiple different unique names for child resources that need to be created before the child constructs are initialized.

Use Case:
When creating multiple child resources that require unique physical names, developers need to call uniqueResourceName before the child constructs are initialized. Since they can only pass the parent construct (this), they get the same name for all resources, causing conflicts.

Example of the problem:

// These all return the same name
const name1 = Names.uniqueResourceName(this);
const name2 = Names.uniqueResourceName(this);

new SomeResource(this, 'SomeResource1', {
  name: name1,  // Need a unique name here
});

new SomeResource(this, 'SomeResource2', {
  name: name2,  // Need a different unique name here
});

Description of changes

Core Implementation:

  1. Added discriminator?: string option to UniqueResourceNameOptions interface in names.ts
  2. Updated pathHash() function in unique-resource-name.ts to include discriminator in the MD5 hash calculation when provided
  3. Modified top-level resource logic to always include a hash when discriminator is present (even for short names) to ensure uniqueness
  4. Made the options parameter optional with default empty object for better API ergonomics

How it works:

  • The discriminator is included in the hash calculation: hashInput = discriminator ? ${pathString}/${discriminator} : pathString
  • The discriminator is NOT included in the human-readable portion, maintaining consistent output length
  • Different discriminators produce different hashes, ensuring unique names
  • Same discriminator always produces the same name (deterministic)

Design decisions:

  • Discriminator in hash only: We considered including it in the human-readable part, but that would change output length and make names less readable. Including it only in the hash keeps names consistent and clean.
  • Optional parameter: Made discriminator optional to maintain backward compatibility. Existing code continues to work without modification.
  • Hash always included with discriminator: When a discriminator is provided, we always add a hash (even for short top-level resources) to guarantee uniqueness. This ensures the feature works reliably in all cases.

Alternatives considered:

  1. Creating empty constructs: Users could create dummy constructs just for name generation, but this pollutes the construct tree unnecessarily
  2. Using Lazy: Users could defer name resolution with Lazy.string(), but this is verbose and requires using Lazy for every resource
  3. Manual name construction: Users could manually construct names like ${Stack.of(this).stackName}-${this.node.id}-ABC, but this loses the hash-based uniqueness guarantee

The discriminator approach is the cleanest solution that maintains all the benefits of uniqueResourceName while solving the multiple-names-from-same-construct problem.

Describe any new or updated permissions being added

No new or updated IAM permissions are required. This change only affects the core CDK library's name generation utility and does not interact with any AWS services or require additional permissions.

Description of how you validated changes

Unit Tests:

  • Added comprehensive unit tests in names.test.ts for the public API:

    • Different discriminators produce different names
    • Same discriminator produces same name
    • Behavior without discriminator remains unchanged
    • Discriminator is included in hash but not human-readable portion
  • Added unit tests in unique-resource-name.test.ts for the private implementation:

    • Discriminator produces different names for same path
    • Same discriminator produces same name
    • Discriminator forces hash inclusion even for short top-level resources
    • Discriminator works with separators and other options

Integration Test:

  • Created integ.unique-resource-name-discriminator.ts that:
    • Generates multiple unique names from the same construct using different discriminators
    • Creates real AWS resources (SQS queues) with those names
    • Verifies that CloudFormation deployment succeeds (would fail if names weren't unique)
    • Tests backward compatibility by creating a queue without discriminator

Documentation:

  • Updated top-level aws-cdk-lib/README.md with a new "Names" section
  • Enhanced JSDoc comments in names.ts with @example tags showing discriminator usage

Manual Testing:

  • Verified that existing code patterns continue to work
  • Confirmed that names generated with discriminators are unique and deterministic
  • Validated that output length remains consistent regardless of discriminator value

All tests pass and the implementation follows existing codebase patterns and conventions.

Checklist


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

@jasdeepbhalla
jasdeepbhalla requested a review from a team as a code owner January 9, 2026 00:43
@github-actions github-actions Bot added the repeat-contributor [Pilot] contributed between 3-5 PRs to the CDK label Jan 9, 2026
@aws-cdk-automation
aws-cdk-automation requested a review from a team January 9, 2026 00:43
@github-actions github-actions Bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 labels Jan 9, 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.

(This review is outdated)

@aws-cdk-automation
aws-cdk-automation dismissed their stale review January 9, 2026 01:40

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@github-actions

github-actions Bot commented Jan 9, 2026

Copy link
Copy Markdown
Contributor

⚠️ Experimental Feature: This security report is currently in experimental phase. Results may include false positives and the rules are being actively refined.
This security report is NOT a review blocker. Please try merge from main to avoid findings unrelated to the PR.
To suppress a specific rule, see Suppressing Rules.


TestsPassed ✅SkippedFailed
Security Guardian Results48 ran48 passed
TestResult
No test annotations available

@github-actions

github-actions Bot commented Jan 9, 2026

Copy link
Copy Markdown
Contributor

⚠️ Experimental Feature: This security report is currently in experimental phase. Results may include false positives and the rules are being actively refined.
This security report is NOT a review blocker. Please try merge from main to avoid findings unrelated to the PR.
To suppress a specific rule, see Suppressing Rules.


TestsPassed ✅SkippedFailed
Security Guardian Results with resolved templates48 ran48 passed
TestResult
No test annotations available

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jan 14, 2026
@aws-cdk-automation aws-cdk-automation added pr/community-review-timeout pr/needs-maintainer-review This PR needs a review from a Core Team Member and removed pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels May 12, 2026
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label May 12, 2026
@aws-cdk-automation

Copy link
Copy Markdown
Collaborator

This PR has been in the MERGE CONFLICTS state for 3 weeks, and looks abandoned. Note that PRs with failing linting check or builds are not reviewed, please ensure your build is passing

To prevent automatic closure:

  • Resume work on the PR
  • OR request an exemption by adding a comment containing 'Exemption Request' with justification e.x "Exemption Request: "
  • OR request clarification by adding a comment containing 'Clarification Request' with a question e.x "Clarification Request: "

This PR will automatically close in 14 days if no action is taken.

- Resolved merge conflict in packages/aws-cdk-lib/README.md by keeping
  both the arnFor<ResourceName>() paragraph from main and the new Names
  section added by this PR
- Added SqsManagedSseEnabled: true to all SQS queues in the integration
  test to satisfy the sqs-encryption-enabled.guard security check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 pr/community-review-timeout pr/needs-maintainer-review This PR needs a review from a Core Team Member repeat-contributor [Pilot] contributed between 3-5 PRs to the CDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(core): Names.uniqueResourceName should support generating multiple unique names from the same construct

2 participants