Set map of variables per list of namespace for nomad using terraform

115 views Asked by At

Im new in terraform and trying below: Here is my variable:

variable "namespace_vars" {
  description = "environment variable"
  type = list(string)
  default = [ "qa", "dev", "staging" ]
}

variable "job_varaible" {
  description = "job variable"
  type = map
  default = {
    "test_var_name1" = "varaible values"
    "test_var_name2" = "variable values"
  }
}

here is the main.tf:

resource "nomad_variable" "test" {
  path      = "nomad/jobs/"
  for_each = toset(var.namespace_vars)
  namespace = each.value
  items     = {
    "${var.job_varaible.key}" = var.job_varaible.value
  }

but looks should make some iteration of the map as well because it doesn't work in that way. It returns an error:

"${var.job_varaible.key}" = var.job_varaible.values │ ├──────────────── │ │ var.job_varaible is map of string with 2 elements │ │ This map does not have an element with the key "values".

The expected result is to set the variables for all namespace

Looks should make some iteration of the map as well because it doesn't work in that way. Something like nested for_each

1

There are 1 answers

0
Marko E On

Since the items argument is a map, by defining a variable which is of type map and assigning it to the items argument should work. Based on the code from the question:

resource "nomad_variable" "test" {
  path      = "nomad/jobs/"
  for_each = toset(var.namespace_vars)
  namespace = each.value
  items     = var.job_varaible
}