Skip to content

Commit

Permalink
feat: NAT gateway and routing (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalc authored Feb 22, 2025
1 parent 39c88b5 commit 3f67775
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,69 @@ resource "aws_subnet" "private" {
Name = "${var.prefix}-private-${var.subnets_private[count.index].availability_zone_short}-${var.suffix}"
}
}

resource "aws_eip" "nat" {

tags = {
Name = "${var.prefix}-${var.suffix}"
}
}

resource "aws_nat_gateway" "main" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public[0].id

tags = {
Name = "${var.prefix}-${var.suffix}"
}

depends_on = [aws_internet_gateway.main]
}

resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id

route {
cidr_block = aws_vpc.main.cidr_block
gateway_id = "local"
}

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}

tags = {
Name = "${var.prefix}-public-${var.suffix}"
}
}

resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id

route {
cidr_block = aws_vpc.main.cidr_block
gateway_id = "local"
}

route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.main.id
}

tags = {
Name = "${var.prefix}-private-${var.suffix}"
}
}

resource "aws_route_table_association" "public" {
count = length(var.subnets_public)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}

resource "aws_route_table_association" "private" {
count = length(var.subnets_private)
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private.id
}

0 comments on commit 3f67775

Please sign in to comment.