Object attribute defaults on terraform - hello simplicity
Using terraform's object attributes defaults empowers you to create complex input values that still apply default values on a highly granular level. This post shows an example how to use this.
Until now i've mostly been using the "configurable defaults" pattern to expose complex configurations to uses of my terraform code (https://blog.nuvotex.de/default-values-on-terraform-objects/).
At some point in time I felt that this is still too much code that also has some downsides under certain situations (an example is deepmerge and the "null value" problen that unsets nested default objects).
Object attribute defaults to the rescue!
A rather simple yet powerful way is to make use of optional attribute defaults (terraform 1.3+). An example for such a variable is shown here:
What is happening here?
For every object / value we declare the value as optional but still specify a fallback or default. This results in the behavior that terraform will set the default value for us if the attribute is not specified explicitly.
To apply nested defaults easily, we set the default of every nested object to be {}
and terraform will kick in and apply the defaults of this object now in turn for us.
Isn't that somewhat awesome? Easy, well defined - stuff that everyone loves :-)