-1

When i want to pass a list of map to a Terraform variable(where i use dynamic block to iterate over this list) i get this error:

│ Cannot use a string value in for_each. An iterable collection is required.

My list of maps in my variable group in Azure Devops:

{ "host" = "mysite1.com", "name" = "healthprobe1", "path" = "/healthcheck1" },
{ "host" = "mysite2.com", "name" = "healthprobe2", "path" = "/healthcheck2" },
{ "host" = "mysite2.com", "name" = "healthprobe3", "path" = "/healthcheck3" }

My variable in variable group:

enter image description here My Terraform configuration which iterate over a list of maps:


The variable:
variable "PROBES" {
  default     = [{}]
}

The dynamic block:

  dynamic "probe" {
    for_each = var.PROBES

    content {
      host                                      = probe.value.host
      interval                                  = 30
      minimum_servers                           = 0
      name                                      = probe.value.name
      path                                      = probe.value.path
      pick_host_name_from_backend_http_settings = false
      protocol                                  = local.protocol
      timeout                                   = 30
      unhealthy_threshold                       = 3
      match {
        status_code = ["200-399"]
      }
    }
  }
JSecurity
  • 85
  • 1
  • 9

1 Answers1

1

One of the options (but needs to be tested) is to define the PROBES variable as string and into your dynamic block change from

dynamic "probe" {
    for_each = var.PROBES
...
}

to

dynamic "probe" {
    for_each = tomap(jsondecode(var.PROBES))
...
}

to explicitly convert your string input variable to a structured object

Reference:

EDIT Incorrectly referencing at first at jsonencode function while this should refer to jsondecode functionality

  • 1
    Thanks you that worked using jsondecode function and adding brackets [] to the variable:[{ "host" = "mysite1.com", "name" = "healthprobe1", "path" = "/healthcheck1" }, { "host" = "mysite2.com", "name" = "healthprobe2", "path" = "/healthcheck2" }, { "host" = "mysite2.com", "name" = "healthprobe3", "path" = "/healthcheck3" }] – JSecurity Mar 23 '23 at 10:09