Jananath Banuka
Jananath Banuka

Reputation: 3863

Terraform iterate over a nested iterate

I have this terraform code and it has two dynamic blocks:


# block one
  dynamic "backend_address_pool" {
    for_each = var.backend_pools

    content {
      name      = each.key
      fqdns     = each.value["fqdns"]
      ip_addresses = each.value["ip_addresses"]
    }
  }    

# block two
  dynamic "request_routing_rule" {
    for_each = var.request_routing_rules

    content {
      name      = each.key
      rule_type     = each.value["rule_type"]
      http_listener_name = each.value["http_listener_name"]
      backend_address_pool_name = <How to get the names of the block1's address pools here>
      backend_http_settings_name = each.value["backend_http_settings_name"]
    }
  }

And in the block1 I have iterate over multiple address pools namely pool1, pool2, pool3 and so on, in the block2, I need to get the values of the each block iteration and assign it to the value backend_address_pool_name in the block2

How can I do this?

So in the block2, is there a way we can refer the values of each iteration as dynamic.backend_address_pool and assign to the value backend_address_pool_name in block2 ?

Here's my variables.tf

variable "backend_pools" {
  type = map(object({
    name         = string
    fqdns        = list(string)
    ip_addresses = list(string)
  }))

  description = "The configuration for backend pools"
}

variable "request_routing_rules" {
  type = map(object({
    rule_type                  = string
    http_listener_name         = string
    backend_address_pool_name  = string
    backend_http_settings_name = string
  }))

  description = "The configuration of routing rules for backend pools"
}

here's my variables.tfvars

backend_pools = {
    pool1 = {
      fqdns        = ["some-value1", "some-value2", "some-value3"]
      ip_addresses = ["10.0.0.0"]
    },
    pool2 = {
      fqdns        = ["some-value1", "some-value2", "some-value3"]
      ip_addresses = ["10.0.0.0"]
    },
    pool3 = {
      fqdns        = ["some-value1", "some-value2", "some-value3"]
      ip_addresses = ["10.0.0.0"]
    }        
}

request_routing_rules = { 
    pool1 = {
      rule_type                  = "Basic"
      http_listener_name         = ""
      backend_address_pool_name  = ""
      backend_http_settings_name = ""
    },
    pool2 = {
      rule_type                  = "Basic"
      http_listener_name         = ""
      backend_address_pool_name  = ""
      backend_http_settings_name = ""
    }
    pool3 = {
      rule_type                  = "Basic"
      http_listener_name         = ""
      backend_address_pool_name  = ""
      backend_http_settings_name = ""
    }        
 }

Upvotes: 0

Views: 505

Answers (1)

rk92
rk92

Reputation: 593

To add onto my comment above you could try something like:

backend_pool_and_request_routing_rules = { 
    pool1 = {
      fqdns                     = ["some-value1", "some-value2", "some-value3"]
      ip_addresses               = ["10.0.0.0"]
      rule_type                  = "Basic"
      http_listener_name         = ""
      backend_address_pool_name  = ""
      backend_http_settings_name = ""
    },

# block one
  dynamic "backend_address_pool" {
    for_each = var.backend_pool_and_request_routing_rules

    content {
      name      = each.key
      fqdns     = each.value["fqdns"]
      ip_addresses = each.value["ip_addresses"]
    }
  }    

# block two
  dynamic "request_routing_rule" {
    for_each = var.backend_pool_and_request_routing_rules

    content {
      name      = each.key
      rule_type     = each.value["rule_type"]
      http_listener_name = each.value["http_listener_name"]
      backend_address_pool_name = each.key
      backend_http_settings_name = each.value["backend_http_settings_name"]
    }
  }

Upvotes: 1

Related Questions