Newbie question about assigning maps to variable

Hi,

So I have terraform creating a gke cluster using the gke module. There is a section where you configure node pool labels:

node_pools_labels = {
    all = var.labels

    default-node-pool = {
      default-node-pool = true
    }
  }

It is shown in the documentation that the type is of map(map(string))

I’ve defined the variable like so:

variable "labels" {
  type     = map(any) 
}

The values I’ve placed in tfvars

labels = { billing_owner = "joe", purpose = "testing" }

When applying, there are no errors, but the labels do not get created.

But if I do:

node_pools_labels = {
    all = { billing_owner = "joe", purpose = "testing" }

    default-node-pool = {
      default-node-pool = true
    }
  }

This works fine, but I’d like the to use values defined in tfvars. What am I doing wrong?

Thanks for any help.

Hi @tnr,

When debugging strange behavior, my usual first step is to make sure all of my type constraints are explicitly specified, because that way Terraform will give feedback about incorrect values, rather than quietly inferring. In your case, you mentioned that the type you are intending is a map of map of strings, so the appropriate declaration would be the following:

variable "labels" {
  type     = map(map(string))
}

The .tfvars example you showed seems to just be a map(string) value, so that would be rejected by the type constraint above. Perhaps you instead intended this variable to be map(string), in which case the .tfvars file is correct.

If you can confirm which of these is what you intended and whether you still see the problem with a precise type constraint, I’d be happy to try to help further. It would also be helpful if you can indicate exactly which module you mean when you say “the gke module”, so that I can see which documentation you’re referring to.