Lookup complex variables dynamically

Hello,

I challenging to implement new functionality by refactoring variables definition without breaking backward compatibility.

Input
Original variable definition

variable "inputs" {
  type        = map(map(bool))
  description = <<EOF
    (Optional) redshift-enabled (bool) - Redshift enabler switcher
    (Optional) cdp-crawlers-enabled (bool) - Crawler enable switcher
  EOF
}

Desired definition:

variable "inputs" {
  type        = map(map(any))
  description = <<EOF
    (Optional) redshift-enabled (bool) - Redshift enabler switcher.
    (Optional) crawlers-enabled (bool) - Crawler enable switcher.
    (Optional) s3_versioning_enabled (bool) - Bucket versioning enable switcher.
    (Optional) tags (map(string)) - input specific tags
  EOF
}

Issues

  1. Trying desired definition would lead to
Error: Incorrect variable type. ... : all map elements must have the same type.

This is not the first time but I would expect any to be any(any) and not homogeneous(any).

  1. Trying converting into the object looks to enforce the structure schema and breaks compatibility.

  2. We are not ready to use terraform 1.3 and try optional attribute .

Hi @chell0veck,

If you must continue using a map to fake an object type, instead of using a real object type, then you will need to declare this as type = any to get the effect you want.

There is no way to specify a map where some elements are boolean and some elements are nested maps.

Using just any on its own (not map(any)) means that the caller can write an object type and then it will pass in to the module without any type conversions. You will need to deal with the fact that Terraform then cannot automatically validate the value in any way, but since you were previously using a map of bool that was presumably already true.

1 Like

@apparentlymart

Thanks a lot.