- Newest
- Most votes
- Most comments
Hello.
How about setting "RemovalPolicy" to "RETAIN" to retain AWS resources when removing the CDK stack?
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.RemovalPolicy.html
Steps to Unmanage the Elastic IP
Identify the Elastic IP in your Stack: First, you'll need to identify the Elastic IP resource in your existing CDK stack. You can find this in the CloudFormation template or by checking your AWS Management Console (under the EC2 service -> Elastic IPs) to ensure you have the correct Elastic IP.
Remove the Elastic IP from CDK Management: To prevent the Elastic IP from being deleted when the CDK stack is destroyed, you need to modify your CDK stack so that it no longer manages this resource. You can do this by either:
- Commenting out or removing the line of code in your CDK stack where the Elastic IP is created.
- Manually updating the CloudFormation stack to ignore that specific resource, which will leave the Elastic IP intact after the stack is destroyed.
Tag the Elastic IP for Future Use: After the Elastic IP is unmanaged, tag it with a meaningful identifier using either the AWS Console or CLI so you can easily reference it in the new stack. You can give it a tag like Key: UnmanagedEIP, Value: true.
Import the Elastic IP into Your New CDK Stack: In your new CDK stack, you can now reference this existing Elastic IP by importing it, rather than letting the CDK create a new one. Here’s how you can do it:
import * as ec2 from 'aws-cdk-lib/aws-ec2';
const elasticIp = ec2.CfnEIP.fromCfnAttributes(this, 'ExistingEIP', {
allocationId: 'eipalloc-x', // Use the actual Allocation ID from your EIP
});
Deploy the New Stack: Now that your new stack references the existing Elastic IP, you can proceed to deploy the new stack without needing to create a new Elastic IP.
Alternative (If Unmanage Fails): If for any reason you are unable to unmanage the Elastic IP or you would prefer to keep the current stack around, you can modify the stack to only include the Elastic IP and remove all other resources. This allows the Elastic IP to remain intact and not be recreated, but as you said, this can feel odd.
Hey, just FYI the method
fromCfnAttributesdoes not exist on CfnEIP. Instead you have to use CfNEIPAllocation directly from the 'aws-cdk-lib/aws-ec2' package.

Thanks i was totaly not aware of that possibility.