How to recreate manually deleted resources from the CDK/CloudFormation
Asked Answered
C

2

11

I have two CDK/Cfn stacks which instantiate application load balancers with SSL certificates. I'm using DNS validation which the CDK manages by creating a Lambda function which requests and validates the certificates.

Unfortunately, those Lambda functions were manually deleted and now when I try to update my CDK resources, CloudFormation attempts to replace these Lambdas but fails because they no longer exist.

I wish that CloudFormation would behave like Terraform and just say "oh that thing I need to replace isn't there, nbd I needed to replace it anyway, so let's carry on" but it does not.

Not sure how to get out of this jam. Any help is appreciated.

Crashland answered 4/12, 2022 at 4:22 Comment(0)
N
17

The easiest fix to this drift is redeploying your CDK app with the deleted resource temporarily removed (e.g. commented out). CloudFormation will "delete" the already deleted resource, bringing the template back into sync with the deployed configuration. Then add back the resource to your app and deploy again. Problem solved.

There's a complication in your case. The missing Lambda function is being constructed indirectly by a higher-level CDK construct. Removing the L2/L3 parent will destroy more resources than just the Lambda. If you want to avoid this collateral damage, you can use escape hatch syntax and the node.tryRemoveChild method to surgically remove the missing Lambda only.

Nawab answered 4/12, 2022 at 11:23 Comment(0)
Y
-1

The easiest way to solve these kinds of issues is to change id in the code.

Let me explain with an example. I have terminated an AWS EC2 instance created with CDK using the code here

instance = ec2.Instance(self, "ec2-instance",

if I want to recreate it I need to change the code to

instance = ec2.Instance(self,"ec2-instance1",

this additional 1 in the string forces the recreation.

I know what you are thinking, <<Do I need to change the code to fix a manual change?>>, crazy eh and yes Terraform is much better,

Year answered 29/8 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.