Skip to content

Use or Remove default unassociated Route Table name in CDK

0

I've created a fresh new VPC for a project with 2 route tables. 1 for public subnets and 1 for private subnets. It all seems to work well, but I did notice that there is a standard route table without any subnets associated to it.

VPC Rotue Tables

I think this is created upon creation of the VPC, but I'm not sure. I'm not able to find it anywhere in the VPC object when I print it out with console.log with cdk synth.

I also tried to cerate my VPC with CfnVPC, but the result stays the same.

This is my code:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { aws_ec2 } from 'aws-cdk-lib';

export class CcwVpcStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Create VPC called CCW with CIDR block 10.1.0.0/16 in us-east-1 with all availability zones in this region.
    const vpc = new aws_ec2.Vpc(this, 'CCW', {
      ipAddresses: aws_ec2.IpAddresses.cidr('10.1.0.0/16'),
      availabilityZones: ['us-east-1a', 'us-east-1b', 'us-east-1c', 'us-east-1d', 'us-east-1e', 'us-east-1f'],
      subnetConfiguration: [],
      createInternetGateway: false,
    });

    const defaultRouteTable = vpc;

    console.log(defaultRouteTable);

    // Create Public Route Table
    const publicRouteTable = new aws_ec2.CfnRouteTable(this, 'PublicRouteTable', {
      vpcId: vpc.vpcId,
      tags: [{ key: 'Name', value: 'PublicRouteTable' }]
    });

    // Create Private Route Table
    const privateRouteTable = new aws_ec2.CfnRouteTable(this, 'PrivateRouteTable', {
      vpcId: vpc.vpcId,
      tags: [{ key: 'Name', value: 'PrivateRouteTable' }]
    });

    // Create Internet Gateway for Public Subnet Routing
    const internetGateway = new aws_ec2.CfnInternetGateway(this, 'InternetGateway', {
      tags: [{ key: 'Name', value: 'CCW-IGW' }]
    });

    new aws_ec2.CfnVPCGatewayAttachment(this, 'VpcGatewayAttachment', {
      vpcId: vpc.vpcId,
      internetGatewayId: internetGateway.ref
    });


    // Create a default route in the public route table to the Internet Gateway
    new aws_ec2.CfnRoute(this, 'PublicRoute', {
      routeTableId: publicRouteTable.ref,
      destinationCidrBlock: '0.0.0.0/0',
      gatewayId: internetGateway.ref
    });

    // Create 1 public subnet in this VPC with CIDR block 10.1.0.0/24
    const publicServicesSubnet = new aws_ec2.CfnSubnet(this, 'PublicServicesSubnet', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.0.0/24',
      availabilityZone: 'us-east-1a',
      tags: [{ key: 'Name', value: 'PublicServicesSubnet' }]
    });

    // Create 3 public subnets in this VPC with CIDR blocks 10.1.1.0/24, 10.1.2.0/24, 10.1.3.0/24
    const publicSubnet1a = new aws_ec2.CfnSubnet(this, 'PublicSubnet1a', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.1.0/24',
      availabilityZone: 'us-east-1a',
      tags: [{ key: 'Name', value: 'PublicSubnet1a' }]
    });

    const publicSubnet1b = new aws_ec2.CfnSubnet(this, 'PublicSubnet1b', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.2.0/24',
      availabilityZone: 'us-east-1b',
      tags: [{ key: 'Name', value: 'PublicSubnet1b' }]
    });

    const publicSubnet1c = new aws_ec2.CfnSubnet(this, 'PublicSubnet1c', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.3.0/24',
      availabilityZone: 'us-east-1c',
      tags: [{ key: 'Name', value: 'PublicSubnet1c' }]
    });


    // Create 3 private subnets in this VPC with CIDR blocks 10.1.11.0/24, 10.1.12.0/24, 10.1.13.0/24
    const privateSubnet1a = new aws_ec2.CfnSubnet(this, 'PrivateSubnet1a', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.11.0/24',
      availabilityZone: 'us-east-1a',
      tags: [{ key: 'Name', value: 'PrivateDbSubnet1a' }]
    });

    const privateSubnet1b = new aws_ec2.CfnSubnet(this, 'PrivateSubnet1b', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.12.0/24',
      availabilityZone: 'us-east-1b',
      tags: [{ key: 'Name', value: 'PrivateSubnet1b' }]
    });

    const privateSubnet1c = new aws_ec2.CfnSubnet(this, 'PrivateSubnet1c', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.13.0/24',
      availabilityZone: 'us-east-1c',
      tags: [{ key: 'Name', value: 'PrivateSubnet1c' }]
    });

    // Create 3 private subnets in this VPC with CIDR blocks 10.1.11.0/24, 10.1.12.0/24, 10.1.13.0/24
    const privateDbSubnet1a = new aws_ec2.CfnSubnet(this, 'PrivateDbSubnet1a', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.21.0/24',
      availabilityZone: 'us-east-1a',
      tags: [{ key: 'Name', value: 'PrivateDbSubnet1a' }]
    });

    const privateDbSubnet1b = new aws_ec2.CfnSubnet(this, 'PrivateDbSubnet1b', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.22.0/24',
      availabilityZone: 'us-east-1b',
      tags: [{ key: 'Name', value: 'PrivateDbSubnet1b' }]
    });

    const privateDbSubnet1c = new aws_ec2.CfnSubnet(this, 'PrivateDbSubnet1c', {
      vpcId: vpc.vpcId,
      cidrBlock: '10.1.23.0/24',
      availabilityZone: 'us-east-1c',
      tags: [{ key: 'Name', value: 'PrivateDbSubnet1c' }]
    });

    // Associate Public Subnets with Public Route Table
    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PublicServicesSubnetAssociation', {
      subnetId: publicServicesSubnet.attrSubnetId,
      routeTableId: publicRouteTable.ref
    });

    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PublicSubnet1aAssociation', {
      subnetId: publicSubnet1a.attrSubnetId,
      routeTableId: publicRouteTable.ref
    });

    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PublicSubnet1bAssociation', {
      subnetId: publicSubnet1b.attrSubnetId,
      routeTableId: publicRouteTable.ref
    });

    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PublicSubnet1cAssociation', {
      subnetId: publicSubnet1c.attrSubnetId,
      routeTableId: publicRouteTable.ref
    });

    // Associate Private Subnets with Private Route Table
    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PrivateSubnet1AAssociation', {
      subnetId: privateSubnet1a.attrSubnetId,
      routeTableId: privateRouteTable.ref
    });

    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PrivateSubnet1BAssociation', {
      subnetId: privateSubnet1b.attrSubnetId,
      routeTableId: privateRouteTable.ref
    });

    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PrivateSubnet1CAssociation', {
      subnetId: privateSubnet1c.attrSubnetId,
      routeTableId: privateRouteTable.ref
    });

    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PrivateDbSubnet1AAssociation', {
      subnetId: privateDbSubnet1a.attrSubnetId,
      routeTableId: privateRouteTable.ref
    });

    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PrivateDbSubnet1BAssociation', {
      subnetId: privateDbSubnet1b.attrSubnetId,
      routeTableId: privateRouteTable.ref
    });

    new aws_ec2.CfnSubnetRouteTableAssociation(this, 'PrivateDbSubnet1CAssociation', {
      subnetId: privateDbSubnet1c.attrSubnetId,
      routeTableId: privateRouteTable.ref
    });

  }
}

