aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(aws-ec2): Nat Instance & Elastic IP association

Open geoff192 opened this issue 4 years ago • 3 comments

General Issue

Unable to Associate NAT Instance with Elastic IP

The Question

I believe there's a limitation to associate Elastic IPs with NAT Instances.

Documentation Link

https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ec2.NatProvider.html#static-instanceprops

Issue

I created a NAT instance using AWS CDK.

const nat_instance_provider = ec2.NatProvider.instance({
    instanceType: new ec2.InstanceType('t3.micro')
});

Then I created an elastic IP.

const elastic_ip = new ec2.CfnEIP(this, "elastic_ip");

Now I need to associate the EC2 instance with the Elastic IP.

let ec2Assoc = new ec2.CfnEIPAssociation(this, "Ec2Association", {
   eip: elastic_ip.ref,
   instanceId: <EC2 ID> ???
});

The issue I'm facing is that so far I couldn't find a way to get the instance ID and I feel this is a limitation or missing documentation but I might be missing something.

CDK CLI Version

1.131.0 (build 7560c79)

Framework Version

No response

Node.js Version

No response

OS

No response

Language

Typescript

Language Version

No response

Other information

No response

geoff192 avatar Jan 11 '22 00:01 geoff192

Hey @geoff192,

The configuredGateways prop on nat_instance_provider should return the instance id on the gatewayId property of each instance it created. Have you tried this out?

peterwoodworth avatar Jan 14 '22 02:01 peterwoodworth

It's just as @peterwoodworth described it.

This is my solution.

const natGatewayProvider = new ec2.NatInstanceProvider({ /** your props */})

const vpc = new ec2.Vpc(this, 'VPC', {
  natGatewayProvider,
})
   
// you can add EIP after give VPC the NAT Instance
natGatewayProvider.configuredGateways.map(
  (nat, index) =>
    new ec2.CfnEIP(this, `NatEIP${index + 1}`, {
      instanceId: nat.gatewayId,
      tags: [
        { key: 'Name', value: `NatInstanceEIP${index + 1}` },
      ],
    })
)

WinterYukky avatar Jan 14 '22 02:01 WinterYukky

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Jan 16 '22 04:01 github-actions[bot]