Skip to content

Commit 50bc645

Browse files
committed
Add variable validation rule to make sure there isn't subnet with a route
for 0.0.0.0/0 CIDR and connect_to_public_natgw set to true
1 parent a3818e8 commit 50bc645

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ EOF
257257
error_message = "Any subnet type `name_prefix` must not contain \"/\"."
258258
condition = alltrue([for _, v in var.subnets : !can(regex("/", try(v.name_prefix, "")))])
259259
}
260+
261+
# We check here if there exists at least one subnet that meets the following criteria:
262+
# a. The subnet has a route with the destination CIDR block of "0.0.0.0/0".
263+
# b. The subnet has the 'connect_to_public_natgw' attribute set to true.
264+
validation {
265+
error_message = "Route with CIDR '0.0.0.0/0' is mutually exclusive with 'connect_to_public_natgw'."
266+
condition = !anytrue(
267+
[
268+
for name, subnet in var.subnets :
269+
anytrue(
270+
[
271+
for route in lookup(subnet, "routes", []) :
272+
lookup(route, "destination_cidr_block", "") == "0.0.0.0/0"
273+
]
274+
) && lookup(subnet, "connect_to_public_natgw", false)
275+
]
276+
)
277+
}
260278
}
261279

262280
variable "tags" {

0 commit comments

Comments
 (0)