0
0
AwsHow-ToBeginner · 4 min read

How to Configure Route Table in AWS: Step-by-Step Guide

To configure a route table in AWS, create or select a route table in your VPC, then add routes that specify destination CIDR blocks and target gateways or instances. Finally, associate the route table with one or more subnets to control traffic flow within your network.
📐

Syntax

A route table in AWS consists of these main parts:

  • Route Table ID: Unique identifier for the route table.
  • Routes: Rules that define where network traffic is directed, each with a Destination CIDR block and a Target (like an internet gateway or NAT gateway).
  • Associations: Links between the route table and one or more subnets in your VPC.

Each route directs traffic for a specific IP range to a target resource.

hcl
resource "aws_route_table" "example" {
  vpc_id = "vpc-12345678"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "igw-12345678"
  }

  tags = {
    Name = "example-route-table"
  }
}

resource "aws_route_table_association" "example_assoc" {
  subnet_id      = "subnet-12345678"
  route_table_id = aws_route_table.example.id
}
💻

Example

This example creates a route table for a VPC, adds a route to send all internet traffic (0.0.0.0/0) to an internet gateway, and associates the route table with a subnet.

hcl
resource "aws_route_table" "my_route_table" {
  vpc_id = "vpc-0abc1234def56789"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "igw-0a1b2c3d4e5f6g7h"
  }

  tags = {
    Name = "my-route-table"
  }
}

resource "aws_route_table_association" "my_assoc" {
  subnet_id      = "subnet-0a1b2c3d4e5f6g7h"
  route_table_id = aws_route_table.my_route_table.id
}
Output
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
⚠️

Common Pitfalls

Common mistakes when configuring AWS route tables include:

  • Not associating the route table with the correct subnet, so traffic does not follow the intended routes.
  • Forgetting to add a route for internet traffic (0.0.0.0/0) when public subnet access is needed.
  • Using incorrect target IDs like a wrong internet gateway or NAT gateway ID.
  • Overlapping CIDR blocks causing routing conflicts.

Always verify subnet associations and route targets carefully.

hcl
resource "aws_route_table" "wrong_route" {
  vpc_id = "vpc-12345678"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "igw-wrongid"
  }
}

# Corrected version
resource "aws_route_table" "correct_route" {
  vpc_id = "vpc-12345678"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "igw-12345678"
  }
}
📊

Quick Reference

Key points to remember when configuring AWS route tables:

  • Route Table: Controls traffic routing in a VPC.
  • Routes: Define destination IP ranges and targets.
  • Associations: Link route tables to subnets.
  • Internet Access: Use 0.0.0.0/0 route with an internet gateway for public subnets.
  • Private Subnets: Use NAT gateway routes for outbound internet access.

Key Takeaways

Create or select a route table in your VPC to control network traffic.
Add routes with destination CIDR blocks and targets like internet gateways.
Associate the route table with subnets to apply routing rules.
Ensure correct gateway IDs and subnet associations to avoid traffic issues.
Use 0.0.0.0/0 route for internet access in public subnets.