how to get route table ID in typescript with AWS CDK

Viewed 1167
   // create a nat gateway 
   const natGateway = new ec2.CfnNatGateway(this, 'nat-gateway-1',{
      subnetId: mySubnet.subnetId,
      allocationId: new ec2.CfnEIP(this, 'eip-1', {
        domain: 'vpc'
      }).attrAllocationId
    })
    // create a route table
    const routeTable = new ec2.CfnRouteTable(this, 'route-table-1', {
      vpcId: vpc.vpcId,
    })
    // create route
    const route = new ec2.CfnRoute(this, 'route-1', {
      // how to get the route table id?
      routeTableId: 'how to get the id?',
      natGatewayId: 'how to get the id?',
      destinationCidrBlock: vpc.vpcCidrBlock
    })

When I create a route table or a nat gateway, how can I get the ID? I am working with typescript

1 Answers

!Ref on NatGateway resource gives NatGatewayId

Similarly !Ref on RouteTable resource give routeTableId

We just need routeTable.ref and natGateway.ref to get routeTableId and natGatewayId.

const natGateway = new ec2.CfnNatGateway(this, "nat-gateway-1", {
  subnetId: mySubnet.ref,
  allocationId: new ec2.CfnEIP(this, "eip-1", {
    domain: "vpc",
  }).attrAllocationId,
});

// create a route table
const routeTable = new ec2.CfnRouteTable(this, "route-table-1", {
  vpcId: this.vpc.ref,
});

// create route
const route = new ec2.CfnRoute(this, "route-1", {
  // how to get the route table id?
  routeTableId: routeTable.ref,
  natGatewayId: natGateway.ref,
  destinationCidrBlock: "0.0.0.0/0",
});
Related