'Terraform for-each loop on map or maps

I am trying to build a virtual network using a terraform script. I am struggling with creating a loop on the variable "vcn" below. My use case is as follows

  • A "vcn" object can have 1 to many "ads" objects
  • Each "ads" object has 1 to many "subnets" object

I represented the object as shown below ( assuming that is the correct representation )

How do I create a for-each loop in a terraform script?

variable "vcn" {
  type = map(object({
    vcn_cidr_block_in = string
    ads = map(object({
      subnets = map(object({
        sub_cidr_block_in   = string
        sub_display_name_in = string
        sub_dns_label_in    = string
      }))
      ad_name = string
    }))
  }))
}

Appreciate any guidance to solve this problem.

TIA



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source