Terrafrom: Can I set variable type = any dict/object

Is there a way to allow any type of dictionary/object as an input variable?

I have a module to create cron expressions with lambda and I’m trying to add a variable to take in a dictionary to pass into the resource call.

I’d like just allow any dictionary of any length. With any type for the keys and values in it.

Initially, I tried just:

variable vars {
  type = object
}

But that isn’t allowed.

Right now I just have the type empty, so it will accept anything, but that doesn’t seem like good practice.

>Solution :

Ideally this would be the complex type map(any) to specify it must be a map with any type nested. However, you state that:

I’d like just allow any dictionary of any length. With any type for the keys and values in it.

Unfortunately there is a stipulation that map(any) type for a variable declaration must have a consistent structure across all values for the entries. Therefore, you could use map(any) if the input structure is consistent like:

{ 
  "one" = { "a_key" = "a_value", "another_key" = "another_value" },
  "two" = { "a_key" = "value", "another_key" = "the_value" },
}

However an inconsistent structure:

{ 
  "one" = { "another_key" = "another_value" },
  "two" = { "a_key" = "value", "another_key" = 0 },
}

would force the any type, which is the least restrictive and what you stated you did not want, but it is your only option in that situation.

Leave a Reply