Skip to content

Commit 2580c9e

Browse files
authored
feat: add support for disabling IGW for public subnets (#457)
1 parent b062031 commit 2580c9e

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,12 @@ It is possible to integrate this VPC module with [terraform-aws-transit-gateway
287287
| create\_database\_nat\_gateway\_route | Controls if a nat gateway route should be created to give internet access to the database subnets | `bool` | `false` | no |
288288
| create\_database\_subnet\_group | Controls if database subnet group should be created (n.b. database\_subnets must also be set) | `bool` | `true` | no |
289289
| create\_database\_subnet\_route\_table | Controls if separate route table for database should be created | `bool` | `false` | no |
290+
| create\_egress\_only\_igw | Controls if an Egress Only Internet Gateway is created and its related routes. | `bool` | `true` | no |
290291
| create\_elasticache\_subnet\_group | Controls if elasticache subnet group should be created | `bool` | `true` | no |
291292
| create\_elasticache\_subnet\_route\_table | Controls if separate route table for elasticache should be created | `bool` | `false` | no |
292293
| create\_flow\_log\_cloudwatch\_iam\_role | Whether to create IAM role for VPC Flow Logs | `bool` | `false` | no |
293294
| create\_flow\_log\_cloudwatch\_log\_group | Whether to create CloudWatch log group for VPC Flow Logs | `bool` | `false` | no |
295+
| create\_igw | Controls if an Internet Gateway is created for public subnets and the related routes that connect them. | `bool` | `true` | no |
294296
| create\_redshift\_subnet\_group | Controls if redshift subnet group should be created | `bool` | `true` | no |
295297
| create\_redshift\_subnet\_route\_table | Controls if separate route table for redshift should be created | `bool` | `false` | no |
296298
| create\_vpc | Controls if VPC should be created (it affects almost all resources) | `bool` | `true` | no |

main.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ resource "aws_vpc_dhcp_options_association" "this" {
8989
# Internet Gateway
9090
###################
9191
resource "aws_internet_gateway" "this" {
92-
count = var.create_vpc && length(var.public_subnets) > 0 ? 1 : 0
92+
count = var.create_vpc && var.create_igw && length(var.public_subnets) > 0 ? 1 : 0
9393

9494
vpc_id = local.vpc_id
9595

@@ -103,7 +103,7 @@ resource "aws_internet_gateway" "this" {
103103
}
104104

105105
resource "aws_egress_only_internet_gateway" "this" {
106-
count = var.create_vpc && var.enable_ipv6 && local.max_subnet_length > 0 ? 1 : 0
106+
count = var.create_vpc && var.create_egress_only_igw && var.enable_ipv6 && local.max_subnet_length > 0 ? 1 : 0
107107

108108
vpc_id = local.vpc_id
109109

@@ -134,7 +134,7 @@ resource "aws_route_table" "public" {
134134
}
135135

136136
resource "aws_route" "public_internet_gateway" {
137-
count = var.create_vpc && length(var.public_subnets) > 0 ? 1 : 0
137+
count = var.create_vpc && var.create_igw && length(var.public_subnets) > 0 ? 1 : 0
138138

139139
route_table_id = aws_route_table.public[0].id
140140
destination_cidr_block = "0.0.0.0/0"
@@ -146,7 +146,7 @@ resource "aws_route" "public_internet_gateway" {
146146
}
147147

148148
resource "aws_route" "public_internet_gateway_ipv6" {
149-
count = var.create_vpc && var.enable_ipv6 && length(var.public_subnets) > 0 ? 1 : 0
149+
count = var.create_vpc && var.create_igw && var.enable_ipv6 && length(var.public_subnets) > 0 ? 1 : 0
150150

151151
route_table_id = aws_route_table.public[0].id
152152
destination_ipv6_cidr_block = "::/0"
@@ -199,7 +199,7 @@ resource "aws_route_table" "database" {
199199
}
200200

201201
resource "aws_route" "database_internet_gateway" {
202-
count = var.create_vpc && var.create_database_subnet_route_table && length(var.database_subnets) > 0 && var.create_database_internet_gateway_route && false == var.create_database_nat_gateway_route ? 1 : 0
202+
count = var.create_vpc && var.create_igw && var.create_database_subnet_route_table && length(var.database_subnets) > 0 && var.create_database_internet_gateway_route && false == var.create_database_nat_gateway_route ? 1 : 0
203203

204204
route_table_id = aws_route_table.database[0].id
205205
destination_cidr_block = "0.0.0.0/0"
@@ -223,7 +223,7 @@ resource "aws_route" "database_nat_gateway" {
223223
}
224224

225225
resource "aws_route" "database_ipv6_egress" {
226-
count = var.create_vpc && var.enable_ipv6 && var.create_database_subnet_route_table && length(var.database_subnets) > 0 && var.create_database_internet_gateway_route ? 1 : 0
226+
count = var.create_vpc && var.create_egress_only_igw && var.enable_ipv6 && var.create_database_subnet_route_table && length(var.database_subnets) > 0 && var.create_database_internet_gateway_route ? 1 : 0
227227

228228
route_table_id = aws_route_table.database[0].id
229229
destination_ipv6_cidr_block = "::/0"
@@ -926,7 +926,7 @@ resource "aws_route" "private_nat_gateway" {
926926
}
927927

928928
resource "aws_route" "private_ipv6_egress" {
929-
count = var.create_vpc && var.enable_ipv6 ? length(var.private_subnets) : 0
929+
count = var.create_vpc && var.create_egress_only_igw && var.enable_ipv6 ? length(var.private_subnets) : 0
930930

931931
route_table_id = element(aws_route_table.private.*.id, count.index)
932932
destination_ipv6_cidr_block = "::/0"

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,3 +2330,15 @@ variable "flow_log_max_aggregation_interval" {
23302330
type = number
23312331
default = 600
23322332
}
2333+
2334+
variable "create_igw" {
2335+
description = "Controls if an Internet Gateway is created for public subnets and the related routes that connect them."
2336+
type = bool
2337+
default = true
2338+
}
2339+
2340+
variable "create_egress_only_igw" {
2341+
description = "Controls if an Egress Only Internet Gateway is created and its related routes."
2342+
type = bool
2343+
default = true
2344+
}

0 commit comments

Comments
 (0)