Skip to content

Commit

Permalink
fix nat gw
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-svensson committed Sep 22, 2021
1 parent ca6ac6b commit f3a2fd9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 2 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ locals {
private_cidrs = tomap({
for i, zone in var.private_subnet_zones : zone => cidrsubnet(var.vpc_cidr, 3, length(local.public_cidrs) + i)
})

create_private_subnets = length(var.private_subnet_zones) > 0
}
7 changes: 3 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ resource "aws_internet_gateway" "igw" {
}

resource "aws_nat_gateway" "nat_gw" {
for_each = aws_subnet.public
for_each = local.create_private_subnets ? aws_subnet.public : {}
allocation_id = aws_eip.elastic_ips[each.key].id
subnet_id = each.value.id
tags = {
Expand Down Expand Up @@ -87,14 +87,13 @@ resource "aws_route_table_association" "private" {
route_table_id = aws_route_table.private[each.key].id
}

resource "aws_route_table_association" "utility" {
resource "aws_route_table_association" "public" {
for_each = aws_subnet.public
subnet_id = each.value.id
route_table_id = aws_route_table.public.id
}

resource "aws_route" "igw_route_private" {
count = length(local.private_cidrs) > 0 ? 1 : 0
resource "aws_route" "igw_route" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
Expand Down

0 comments on commit f3a2fd9

Please sign in to comment.