Jananath Banuka
Jananath Banuka

Reputation: 3883

How to use Terraform dynamic block

I am trying to create a azure app gateway using terraform and my code looks like below:

main.tf

# Create Application Gateway
resource "azurerm_application_gateway" "app_gateway" {
  name                = var.name
  resource_group_name = var.appg_name
  location            = var.appg_location
  .
  .
  .

#   backend_address_pool {
#     name = var.backend_address_pool_name
#   }

  dynamic "backend_address_pool" {
    for_each = var.backend_pools
    content {
      name      = backend_pools.value["name"]
      fqdns     = backend_pools.value["fqdns"]
      ip_addresses = backend_pools.value["ip_addresses"]
    }
  }    

  backend_http_settings {
    name                  = var.http_setting_name
    cookie_based_affinity = var.cookie_based_affinity
    path                  = var.backend_http_settings_path
    port                  = var.http_setting_port
    protocol              = var.http_setting_protocol
    request_timeout       = var.http_setting_request_time_out_value
  }
  .
  .
  .
}

And here's my variables.tf - only backend_pools is mentioned here of all the variables

variable "backend_pools" {
  type = list(map(string))
}

And this is my terraform.tfvars

backend_pools = [
 {
   name = "pool1"
   fqdns = "fqdns1"
   ip_addresses = "10.0.0.0"
 },
 {
   name = "pool2"
   fqdns = "fqdns2"
   ip_addresses = "10.0.0.0"
 },
 {
   name = "pool3"
   fqdns = "fqdns3"
   ip_addresses = "10.0.0.0"
 }, 
]

What I am worried about is, according to the terraform docs it is mentioned that the data types of both fqdns and ip_addresses are list, then how can I change my variables.tf and terraform.tfvars and also main.tf which contain the dynamic block accordingly.

What I want to do is, create multiple backend pools and pass the values using a variable file or is it possible to create a json file with multiple backend pool and pass it to the backend_pools parameter?

Can someone please help me on this?

Upvotes: 1

Views: 1858

Answers (1)

Charles Xu
Charles Xu

Reputation: 31424

First, there is a mistake in your Terraform code:

dynamic "backend_address_pool" {
    for_each = var.backend_pools
    content {
      name      = backend_pools.value["name"]
      fqdns     = backend_pools.value["fqdns"]
      ip_addresses = backend_pools.value["ip_addresses"]
    }
  } 

When you use the for_each loop, it should be:

dynamic "backend_address_pool" {
    for_each = var.backend_pools
    content {
      name      = each.value["name"]
      fqdns     = each.value["fqdns"]
      ip_addresses = each.value["ip_addresses"]
    }
  }    

When you use the list in the for_each loop, you can try to change the type like this:

for_each = toset(var.backend_pools)

But the better way is to use the map, so you can change your variables like this:

backend_pools = {
 pool1 = {
    fqdns = "fqdns1"
    ip_addresses = "10.0.0.0"
 },
 pool2 = {
    fqdns = "fqdns2"
    ip_addresses = "10.0.0.0"
 },
 pool3 = {
    fqdns = "fqdns3"
    ip_addresses = "10.0.0.0"
 }
}

Then your dynamic block will look like this:

dynamic "backend_address_pool" {
  for_each = var.backend_pools
  content {
    name      = each.key
    fqdns     = each.value["fqdns"]
    ip_addresses = each.value["ip_addresses"]
  }
} 

Upvotes: 1

Related Questions