Skip to content

Instantly share code, notes, and snippets.

@achenchi7
Last active February 6, 2025 08:21
Show Gist options
  • Save achenchi7/f1ca356ad8b42c9ea1d74f7b9bc5010d to your computer and use it in GitHub Desktop.
Save achenchi7/f1ca356ad8b42c9ea1d74f7b9bc5010d to your computer and use it in GitHub Desktop.
3-tier -> Public & Private RTs
# Private route table
resource "aws_route_table" "private-rt" {
vpc_id = aws_vpc.main-vpc.id
tags = {
Name = "${var.vpc_name}-private-rt"
}
}
# Private route table routes
resource "aws_route" "private-route" {
route_table_id = aws_route_table.private-rt.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat.id
}
# Associate private subnet 1 (app tier) with private route table
resource "aws_route_table_association" "app-privatesubnet1-association" {
subnet_id = aws_subnet.app-privatesubnet1.id
route_table_id = aws_route_table.private-rt.id
}
# Associate private subnet 2 (app tier) with private route table
resource "aws_route_table_association" "app-privatesubnet2-association" {
subnet_id = aws_subnet.app-privatesubnet2.id
route_table_id = aws_route_table.private-rt.id
}
# Associate private subnet 1 (db tier) with private route table
resource "aws_route_table_association" "db-privatesubnet1-association" {
subnet_id = aws_subnet.db-privatesubnet1.id
route_table_id = aws_route_table.private-rt.id
}
# Associate private subnet 2 (db tier) with private route table
resource "aws_route_table_association" "db-privatesubnet2-association" {
subnet_id = aws_subnet.db-privatesubnet2.id
route_table_id = aws_route_table.private-rt.id
}
# Public route table routes
resource "aws_route_table" "main-rt" {
vpc_id = aws_vpc.main-vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "${var.vpc_name}-main-rt"
}
}
# Associate Web tier public subnet 1 with main route table
resource "aws_route_table_association" "publicsubnet1-association" {
subnet_id = aws_subnet.publicsubnet1.id
route_table_id = aws_route_table.main-rt.id
}
# Associate Web tier public subnet 2 with main route table
resource "aws_route_table_association" "publicsubnet2-association" {
subnet_id = aws_subnet.publicsubnet2.id
route_table_id = aws_route_table.main-rt.id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment