aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.71k stars 3.93k forks source link

(vpc): Access the Internet Gateway for a VPC loaded via "ec2.Vpc.fromLookup" #19094

Open jordan-brough opened 2 years ago

jordan-brough commented 2 years ago

Description

Add the ability to look up the Internet Gateway for a VPC loaded via Vpc.fromLookup.

Use Case

I'd like to be able to add a subnet route like this:

const vpc = ec2.Vpc.fromLookup(this, 'vpc', { 'vpc-XXXXXXXX' });

const subnet = ...;

subnet.addRoute('IgwRoute', {
  routerType: ec2.RouterType.GATEWAY,
  routerId: vpc.internetGatewayId,
  destinationCidrBlock: "0.0.0.0/0",
})

but vpc.internetGatewayId comes back as undefined for me. Is this expected?

Proposed Solution

I believe there is some relevant context here: https://github.com/aws/aws-cdk/issues/5327#issuecomment-961894054

Other information

No response

Acknowledge

wyattisimo commented 2 years ago

vpc.internetGatewayId will be populated if the VPC has any public subnets.

vpc.internetGatewayId will be undefined if the VPC has no subnets or only isolated subnets (see: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Vpc.html#internetgatewayid).

Your example doesn't indicate what type of subnets the VPC is configured with, so I'm not sure if this is the case for you.

corymhall commented 2 years ago

I am unassigning and marking this issue as p2, which means that we are unable to work on this immediately.

We use +1s to help prioritize our work, and are happy to revaluate this issue based on community feedback. You can reach out to the cdk.dev community on Slack to solicit support for reprioritization.

Randy-Ram commented 2 years ago

Could it be that the from_lookup method returns an IVpc object and internet_gateway_id is not a supported attribute of this class?

When I try I get this error:

/private/tmp/brazil-path/run.runtimefarm/lib/python3.8/site-packages/jsii/_reference_map.py", line 149, in __getattr__
    raise AttributeError(f"'%s' object has no attribute '%s'" % (type_info, name))
AttributeError: '<class 'monocdk.Resource'>+<class 'monocdk.aws_ec2._IVpcProxy'>' object has no attribute 'internet_gateway_id'
matt-github-acct commented 1 year ago

I've also encountered this issue. I'm trying to lookup the default vpc (via the lookup method) and access the default internet gateway id associated with the default vpc. The IVPC returned from the lookup method doesn't have an internet gateway id.

jjdaosavanh commented 1 year ago

Hello, is there any info on this? I am also encountering this issue.

aws-apradana commented 1 year ago

Hey I am encountering this issue too. I am trying to do Edge association to associate internet gateway in a vpc I just created with all Public subnets.

const cfnInternetGWRouteTable = new ec2.CfnGatewayRouteTableAssociation(this, 'cfnInternetGWRouteTable', {
      routeTableId: IGWRouteTable.attrRouteTableId,
      gatewayId: vpc.internetGatewayId,
    });

Strange thing is, when I am trying to do a routing using CfnRoute, vpc.internetGatewayId being undefined is okay but when associating it with a route table, it returns error.