Open topic with navigation

# Creating the Route Tables

Route tables define the routing paths between resources in private and public subnets and the Internet.

To create the private and public route tables:

1. Run the following command to create a route table and retrieve its ID:

       aws ec2 create-route-table \
       --vpc-id <VpcId> \
       | jq -r '.RouteTable.RouteTableId'

1. Run the command in Step 1 a second time, to create another route table and retrieve its ID.

Example input and output:

    aws ec2 create-route-table \
    --vpc-id vpc-0143197ca9bd9c117 \
     jq -r '.RouteTable.RouteTableId'

    rtb-0deda70daa09ca3bfw

1. Tag the first route table as private. Run the command:

       aws ec2 create-tags --resources <route table ID> \
       --tags Key=Name,Value=<route table name indicating private>

Example:

    aws ec2 create-tags \
    --resources rtb-0deda70daa09ca3bf \
    --tags Key=Name,Value=srgdemo-private-route-table

1. Repeat Step 3 for the second route table, with the -`-tags` value indicating `public` instead of `private`.

## Associating the Route Tables to Subnets

The route tables will now need to be associated to the subnets you have created.

To associate the route tables to your public subnets:

1. Select one of your public subnets.
2. Associate the public route table to the selected public subnet by running the command:

       aws ec2 associate-route-table \
       --route-table-id <public route table ID> \
       --subnet-id <public subnet ID>

3. Repeat the command in Step 2 for each of the other two public subnets.

To associate the route tables to your private subnets:

1. Select one of your private subnets.
2. Associate the private route table to the selected private subnet by running the command:

       aws ec2 associate-route-table \
       --route-table-id <private route table ID> \
       --subnet-id <private subnet ID>

3. Repeat the command in Step 2 for each of the other two private subnets.

Example input and output:

    aws ec2 associate-route-table \
    --route-table-id rtb-0deda70daa09ca3bf \
    --subnet-id subnet-0fb2ebb5882c061f0

    {
       "AssociationId":"rtbassoc-781d0d1a",
       "AssociationState":{
          "State":"associated"
       }
    }

## Adding the NAT Gateway Route Path to the Private Route Table

To add the NAT gateway route path to the private route table:

1. Run the following command:

       aws ec2 create-route \
       --route-table-id <private route table Id> \
       --destination-cidr-block "0.0.0.0/0" \
       --nat-gateway-id <NAT GW Id>

1. The command will return the creation status. A status of `true` indicates that the request succeeded.

Example input and output:

    aws ec2 create-route \
    --route-table-id rtb-0deda70daa09ca3bf \
    --destination-cidr-block "0.0.0.0/0" \
    --nat-gateway-id nat-013416dad7b7656ea

    {
       "Return":true
    }

## Adding the Internet Gateway Route Path to the Public Routing Table

To add the Internet Gateway route path to the public routing table:

1. Run the following command:

       aws ec2 create-route \
       --route-table-id <public route table Id> \
       --destination-cidr-block "0.0.0.0/0" \
       --gateway-id <Internet Gateway Id>

Example:

    >aws ec2 create-route \
    --route-table-id rtb-0fa9f294a3743c9aa \
    --destination-cidr-block "0.0.0.0/0" \
    --gateway-id igw-0ddcfa7511fe10b43

Next Step: Creating Security Groups