Skip to content

fix(aws-cdk): cdk migrate deduplication drops resources with compound identifiers - #1879

Open
Adityaj0 wants to merge 1 commit into
aws:mainfrom
Adityaj0:fix/migrate-deduplicate-compound-identifier
Open

fix(aws-cdk): cdk migrate deduplication drops resources with compound identifiers#1879
Adityaj0 wants to merge 1 commit into
aws:mainfrom
Adityaj0:fix/migrate-deduplicate-compound-identifier

Conversation

@Adityaj0

Copy link
Copy Markdown
Contributor

Reason for this change

cdk migrate --from-scan can silently drop real, distinct resources from the generated CDK app/template. deduplicateResources() in packages/aws-cdk/lib/commands/migrate.ts computed its uniqueness key using only the first key of a resource's ResourceIdentifier map:

const key = Object.keys(resource.ResourceIdentifier!)[0];
const uniqueIdentifer = `${resource.ResourceType}:${key}:${resource.ResourceIdentifier![key]}`;

Some CloudFormation resource types have compound identifiers with more than one key (e.g. AWS::Route53::KeySigningKey is identified by the pair HostedZoneId + Name, as already documented elsewhere in this codebase in packages/@aws-cdk/toolkit-lib/lib/api/resource-import/importer.ts). Two distinct resources that merely share the value of their first identifier key collided on the same dedup key and one silently overwrote the other, with no warning to the user.

Description of changes

Changed deduplicateResources() to build its uniqueness key from the full, sorted set of ResourceIdentifier entries instead of just the first one, so resources are only considered duplicates when their entire identifier matches.

Description of how you validated changes

Added a regression test in packages/aws-cdk/test/commands/migrate.test.ts that scans two resources of type AWS::Route53::KeySigningKey sharing the same HostedZoneId but different Name values, and asserts both are still sent to CreateGeneratedTemplateCommand. Verified the test fails against the pre-fix code (both resources collapse into one) and passes after the fix. Ran the full migrate.test.ts suite — all tests pass.

Checklist

  • Unit tests added/updated
  • Integration tests added/updated (if deploying new resource types or cross-service interactions) - not needed, this is pure logic
  • No manual edits to generated files

Closes #1878

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

… identifiers

deduplicateResources() in lib/commands/migrate.ts built its uniqueness key from
only the FIRST key of a resource's ResourceIdentifier map. Several CloudFormation
resource types (e.g. AWS::Route53::KeySigningKey, identified by the pair
HostedZoneId + Name) have compound identifiers with more than one key. Two
genuinely distinct resources sharing just the first key's value collided on the
same synthetic identifier string and silently overwrote each other in the
dedup map, so `cdk migrate --from-scan` could drop real resources from the
generated template with no warning.

Fix hashes on the full sorted set of ResourceIdentifier entries instead of just
the first one.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.31%. Comparing base (7ff50e7) to head (aeccd94).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1879      +/-   ##
==========================================
+ Coverage   91.10%   91.31%   +0.21%     
==========================================
  Files          80       80              
  Lines       12205    12207       +2     
  Branches     1742     1750       +8     
==========================================
+ Hits        11119    11147      +28     
+ Misses       1050     1024      -26     
  Partials       36       36              
Flag Coverage Δ
suite.unit 91.31% <100.00%> (+0.21%) ⬆️

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cdk migrate: deduplicateResources() drops distinct resources with compound identifiers

2 participants