jmct
jmct

Reputation: 65

Iterate over list of maps

I'm trying to iterate over a simple list of maps. Here's a segment of what my module code looks like:

resource "helm_release" "nginx-external" {
  count      = var.install_ingress_nginx_chart ? 1 : 0
  name       = "nginx-external"
  repository = "https://kubernetes.github.io/ingress-nginx"
  chart      = "ingress-nginx"
  version    = var.nginx_external_version
  namespace  = "default"
  lint       = true

  values = [
    "${file("chart_values/nginx-external.yaml")}"
  ]

  dynamic "set" {
    for_each = { for o in var.nginx_external_overrides : o.name => o }

    content {
      name  = each.value.name
      value = each.value.value
    }
  }
}

variable "nginx_external_overrides" {
  description = "A map of maps to override customizations from the default chart/values file."
  type = any
}

And here's a snippet of how I'm trying to call it from terragrunt:

  nginx_external_overrides = [
    { name = "controller.metrics.enabled", value = "false" }
  ]

When trying to use this in a dynamic block, I'm getting:

Error: each.value cannot be used in this context

A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.

Ideally, I would be able to pass any number of maps in nginx_external_overrides to override the settings in the yaml being passed, but am struggling to do so. Thanks for the help.

Upvotes: 2

Views: 1894

Answers (1)

Marcin
Marcin

Reputation: 238867

If you are using for_each in dynamic blocks, you can't use each. Instead, in your case, it should be set:

  dynamic "set" {
    for_each = { for o in var.nginx_external_overrides : o.name => o }

    content {
      name  = set.value.name
      value = set.value.value
    }
  }

Upvotes: 1

Related Questions