amazon-web-servicesamazon-ec2amazon-vpcaws-cdkelastic-ip

associate custom Elastic IP to NAT Gateway with AWS CDK


After struggling with that for several hours, here is my question. I am using CDK to create a VPC in the most simple form currently:

let vpc = new Vpc(this, "myVpc", {maxAzs: 1});

This gets me a public Subnet and a private one with the all the Gateways (internet and NAT). My NAT Gateway got a public EIP from the AWS pool. Of course when i destroy the stack and re-create it, i will get a new EIP from AWS, but THIS i dont want.

What i want is: Creating an Elastic IP outside of my CDK project (manually via CLI or AWS Console) and attach it to my NAT GW, so that even after destroying the stack, i can re-attach my (external) EIP to the "new" NAT GW.

So there must be a way to not have the AWS::EC2::NatGateway created automatically by the VPC but manually with the proper EIP association and then attach it to the VPC / Public Subnet. Pretty much the same way i can explicitly define Subnets and associate them with the VPC instead of CDK construct magic.


Solution

  • You can refer here https://github.com/aws/aws-cdk/issues/4067 at the last post.

    You can define EIP allocations then assign it into Nat Gateway while CDK deployment.

    Of course, you must manually create EIP first.