I have a similar requirement (see here) and thought it useful to add @adamo89's comment in a new post as runnable code:
locals {
vpc = {
for k, v in var.vpcs : k =>
k != "mgmt" ?
[
for i in range(2 * var.amount_of_az) : (
i <= 2 ?
(cidrsubnet(v.cidr_block, 8, i + 10)) :
(cidrsubnet(v.cidr_block, 8, i + 20 - var.amount_of_az))
)
] :
[for i in range(0, var.amount_of_az) : cidrsubnet(v.cidr_block, 8, i)]
}
}
variable "vpcs" {
type = object({
mgmt = object({
cidr_block = string
})
dev = object({
cidr_block = string
})
})
default = {
mgmt = {
cidr_block = "10.10.0.0/16"
},
dev = {
cidr_block = "10.20.0.0/16"
}
}
}
variable "amount_of_az" {
type = number
default = 3
}
output "vpc" {
value = local.vpc
}