Passing a null value as a module input argument will override any default value.
Setting nullable to false ensures that the variable value will never be null within the module. If nullable is false and the variable has a default value, then Terraform uses the default when a module input argument is null.
That is the relevant documentation, and it sounds exactly like what is happening here – null is being taken in place of the variable default. If you want the default value when an explicit null is assigned, then you must set nullable = false in the variable declaration.
If the provider isn’t giving a good error message, I would just find any other way to get the value to show up in the plan. Probably the easiest is to assign it into a null_resource.
Default values only apply when the caller provides no value at all, either by omitting the argument in the module block or (for non-nullable variables) explicitly setting it to null.
If the caller sends "" then they have now defined the variable to be an empty string and so the default cannot apply.
If you want to also treat an empty string as “unset” then you won’t be able to do that using the built-in capabilities of input variables but there are some other options:
Write a validation block inside the variable block which considers empty string as an invalid value and tells the caller to use null instead.
Set the default value for the variable to be "" and then use other logic inside your module to select another value instead whenever the given value is an empty string. This is effectively implementing your own version of default values using expressions in your module, so you can choose your own rules for what “unset” means.
@apparentlymart ,
the “” != NULL, makes a lot of sense to me. I just wish i saw it somewhere mentioned explicitly in the documentation. But maybe I just didn’t read carefully enough.
The “validation” is probably a way forward for me.
I think what I recommended above is the result of a combination of a few different documented behaviors. In case it’s useful for further learning, here are the two main ones that cover what we discussed here:
Disallowing Null Input Values discusses the nullable = false argument that you were discussing with @jbardin earlier in this conversation. It mentions how the treatment of null differs depending on how you set nullable.
Types and Values includes some discussion about what null represents throughout the language:
a value that represents absence or omission. If you set an argument of a resource to null, Terraform behaves as though you had completely omitted it — it will use the argument’s default value if it has one, or raise an error if the argument is mandatory.
This paragraph admittedly mentions resources specifically even though this idea of null representing omission is true of most parts of the Terraform language. Input variables have a special historical exception controlled by this special nullable option, but if you set nullable = false then they will behave exactly how resource arguments behave, as described here.
I understand that it can often be hard to collect information from various parts of the documentation together to develop a mental model about a new feature you aren’t already familiar with. I wish it were easier to draw all of the connections between all of the language features in all of the different ways they are used, but unfortunately many language features (including null) can be used in many different contexts and so it isn’t really viable to define them in full everywhere we discuss them, or to predict every possible combination of features someone might have questions about.
Hopefully the above links are still helpful in retrospect to give some additional background information beyond what we discussed directly here!