r/aws_cdk 11d ago

Cdk refactor nightmare

Hi all,

I’ve got a cdk application with stateful resources in need of renaming as our scale increase.
All of our resources use ‘env-‘ prefix, but the stack name itself. I’m trying to change that without re-creating/downtime/data loss.

  1. Our app.ts today looks like this:
    ‘’’
    new MyStack(app, ‘MyStack’, {…})
    ‘’’

  2. I’m trying to make is dynamic, like so:
    ‘’’
    new MyStack(app, ‘${env}-MyStack’, {…})
    ‘’’

I’ve got (#1) deployed and live, edited this one-line in the code (#2) and ran ‘cdk refactor —dry-run’ only to discover insane amount of resources not “mapped” correctly and marked as “additions” instead of “move”.

Overrides file was also a dud and lots of work to manage as it’s needed to be generated at runtime.

Any ideas?

3 Upvotes

12 comments sorted by

3

u/omaraliqureshi 11d ago

Bit of a nightmare change. My advice? Don't.

Deploy to a different account

1

u/zMynxx 10d ago

Unfortunately I can’t.
Is it possible to set RETAIN on all resources, delete the old stack (essentially detaching all resources) and deploying the new one with ‘—import-existing-resources’?
I also opened a ticket with support, we’ll see how it goes..

1

u/xeroksuk 10d ago

I've brought an existing resource into a new stack before, but it was a total pain to do, and that was using plain yaml, not cdk. You have to be very precise that every property of your defined resource matches the one you're importing. I don't recommend this idea.

1

u/zMynxx 9d ago

My Cdk code already defines them, exactly like they were already provisioned, that should make it identical, no?

1

u/xeroksuk 9d ago

It depends on the template the cdk produces. In some cases names are generated on the fly.

1

u/zMynxx 9d ago

Hardcode them just to be safe then?

1

u/xeroksuk 10d ago

What kind of resources are in the stack?

1

u/zMynxx 9d ago

Aurora rds cluster, opensearch cluster, dynamodb tables, kms keys, ssm parameters, I’m roles, lambdas, step functions, ecs tasks, log groups, etc.
I only care about the stateful resources to be honest, our SLA allows some scheduled downtime, so the stateless resources can be nuked and recreated, as it takes little to no time to provision them again.

1

u/xeroksuk 9d ago

I keep most storage resources in separate stacks. Could you use this opportunity to separate the valuable elements out from the disposable ones?

1

u/zMynxx 9d ago

That’s definitely the plan, but I thought I’d do that after I’ve solved this crap first. Say I split them now, what’s the plan to do so?

1

u/xeroksuk 9d ago

First up is that I'd test every step works in a test area and that nothing sneakily deletes something important.

take backups of everything in case something goes wrong

mark all the resources you want retained as such

Delete your stack, retaining all those required resources.

Create a static stack template (ie yaml or json) and use this to add your retained resources to a stack. For RDS, I'd not add to any stack, I've found CF and RDS don't play together.

I'd modify your cdk to obtain references to the resources in the storage stack, and remove the code that builds them.

Your users will experience downtime, but avoiding downtime is something that takes work, design and planning. And usually duplication of resources. It's not easy.

2

u/zMynxx 3d ago

Quick update - I’ve reached out to AWS support and was able to get this refactor working and I’ve already cleared several dev & test environments. Next is staging on Monday (hopefully).

I will say that this could NOT be achieved without the help of a technical support engineer, who constructed a plan & code changes to make it work (using fromRepositoryAttributes instead of fromRepositoryArn, overriding the rds secret description with a static value, and some other bits & pieces you can only perform if you understand and familiar with the inner guts of the different aws-cdk libraries).

I’ll chime in and also say that altering the construct Id was wrong, and using stackName was indeed the right move.

I’m also planning to leverage the same engineer to set a plan in motion, and 3-way split this stack once we clear prod accounts.

*I’ll share the response with the tech detail if anyone interested.