Skip to content

fix(toolkit-lib): stack definitions for refactor are not considering resource refrences #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025

Conversation

otaviomacedo
Copy link
Contributor

The createStackRefactor method, from the CloudFormation API, requires the client to pass not only the mappings, but also the templates in the state they are supposed to be after the refactor.

Previously, this was done by iterating over the mappings (either computed by the toolkit or prescribed by the user), and using them as a list of instructions on how to modify the deployed stacks, moving resources around as necessary. This is a simple way to put the resources in their correct locations. But it becomes unwieldy when it comes to updating references between resources. Every time a resource is moved, all references to it -- and sometimes from it -- have to be updated as well.

This PR changes implements a different algorithm: take all the synthesized templates as a starting point, and adjust them so that the final templates are equal to the deployed ones up to logical IDs and references. The result is what will be sent to CloudFormation. Specifically this involves:

  • Removing resources that exist locally but not remotely.
  • Adding resources that exist remotely but not locally.
  • Update the CDK construct path to the deployed value. (we will drop this once CloudFormation starts allowing changes in the construct path along with refactors).

One case that is not yet covered is when physical IDs are set. Because we use the physical ID plus the type of the resource to identify it, users are free to change other properties, and we can still recognize that it's the same resource. Since we are simply taking the local resource as the basis and not updating anything other than the construct path, it's possible that it differs from the deployed one. This case will be handled in a following PR.


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

…rences

The `createStackRefactor` method, from the CloudFormation API, requires the client to pass not only the mappings, but also the templates in the state they are supposed to be after the refactor.

Previously, this was done by iterating over the mappings (either computed by the toolkit or prescribed by the user), and using them as a list of instructions on how to modify the deployed stacks, moving resources around as necessary. This is a simple way to put the resources in their correct locations. But it becomes unwieldy when it comes to updating references between resources. Every time a resource is moved, all references to it -- and sometimes from it -- have to be updated as well.

This PR changes implements a different algorithm: take all the synthesized templates as a starting point, and adjust them so that the final templates are equal to the deployed ones up to logical IDs and references. The result is what will be sent to CloudFormation. Specifically this involves:

- Removing resources that exist locally but not remotely.
- Adding resources that exist remotely but not locally.
- Update the CDK construct path to the deployed value. (we will drop this once CloudFormation starts allowing changes in the construct path along with refactors).

One case that is not yet covered is when physical IDs are set. Because we use the physical ID plus the type of the resource to identify it, users are free to change other properties, and we can still recognize that it's the same resource. Since we are simply taking the local resource as the basis and not updating anything other than the construct path, it's possible that it differs from the deployed one. This case will be handled in a following PR.
@github-actions github-actions bot added the p2 label May 28, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team May 28, 2025 13:03
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.21%. Comparing base (de608f6) to head (21b3b82).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #547      +/-   ##
==========================================
+ Coverage   79.09%   79.21%   +0.12%     
==========================================
  Files          47       47              
  Lines        7074     7074              
  Branches      790      796       +6     
==========================================
+ Hits         5595     5604       +9     
+ Misses       1460     1451       -9     
  Partials       19       19              
Flag Coverage Δ
suite.unit 79.21% <ø> (+0.12%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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 changed the title fix: stack definitions for refactor are not considering resource refrences fix(toolkit-lib): stack definitions for refactor are not considering resource refrences May 29, 2025
@aws-cdk-automation aws-cdk-automation added this pull request to the merge queue May 29, 2025
Merged via the queue into main with commit 29a724a May 29, 2025
42 of 44 checks passed
@aws-cdk-automation aws-cdk-automation deleted the otaviom/fix-generate-stack-definitions branch May 29, 2025 09:49
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.

4 participants