I have a module which I need to call multiple time with almost identical variable values.
The module requires handful of input variables. I will like to avoid duplicating the variables in every module call.
Is it possible to initialise a map with required variable names as keys and pass that map instead of every variable individually?
Any other suggestion to remove duplicates is appreciated as well.
Similar to calling a function in a statically typed language, Terraform requires that you define each module argument explicitly so that Terraform can statically check that all of the argument names are correct and return an early error if not.
However, if you have a common set of values that you need to pass to many modules then one potential answer is to define a single input variable of an object type and then pass a shared object of that type into many of your modules. You would then only need a single argument “hard-coded” in each module block, to assign that shared object to the variable. This is like passing an object as an argument to a function: the child module can access all of the data in the object through a single argument.
Thanks for the suggestion.
I though of that, but it takes away certain things like providing default and adding conditions on the variables.
Let me know if I am wrong. Otherwise for now I will have to keep the duplicate calls (Only the variable assignments are duplicate. I have initialised the values to be assigned using a local variable map, so at least the values are not duplicate).
The forthcoming Terraform 1.3 is going to introduce the possibility of setting a default for an optional nested attribute, so if you are willing to do it without the defaults for now then you could add default values later once v1.3.0 is released and you’re ready to upgrade to it.
If by conditions you mean
validation blocks then it’s true that each individual attribute cannot have its own conditions, but you can still declare validation rules on the whole object where the condition expression refers to an individual attribute, and so you can still reject invalid values assigned to a nested attribute.
Thanks a lot. That was really helpful. It seems I can work with your suggestion. Thanks once again.