I'm not sure how I can access this Route Table, so I can maybe use this as my public router.

3 Answers
3

Hello,

You're right, there is a default route table created automatically when you define a VPC in CDK using aws_ec2.Vpc. This default route table isn't explicitly accessible through the vpc object and cannot be removed.

Don't Use the Default Route Table:

  • Remove the line const defaultRouteTable = vpc;. You don't need to assign the default route table to a variable as you're not using it.

Associate Public Subnets with Your Public Route Table:

  • You've already created the Public Route Table(publicRouteTable)and configured it with an internet gateway. Now, ensure all your public subnets (including publicServicesSubnet, publicSubnet1a, etc.) are associated with this publicRouteTable. Your code for associating public subnets looks good.

Leave Private Subnets Unassociated:

  • Private subnets typically don't need a direct route to the internet. They can access the internet through a NAT Gateway or other mechanisms within your VPC. So, there's no need to associate them with any route table at this point.

https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html

EXPERT

answered 2 years ago

2
Accepted Answer

What you are thinking is correct: just creating a VPC will also create a "main route table" (https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html#main-route-table), default security group, and a default network ACL. They aren't explicitly declared or requested, and there's no way to prevent them from getting created.

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

1

https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html

When you create a VPC, it automatically has a main route table. When a subnet does not have an explicit routing table associated with it, the main routing table is used by default. On the Route tables page in the Amazon VPC console, you can view the main route table for a VPC by looking for Yes in the Main column.

You can't delete the main route table

You can explicitly associate a subnet with the main route table, even if it's already implicitly associated.

EXPERT

answered 2 years ago

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.