Route tables configuration in AWS - Commands & Configuration
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.
terraform init
terraform apply -auto-approve
-auto-approve - Automatically approves the plan without asking for confirmationaws ec2 describe-route-tables --filters "Name=vpc-id,Values=vpc-0a1b2c3d4e5f6g7h8"--filters - Filters results to show only route tables for the specified VPCIf you remember nothing else from this pattern, remember: route tables tell your cloud network where to send traffic, like a map for your data.