feat(core): support discriminator option in uniqueResourceName - #36629
feat(core): support discriminator option in uniqueResourceName#36629jasdeepbhalla wants to merge 11 commits into
Conversation
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
|
|
||||||||||||||
|
|
||||||||||||||
|
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:
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
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
uniqueResourceNamebefore 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:
Description of changes
Core Implementation:
discriminator?: stringoption toUniqueResourceNameOptionsinterface innames.tspathHash()function inunique-resource-name.tsto include discriminator in the MD5 hash calculation when providedoptionsparameter optional with default empty object for better API ergonomicsHow it works:
hashInput = discriminator ?${pathString}/${discriminator}: pathStringDesign decisions:
Alternatives considered:
Lazy.string(), but this is verbose and requires using Lazy for every resource${Stack.of(this).stackName}-${this.node.id}-ABC, but this loses the hash-based uniqueness guaranteeThe discriminator approach is the cleanest solution that maintains all the benefits of
uniqueResourceNamewhile 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.tsfor the public API:Added unit tests in
unique-resource-name.test.tsfor the private implementation:Integration Test:
integ.unique-resource-name-discriminator.tsthat:Documentation:
aws-cdk-lib/README.mdwith a new "Names" sectionnames.tswith@exampletags showing discriminator usageManual Testing:
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