Error: The given value is not suitable for child module

Trying to define variables with complex data structures.

As an example

resolver_rules = { "Rule1" = { domain = "domain1" type = "FORWARD" target_ip = [ "1.1.1.1", "2.2.2.2" ] }, "Rule2" = { domain = "domain2" type = "SYSTEM" } }

Which is used to configure resolver rules for Route53. The problem with the above is that it generates an error because each nested map does not have the same fields (resolver rules of type SYSTEM don’t need a target_ip).

To get around this I have to specify an empty list. The variable this is passed in as is a map.

Hi @cyrus-mc,

As you’ve seen, defining a variable as having an object type (or, in this case, a map of object type) means that the given value must have at least the attributes defined in the type constraint, or else Terraform will not consider the value as matching the type constraint.

variable "resolver_rules" {
  type = map(object({
    domain    = string
    type      = string
    target_ip = list(string)
  }))
}

Setting an empty list is one way to meet the constraint, and that can make sense if this causes there to be zero of something created as a result of the list being empty. The other option is to set the attribute to null, which in this case represents an intentional absence of a value, which Terraform will then permit as an argument – although the location where that object is eventually used may end up producing an error in that case if it is not prepared to accept a null list.

1 Like