How to pass a module output into dynamic content block

we organize the terraform structure in below form, but there is problem, or maybe not a problem, just don’t know how to do it, we have dynamic block defined in root modules, we use the root module in below form, but how to pass a module output into dynamic content block with below form, an example of module’s output reference you can observe an output rg_id of resourcegroup module in below example code( module.resourcegroup[“poctesting-poc-rg”].rg_id )

locals {
  ack_map = { for item in var.ack_instances : "${item.name}" => item }
  resourcegroup_map = { for item in var.resource_groups : "${item.resource_group_name}" => item }
  alb_map = { for item in var.alb_instances : "${item.load_balancer_name}" => item }
}

##########################  resource group  ##################################
module "resourcegroup" {
  for_each = local.resourcegroup_map
  source   = "./modules/resourcegroup"

  display_name        = each.value.display_name
  resource_group_name = each.value.resource_group_name
  tags                = local.tags
}

variable "resource_groups" {
  description = "resource group"
  type = list(object({
    display_name        = string
    resource_group_name = string
  }))
}

resource_groups = [
  {
    display_name        = "poctesting-poc-rg"
    resource_group_name = "poctesting-poc-rg"
  }
]

############################  alb  ######################################
module "alb" {
  for_each = local.alb_map
  source   = "./modules/alb"

  alb_name = each.value.display_name
  resource_group_id  = module.resourcegroup["poctesting-poc-rg"].rg_id
  ...
}

variable "alb_instances" {
   type = list(object({
     alb_name = string
     ....
  }))
}

alb_instances = [
 {
    load_balancer_name  = "demo_alb"
    ...
 }
]

############################ ack  ######################################
module "ack" {
  for_each = local.ack_map
  source   = "./modules/ack"

  name = each.value.name
  resource_group_id  = module.resourcegroup["poctesting-poc-rg"].rg_id
  ...
  addons_config = each.value.addons_config # here, how do I refer an output of alb id from above alb module, addons_config in root alb module is a dynamic block, a definition of addons_config map object please see below
}

variable "ack_instances" {
  description = "ack instance variables definition"
  type = list(object({
    name = string
    addons_config = list(object({
      name     = string
      config   = string
      version  = string
      disabled = bool
    }))

    ....
  }))
}


ack_instances = [
  {
    name  = "poctesting-gc-shanghai-poc-ack"
    ...
    addons_config = [
      {
        name = "csi-plugin"
      },
      {
        name = "alb-ingress-controller"
        config = jsonencode({
          albIngress = {
            CreateDefaultALBConfig = true
            LoadBalancerId  = module.alb["demo_alb"].alb_id # here the way to pass alb id to dynamic block value is wrong, only a kind of placeholder to let you know that I need a reference of module output vaule to place here 
          }
        })
      }
    ]
   }
 ]

1 Like