0
0
AWScloud~5 mins

Route tables configuration in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Route tables control where network traffic goes inside your cloud network. They help your servers and devices talk to each other and to the internet safely and correctly.
When you want to connect your cloud servers to the internet through a gateway.
When you need to allow communication between different parts of your cloud network.
When you want to block or allow traffic to specific destinations.
When setting up private networks that need to access public services securely.
When you want to control traffic flow for security or performance reasons.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "example_vpc" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "example-vpc"
  }
}

resource "aws_internet_gateway" "example_igw" {
  vpc_id = aws_vpc.example_vpc.id
  tags = {
    Name = "example-igw"
  }
}

resource "aws_route_table" "example_route_table" {
  vpc_id = aws_vpc.example_vpc.id
  tags = {
    Name = "example-route-table"
  }
}

resource "aws_route" "internet_access" {
  route_table_id         = aws_route_table.example_route_table.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.example_igw.id
}

resource "aws_subnet" "example_subnet" {
  vpc_id            = aws_vpc.example_vpc.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-east-1a"
  tags = {
    Name = "example-subnet"
  }
}

resource "aws_route_table_association" "example_association" {
  subnet_id      = aws_subnet.example_subnet.id
  route_table_id = aws_route_table.example_route_table.id
}

This Terraform file creates a virtual private cloud (VPC) with a subnet and an internet gateway. It defines a route table that sends all traffic (0.0.0.0/0) to the internet gateway, allowing internet access. The route table is then linked to the subnet so that instances in the subnet use this route.

provider: sets the AWS region.

aws_vpc: creates a private network.

aws_internet_gateway: allows internet access.

aws_route_table: holds routing rules.

aws_route: defines a rule sending all traffic to the internet gateway.

aws_subnet: a smaller network inside the VPC.

aws_route_table_association: links the route table to the subnet.

Commands
This command prepares Terraform to work with AWS by downloading necessary plugins and setting up the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.60.0... - Installed hashicorp/aws v4.60.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command creates the VPC, subnet, internet gateway, route table, and associations as defined in the configuration file.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_vpc.example_vpc: Creating... aws_vpc.example_vpc: Creation complete after 3s [id=vpc-0a1b2c3d4e5f6g7h8] aws_internet_gateway.example_igw: Creating... aws_internet_gateway.example_igw: Creation complete after 2s [id=igw-0a1b2c3d4e5f6g7h8] aws_route_table.example_route_table: Creating... aws_route_table.example_route_table: Creation complete after 2s [id=rtb-0a1b2c3d4e5f6g7h8] aws_route.internet_access: Creating... aws_route.internet_access: Creation complete after 1s aws_subnet.example_subnet: Creating... aws_subnet.example_subnet: Creation complete after 3s [id=subnet-0a1b2c3d4e5f6g7h8] aws_route_table_association.example_association: Creating... aws_route_table_association.example_association: Creation complete after 1s [id=rtbassoc-0a1b2c3d4e5f6g7h8] Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approves the plan without asking for confirmation
This command checks the route tables in the VPC to verify the routes are set correctly.
Terminal
aws ec2 describe-route-tables --filters "Name=vpc-id,Values=vpc-0a1b2c3d4e5f6g7h8"
Expected OutputExpected
{ "RouteTables": [ { "RouteTableId": "rtb-0a1b2c3d4e5f6g7h8", "Routes": [ { "DestinationCidrBlock": "0.0.0.0/0", "GatewayId": "igw-0a1b2c3d4e5f6g7h8", "State": "active", "Origin": "CreateRoute" } ] } ] }
--filters - Filters results to show only route tables for the specified VPC
Key Concept

If you remember nothing else from this pattern, remember: route tables tell your cloud network where to send traffic, like a map for your data.

Common Mistakes
Not associating the route table with the subnet
Without association, the subnet won't use the route table rules, so traffic won't flow as expected.
Always create a route table association resource linking your route table to the subnet.
Setting the destination CIDR block incorrectly, like using 0.0.0.0/1 instead of 0.0.0.0/0
Traffic won't route properly if the destination range is wrong, causing connectivity issues.
Use 0.0.0.0/0 to represent all IPv4 addresses for internet-bound traffic.
Forgetting to create or attach an internet gateway when routing to the internet
Without an internet gateway, traffic cannot leave the VPC to reach the internet.
Create an internet gateway and reference its ID in the route table for internet traffic.
Summary
Initialize Terraform to prepare AWS provider plugins.
Apply the Terraform configuration to create VPC, subnet, internet gateway, route table, and associations.
Verify the route table has the correct route sending all traffic to the internet gateway.