-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
null value is not treated per docs when passed to modules #24142
Comments
This forces module consumers to have knowledge of the default value for the module and specify the same default in their variable definitions. This really should be fixed. |
Can reproduce in 0.12.25, see comment in #21702 |
Hi @oxplot, I agree that the behavior is inconsistent here, and that's confusing. My sense of the ideal behavior is that if a variable value is set to The design challenge in this area is that some modules use We could potentially address this by allowing Unfortunately, given that the current behavior was established in v0.12.0 we cannot just change it now because there will be many modules depending on the current behavior. I'm going to mark this as a breaking change to acknowledge that while it's a valid issue to be addressed we are blocked by finding a responsible way to introduce it that will not cause confusing misbehavior for existing modules. I don't yet know what that approach will be. In the meantime, I think it'd be best to update the documentation for module blocks to be explicit about this exception to the usual interpretation of Thanks for reporting this! |
@apparentlymart I think it's important to consider the converse...a default that is non null that a consumer of the module may or may not set, yet the configuration has to allow for it. This gets even more complex in chained module configurations. |
Agreed that this is unexpected behavior, especially considering the documentation for null in the Expressions documentation.
|
The behavior is inconsistent, but the documentation is not inconsistent, it is wrong. Explicitly so. Fixing the behavior of |
Related to terraform issue: hashicorp/terraform#24142 We can't pass a null value to a module as it is passed explicitly instead of using the default set inside of the module. As a workaround we set empty string as default and set local variables conditionally.
Related to terraform issue: hashicorp/terraform#24142 We can't pass a null value to a module as it is passed explicitly instead of using the default set inside of the module. As a workaround we set empty string as default and set local variables conditionally.
@apparentlymart @jbardin So this issue is now a year and a half old and the documentation still hasn't been updated nor has there been any change to make null work the way the documentation says it's supposed to work. A null should mean What's the work around for this? Am I supposed to know default values of variables in a module whenever I call that module? How many modules actually use "null" as a valid value? And why would they do that? Couldn't you just use an empty string? There have been a lot of tickets that have been closed as duplicates of this issue and yet nothing has been done about this ticket. |
Is there any documentation anywhere that says that a If modules are ignoring the documentation then this isn't a breaking change, this is a major bug and module maintainers are responsible for using something that completely contradicts documentation. After doing some digging, the documentation for |
Still not updates? |
As per Terraform v1.0 Compatibility Promises:
And this is precisely a change which will change the behaviour of countless modules. Modules often contain optional variables with reasonable defaults and a valid use case is to opt out of those defaults in favour of resource's defaults by using
If you would be a maintainer of any module, you'd think otherwise. But hey, the best is to ignore the reality and make it a problem of other people. @apparentlymart |
@vynaloze The reality is, though, that you have absolutely no idea how many modules use This is a bug and should have always been treated as a bug. |
While I have absolutely no idea how many modules are there, neither you do. You suggest to change well-established (as you said: 3 years) behaviour in favour of aligning with documentation (not even a formal language specification). I value maintaining backwards compatibility, you believe that aligning with documentation is more important. I think we won't agree with each other, yet I think we both agree that Hashicorp should finally make a decision and either fix the documentation or code logic. This inconsistency is good for no one. |
I have to agree, this position of you don't know how to unpaint yourselves out of a corner is not an excuse. Nobody is saying it's an easy fix, what we should agree on is that it is a problem worth solving. Why is this discussion still a thing, Hashicorp has already admitted the issue, so can we move to a solution please?
…________________________________
From: Piotr Pawluk ***@***.***>
Sent: Wednesday, October 6, 2021 3:56 AM
To: hashicorp/terraform ***@***.***>
Cc: Butler, Ryan @ Dallas ***@***.***>; Comment ***@***.***>
Subject: Re: [hashicorp/terraform] null value is not treated per docs when passed to modules (#24142)
External
While I have absolutely no idea how many modules are there, neither you do. You suggest to change well-established (as you said: 3 years) behaviour in favour of aligning with documentation (not even a formal language specification).
I value maintaining backwards compatibility, you believe that aligning with documentation is more important. I think we won't agree with each other, yet I think we both agree that Hashicorp should finally make a decision and either fix the documentation or code logic. This inconsistency is good for no one.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_hashicorp_terraform_issues_24142-23issuecomment-2D935784169&d=DwMCaQ&c=jozbAXBGpZCeJmn-Q9SThA&r=04jlXRLWAULcGN4twtAoss0PO49nDy3gx7ijf6oeOQo&m=N89flAD65LVGehCoQOTFBKYT7EXMAwCBOKtr6pUwE0M&s=h22F_2DhtQhHqSam4r3atUqQ0Qrz0RF2x1fEEYkJaCI&e=>, or unsubscribe<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_notifications_unsubscribe-2Dauth_AMKJEQOZMP57Q64HVUVWRCTUFQFNTANCNFSM4KXBZSIA&d=DwMCaQ&c=jozbAXBGpZCeJmn-Q9SThA&r=04jlXRLWAULcGN4twtAoss0PO49nDy3gx7ijf6oeOQo&m=N89flAD65LVGehCoQOTFBKYT7EXMAwCBOKtr6pUwE0M&s=pLgGoI0tQFrNJ0Jf_EKHcgLdBjQVuEFghQo5RnWWPgw&e=>.
Triage notifications on the go with GitHub Mobile for iOS<https://urldefense.proofpoint.com/v2/url?u=https-3A__apps.apple.com_app_apple-2Dstore_id1477376905-3Fct-3Dnotification-2Demail-26mt-3D8-26pt-3D524675&d=DwMCaQ&c=jozbAXBGpZCeJmn-Q9SThA&r=04jlXRLWAULcGN4twtAoss0PO49nDy3gx7ijf6oeOQo&m=N89flAD65LVGehCoQOTFBKYT7EXMAwCBOKtr6pUwE0M&s=2-R_ScewEznP2k-lFF0Rv6YiV3z7UL8qjbXUKJ-FqbQ&e=> or Android<https://urldefense.proofpoint.com/v2/url?u=https-3A__play.google.com_store_apps_details-3Fid-3Dcom.github.android-26referrer-3Dutm-5Fcampaign-253Dnotification-2Demail-2526utm-5Fmedium-253Demail-2526utm-5Fsource-253Dgithub&d=DwMCaQ&c=jozbAXBGpZCeJmn-Q9SThA&r=04jlXRLWAULcGN4twtAoss0PO49nDy3gx7ijf6oeOQo&m=N89flAD65LVGehCoQOTFBKYT7EXMAwCBOKtr6pUwE0M&s=Z-unDpmdqfQ61lOXuOo36KwZDrMAwhfQpnLtsZo6b4g&e=>.
|
i want to reiterate my previous point that is related here. If module A defines a variable X with a default of Y and I am designing module B to wrap module A. I want to supply a variable for X in module B that is passed to module A since it is something I wish the consumer the ability to modify, but this means that this issue kicks in. If the consumer of module B supplies a value of null for the variable I get an error just as stated in this issue. If they omit the value, it can work, but only if I replicate the exact same default from module A in module B's variables. This sucks |
@vynaloze How can this possibly be "well-established" behavior? It's a bug that's not even documented anywhere. If module authors are using this, they did it at their own risk. Why would anyone build something depending on behavior that directly contradicts documentation? |
How about a compromise:
Wouldn't this satisfy everyone? |
Anyone can update the documentation. If you've got the time and want to see this improved, go for it! Simply create a pull request to modify this file: https://github.com/hashicorp/terraform/blob/main/website/docs/language/expressions/types.html.md There are lots of "This sucks", "Hashicorp should do this", "Hashicorp should have done that" comments. Anyone complaining actually paying anything for Terraform? The truth is, this is fairly big change that has the potential to break existing things (which is never good). Terraform (along w/ the rest of the Hashicorp stack) is a great product that greatly simplifies modern DevSecOps for free (for most anyway)... I agree, I'd love to see this fixed as much as anyone, but truth is, it's a fairly simple workaround if your writing a new module....
module "foo" {
source = "./mod1"
}
module "bar" {
source = "./mod1"
optional_input = 10
}
output "foo" {
value = module.foo.foo
}
output "bar" {
value = module.bar.foo
}
variable "optional_input" {
description = "An optional number... If not specified, the default is 5"
type = number
default = null
}
locals {
# If var.optional_input isn't null, use var.optional_input; otherwise, use 5
# as the default
optional_input = var.optional_input != null ? var.optional_input : 5
}
output "foo" {
value = local.optional_input / 5
} Works as needed
|
@nshenry03 Unfortunately, it's not quite that simple a work around. Your solution just omits a parameter if you don't want to set it but that doesn't work if you have to set that parameter and you want to be able to toggle it. This can happen in a number of cases such as having a conditional when passing a module parameter or if you're trying to set a parameter based on a map or a tfvars file. Let's say you have an EKS module and you want to use the default node type for all of your development and staging environments but you want to override that for production. If you're using Terraform workspaces and you're calling a module where you want to override the module default in some workspaces but you want to keep the module default for all other workspaces, the way you'd expect to do that would be to pass in a null for the parameter when you want to use the module default. Since Another example would be calling a module with |
Given the complexity of changing how |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Terraform Version
Terraform Configuration Files
main.tf
:mod1/main.tf
:Debug Output
https://pastebin.com/raw/NuxLJi2U
Expected Behavior
As per docs:
Actual Behavior
An error message is shown saying that the said argument must not be null:
Steps to Reproduce
terraform init
terraform apply
References
The text was updated successfully, but these errors were encountered: