0

In my terragrunt.hcl file I pass few variables to a module - one of them is map and list

terraform {
  source = "git@github.com:<my_account>/infrastructure-modules.git//iam?ref=v.0.0.9"
}

include {
  path = find_in_parent_folders()
}

inputs = {
  var_1 = "string"
  var_2 = { 
    object1 = { 
      val1 : "a", 
      val2 : "b" 
    },
    object2 = { 
      val3 : "c", 
      val4 : "d" 
  }
}

but when I pass it to a module

resource "google_project_iam_binding" "members" {
  for_each = var.var2
  project  = var.project_name
  role     = "projects/${var.project_name}/roles/${each.key}"
  members  = each.value
}

it doesn't understand it and see it as a string

Error: Invalid for_each argument

  on main.tf line 34, in resource "google_project_iam_binding" "members":
  34:   for_each = var.var2

The given "for_each" argument value is unsuitable: the "for_each" argument
must be a map, or set of strings, and you have provided a value of type
string.

anyone have any thoughts on what might be the issue ?

potatopotato
  • 1,024
  • 2
  • 16
  • 38

2 Answers2

3

Solution was odd but ok, had to add jsondecode(var.var2) for it to work

potatopotato
  • 1,024
  • 2
  • 16
  • 38
3

I ran into a similar issue and had to simply define my terraform variable as type map, that seems to allow terragrunt to pass it as a string and terraform to parse it for you.

variable var2 {
  type = map
}
dweebo
  • 3,222
  • 1
  • 17
  • 19
  • This was my solution too. Using terragrunt and dependency, the a list of subnets was being imported as a string. I had to add `type = list` to for Terraform to figure out what I was trying to do. @dweebo, you saved me. =)) – C. Thomas Brittain Dec 23 '21 at 22:37