79233877

Date: 2024-11-28 12:27:17
Score: 1
Natty:
Report link

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
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @adamo89's
  • Low reputation (1):
Posted by: Robin Bowes