From 091217be7623dc26d3ec0bb39aed8106490b0d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Tobias=20Skjong-B=C3=B8rsting?= Date: Tue, 14 Dec 2021 15:15:57 +0100 Subject: [PATCH] Make both Internet gateway and egress-only Internet gateway optional (#38) --- main.tf | 11 +++++++---- variables.tf | 12 ++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/main.tf b/main.tf index de9e6ff..0bee26d 100644 --- a/main.tf +++ b/main.tf @@ -8,6 +8,9 @@ data "aws_region" "current" {} locals { azs = length(var.availability_zones) > 0 ? var.availability_zones : data.aws_availability_zones.main.names nat_gateway_count = var.create_nat_gateways ? min(length(local.azs), length(var.public_subnet_cidrs), length(var.private_subnet_cidrs)) : 0 + + internet_gateway_count = (var.create_internet_gateway && length(var.public_subnet_cidrs) > 0) ? 1 : 0 + egress_only_internet_gateway_count = (var.create_egress_only_internet_gateway && length(var.public_subnet_cidrs) > 0) ? 1 : 0 } resource "aws_vpc" "main" { @@ -26,7 +29,7 @@ resource "aws_vpc" "main" { } resource "aws_internet_gateway" "public" { - count = length(var.public_subnet_cidrs) > 0 ? 1 : 0 + count = local.internet_gateway_count depends_on = [aws_vpc.main] vpc_id = aws_vpc.main.id @@ -39,7 +42,7 @@ resource "aws_internet_gateway" "public" { } resource "aws_egress_only_internet_gateway" "outbound" { - count = length(var.public_subnet_cidrs) > 0 ? 1 : 0 + count = local.egress_only_internet_gateway_count depends_on = [aws_vpc.main] vpc_id = aws_vpc.main.id } @@ -58,7 +61,7 @@ resource "aws_route_table" "public" { } resource "aws_route" "public" { - count = length(var.public_subnet_cidrs) > 0 ? 1 : 0 + count = local.internet_gateway_count depends_on = [ aws_internet_gateway.public, aws_route_table.public, @@ -69,7 +72,7 @@ resource "aws_route" "public" { } resource "aws_route" "ipv6-public" { - count = length(var.public_subnet_cidrs) > 0 ? 1 : 0 + count = local.internet_gateway_count depends_on = [ aws_internet_gateway.public, aws_route_table.public, diff --git a/variables.tf b/variables.tf index 2e4a6d2..d520883 100644 --- a/variables.tf +++ b/variables.tf @@ -53,6 +53,18 @@ variable "create_nat_gateways" { default = true } +variable "create_internet_gateway" { + description = "Optionaly create an Internet Gateway resource" + type = bool + default = true +} + +variable "create_egress_only_internet_gateway" { + description = "Optionaly create an Egress Only Internet Gateway resource" + type = bool + default = true +} + variable "enable_dns_hostnames" { description = "A boolean flag to enable/disable DNS hostnames in the VPC." type = bool