Lewis
Lewis

Reputation: 53

Terraform: 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.

Upvotes: 5

Views: 7403

Answers (2)

Karl Casas
Karl Casas

Reputation: 855

You can just it like this

variable vars {
  type = any
}

Upvotes: 0

Matthew Schuchard
Matthew Schuchard

Reputation: 28774

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.

Upvotes: 5

Related Questions