Skip to content

Commit

Permalink
Make both Internet gateway and egress-only Internet gateway optional (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
larstobi authored Dec 14, 2021
1 parent fc27701 commit 091217b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 7 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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

Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 091217b

Please sign in to comment.