Skip to content

Unmanage CDK created Resource

0

Hello, i have a CDK stack that creates a bunch of resources. More specifically an ec2 instance with an elastic_ip address. For other reasons i had to rebuild the app and the way the needed resources are created. Now, I would like to keep the elastic_ip and not have to create a new one for the other stack. Can i somehow extract the elastic ip address out of the stack? So that it becomes "like it was manually created"? Then i could provide it as an input to the other stack. If all fails i could just keep the stack around and only have the IP Address in there. But that feels odd.

2 Answers
3
Accepted Answer

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

EXPERT
answered a year ago
EXPERT
reviewed a year ago
  • Thanks i was totaly not aware of that possibility.

-1

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.

EXPERT
answered a year ago
  • Hey, just FYI the method fromCfnAttributes does not exist on CfnEIP. Instead you have to use CfNEIPAllocation directly from the 'aws-cdk-lib/aws-ec2' package.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.