How can I do refactoring of CDK code using TypeScript?

bytebridger

I have created a CDK stack and put all my code inside (S3 bucket, Lambda etc.); the code is also in production, so I am not able to delete anything or lose any data and logs.

I want to refactor that stack in a way to create some kind of class that is separated from that stack where I'll put all those resources, and then call it inside of my stack.

I tried to do this but every time CloudFormation tries to create new resources. Is there any better way to do this, without losing anything?

ERROR that I get is related to "resources already exist".

gshpychka

The issue is the fact that when a resource's logical ID changes, the resource is recreated. When you extract your resources into a separate construct, you'll change the logical IDs of all its resources.

There is a built-in partial solution to this explained in the documentation for the Stack.allocateLogicalId method:

If a component is named "Default" it will be omitted from the path. This allows refactoring higher level abstractions around constructs without affecting the IDs of already deployed resources.

So if you instantiate your construct in your stack with the ID Default, the logical IDs of all your resources will stay the same.

An obvious limitation of this is that you can only have a single construct with the name Default in the same scope.

Another solution would be to extract them into a plain TypeScript class that's not a Construct, and simply pass the stack's scope to all the resources instead of passing this in the class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I stop IDEA from reformatting my code when refactoring?

how can I remove generic type from class using refactoring

How do I run my CDK app?

Can I do tooled refactoring for UFT script code, especially when changing function signatures?

How do I determine how many kilobytes there are in a string of code using TypeScript?

How do I attach a "PolicyDocument" object to a TopicPolicy using the AWS CDK?

How do I create an ECS Service using AWS CDK in a VPC with more than 16 subnets

With CDK, how do I synthesize stack via sdk OR how do I get the template in code?

How can I redo this code to do create the table, but using loops?

When using TypeScript in Visual Studio Code, how do I configure TS warnings to be shown as errors?

Is there any way that I can assign Security Group and VPC to my web application hosted in Elastic Beanstalk using AWS CDK code

How can I create a DependsOn relation between EC2 and RDS using aws-cdk

How to execute cdk commands with contextual parameters when cdk is not installed i.e., using npm run cdk?

How do I create an SNS subscription filter involving two attributes using the AWS CDK in Python?

the refactoring option is gone after dart last update, what can I do, I tried restarting and uninstalled Visual Studio Code but nothing happened

How to do refactoring to eliminate type-code if it is used in validation rules?

How can I make code generated by TypeScript using AMD modules, actually execute?

How do I display TypeScript code in "<code>" tags in an Angular app?

How do I construct DHCPOptionsAssociation in AWS CDK using Python

How can I write a date using typescript?

How can I specify a branch folder of a monorepo when deploying an Amplify app using the CDK?

How can I generate AWS SES SMTP credentials using the CDK?

What I can do better in this class? Python class refactoring

How can I improve this? using typescript

How can I create a AWS cluster by specifying the launch configuration using CDK

How do I consume json logs inside Fargate using CDK?

The refactoring method must either RedirectToAction or do nothing. How can I achieve that?

How can I do typed metaprogramming with TypeScript?

How can I bundle Typescript AppSync resolvers in the CDK?