New rule: terraform_variable_nullable
#113
bendrucker
started this conversation in
Ideas
Replies: 2 comments
-
This feels a bit pedantic given validation would immediately fail for the case where
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Yeah I guess that's not necessary, if |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Introduce a new
terraform_variable_nullable
rule that requires users to setnullable
for variables with adefault
. This rule would be disabled by default. Eventually rules liketerraform_documented_variables
should be renamed to clearly group with this and other similar rules (e.g.,terraform_variable_description
).Example
The following configuration is invalid and will error when called:
Output:
Cases
Invalid
Would emit a warning telling the user to set
nullable
:We can potentially suggest
nullable = false
as appropriate for most cases, but only the author can determine whethernull
is handled by the module. Terraform has many options for this, includingval == null
and functions likecoalesce
. It may be theoretically possible to detect null handling with static analysis, but it's impractically difficult.Valid
No warning, because the module should be handling
null
as its the default:Ignored as not applicable:
Background
Since Terraform 0.12, setting an attribute to
null
has been equivalent to leaving it unset.https://www.hashicorp.com/blog/terraform-0-12-conditional-operator-improvements
At the time, this behavior only applied to provider attributes, and not module attributes. A module might define this:
Ideally, a caller should be able to do this:
If the calling module specifies a name, it should override the default. Otherwise, the default should be used. From 0.12 through 1.0, this wasn't possible.
Terraform 1.1 introduced
nullable
, which enables this behavior:https://developer.hashicorp.com/terraform/language/values/variables#disallowing-null-input-values
The child module would define:
While the documentation does not explicitly explain this, this was introduced for backward compatibility, since enabling this behavior by default would be a breaking change:
https://discuss.hashicorp.com/t/request-for-feedback-optional-object-type-attributes-with-defaults-in-v1-3-alpha/40550/27
Also: hashicorp/terraform#29832
In most cases, variables with defined defaults should set
nullable = false
. However, there are valid cases fornullable = true
if the module handles null values for that variable.Beta Was this translation helpful? Give feedback.
All reactions