pkaramol
pkaramol

Reputation: 19342

Not suitable value for terraform variable given the type declaration

I have a module that accepts the following variable

variable "policy" {
  description = "The policy to be created"
  type = map(list(object({
    path = string
    capabilities = list(string)
  })))
}

I am then calling the above module from another one

module "policies" {
  source = "../path/to/module/above"

  for_each = var.policies.policies
 
  policy = each.value

}

Where var.policies is declared in terragrunt.hcl

locals {
  policies = yamldecode(file("config.yaml"))
}

inputs = {
  policies = local.policies
}

and config.yaml

policies:
  policy-test-1:
    - capabilities:
        - read
        - create
      path: /foo/lala
    - capabilities:
        - read
        - create
      path: /bar/lala
  policy-test-2:
    - capabilities:
        - update
        - delete
      path: /foo/lala

This fails:

╷
│ Error: Invalid value for input variable
│
│   on main.tf line 38, in module "policies":
│   38:   policy = each.value
│
│ The given value is not suitable for
│ module.policies["policy-test-2"].var.policy declared at
│ ../path/to/module/above/variables.tf:5,1-18: map of
│ list of object required.
╵
╷
│ Error: Invalid value for input variable
│
│   on main.tf line 38, in module "policies":
│   38:   policy = each.value
│
│ The given value is not suitable for
│ module.policies["policy-test-1"].var.policy declared at
│ ../path/to/module/above/variables.tf:5,1-18: map of
│ list of object required.

Why isn't each var.policies.policies a map of list of objects? Isn't supposed to be an

policy-test-1 : {
  [
    {
      capabilities = ["create", "read"]
      path         = "/foo/lala"
    },
    {
      capabilities = ["create", "read"]
      path         = "/bar/lala"
    }
  ]
}

which is a map (with key policy-test-1) of a list of objects, each object in the list being of

  {
    path = string
    capabilities = list(string)
  }

type?

Upvotes: 1

Views: 2380

Answers (1)

Marcin
Marcin

Reputation: 238209

The correct type for your policy is a list, not a map, because your each.value will be a list:

variable "policy" {
  description = "The policy to be created"
  type = list(object({
    path = string
    capabilities = list(string)
  }))
}

Upvotes: 1

Related Questions