Terraform - applying complex default values
This post explores handling default values in Terraform. It shows merging default values with instance-specific values, using both native merge and the deepmerge module for recursive merging of nested objects. This approach ensures compact, maintainable code and seamless use of default values.
Working with terraform often provides you with the challenge to support default values in your code.
Isn't it easy to use defaults on the TF vars? So why am I writing a blog post about it?
I'm having some development background in object-oriented languages, so i tend to use object types in my TF vars, like this:
This means, someone can provide quite some properties to each defined instance (in this case VRRP instances). Using an object as variable provides me quite some compact form to feed values into the instances (and quite some other benefits), but makes it harder to support defaults.
So, how do I do defaults quick and easy?
Merge defaults
My solution here is to do an object merge from a default value (that can have same properties like my actual config object) and merge these defaults with the instances.
The defaults look like this:
There are two ways to merge.
Native merge
What's happening here:
- We're iterating over the values
- On each value, we're merge the default value
- On each value, we're taking the instance value and keep only fields that have a non-null value set (to make sure defaults will "survive" the merge)
The benefit is easy - on each iteration, each.value uses just the value of each iteration and you don't need to fiddle around with lookup or try between the value and the defaults.
Pushing further - recursive merge a.k.a. deepmerge
If you have been looking very careful in my example, you could wonder if it's possible to set connection_tracking.remote_address using the defaults and enabled per instance. Using the native merge it's not possible because having a connection_tracking object in any instance, it would override the default.
The solution is to use a terraform module - in our case: registry.terraform.io/isometry/deepmerge
Just add this module and slightly adjust the code:
Summary
We're using this approach on several modules and i just can say: it's quite awesome. Using objects for the variables not only provides the ability to merge stuff, it's already in place in the code by just adding the fields to the vars.