Skip to content
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

(core): Cloudformation Stack refactoring #33333

Open
1 of 2 tasks
wladyslawczyzewski opened this issue Feb 7, 2025 · 6 comments
Open
1 of 2 tasks

(core): Cloudformation Stack refactoring #33333

wladyslawczyzewski opened this issue Feb 7, 2025 · 6 comments
Labels
@aws-cdk/core Related to core CDK functionality feature-request A feature should be added or improved. p1

Comments

@wladyslawczyzewski
Copy link
Contributor

wladyslawczyzewski commented Feb 7, 2025

Describe the feature

Recently AWS introduced the possibility to refactor Cfn stacks using AWS CLI and having in mind how's hard to refactor CDK stacks and move resources between stacks, I think this feature needs an implementation and integration within the CDk.

https://aws.amazon.com/about-aws/whats-new/2025/02/reshape-aws-cloudformation-stack-refactoring/

Use Case

Refactoring of CDK stacks - moving resources between stacks, renaming them (logical IDs)

Proposed Solution

Thinking aloud:

  • separate CDK cli command to apply the refactor: a) find the difference in deployed stacks using diff mechanisms, b) find the newly added resources in other stacks that share the same resource (shallow copy equality(?) since logicalId will be different), c) run the refactor Cfn's API calls
  • when synthesising the stack, in Stack:_toCloudFormation() function, we could detect removed resources either by comparing newly generated template or using diff mechanism as above, then creating templates for Cfn's CreateStackRefactor API call for deploy stage.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.178.0

Environment details (OS name and version, etc.)

macOS, node v22

@wladyslawczyzewski wladyslawczyzewski added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Feb 7, 2025
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Feb 7, 2025
@pahud
Copy link
Contributor

pahud commented Feb 7, 2025

Yes, it's awesome to see this feature being announced. Bringing this up to the team for visibility and inputs. Meanwhile, we welcome any inputs and ideas here.

@pahud pahud added p2 and removed needs-triage This issue or PR still needs to be triaged. labels Feb 7, 2025
@glb
Copy link

glb commented Feb 8, 2025

It would be very much appreciated if the implementation of this allowed for separate plan + apply stages that can be done with different permission sets, similar to how you can currently cdk synth and cdk deploy the synthesized assets. Making changes in production stacks is often restrictedcitation needed and not something that's super-fun to YOLO.

@wladyslawczyzewski
Copy link
Contributor Author

hey guys, I needed this for my commercial project I'm working on, so I put together the dirty solution and found some interesting moments:

  1. We need to be able to search through nested stacks as for now selectStacks() doesn't return it and therefore we can't move resource to nested stacks (or between nested stacks)
  2. The resource in target stack should be the copy of resource in source stack - including metadata. Fortunately, we we do the deploy of target stack just after the refactor – metadata updates successfully, so I guess immediately after refactor command we need to run deploy too.
  3. Target stack need to have at least one resource before Cfn can move resource from source stack to it. For empty target stacks I guess we need to add some resource and delete it just after refactor command.

The draft PR is linked to this issue – happy to hear community thoughts on this dirty implementation and possible solutions to the points above.

➜  infra git:(main) ✗ npc cdk deploy "Dev/DataPlatformStorage" --profile "..." # deploy any outstanding changes of source stack before refactor
➜  infra git:(main) ✗ npx cdk deploy "Dev/AnotherTmpPlatformStorage" --profile "..." # first deploy the target stack with at least one resource
➜  infra git:(main) ✗ npx cdk refactor --from-stack "Dev/DataPlatformStorage" --to-stack "Dev/AnotherTmpPlatformStorage" --source-resource "AppleRawBucketBB5C0068" --target-resource "AppleRawBucketBB5C0068" --profile "..."

@mrgrain
Copy link
Contributor

mrgrain commented Feb 15, 2025

@otaviomacedo see here for some real life context

@github-actions github-actions bot added p1 and removed p2 labels Feb 16, 2025
Copy link

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

@mrgrain
Copy link
Contributor

mrgrain commented Feb 20, 2025

RFC is in the works: aws/aws-cdk-rfcs#705

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality feature-request A feature should be added or improved. p1
Projects
None yet
4 participants