-
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
Provide a count-like mechanism for variable data that only checks against null. #30816
Comments
Hi @theherk, Thanks for the enhancement idea. The techniques for dealing with unknown values vary by circumstances, but the more flexible expansion method of Taking your example though, if you are certain in the configuration that If the requirement is to ensure that a value might be unknown, but guaranteed to be non-null, modules can use non-nullable inputs. Extending this idea to resource schemas is covered in #26755, along with similar advice. Fundamentally terraform must know which instances it's planning, which is why Since these ideas are already covered in-depth in other issues, I'm going to close this one out. If you have more questions, feel free to use the community forum where there are more people ready to help. Thanks! |
I don't think my contrived example clarified the issue. There are cases where a policy document is a variable, that should be nullable. If it is not passed in the policy resource should not be created. If it is passed in the resource should be created with zero concern for the values contained therein. As in, the number is known to be exactly one in that case, but that fails with an error claiming the count cannot be known. The requirement is simply that count should be able to compare null vs not null regardless of the computability of the values within the non-null value. I'm trying to wrap my head around your |
The question has been asked as recommended in the community forum, here. I do hope somebody will take the time to clarify this, as it really doesn't make sense, and I've seen people struggle with it for years, precisely because it makes so little sense. |
If a nullable value is passed in in some way, then that is what should be compared for the To help clarify, |
Okay, excellent. I didn't find #26755 earlier. I have added it to the references. Thank you for taking the time. |
For posterity, I think I have found a workaround; kludge but seems to work. This moves the "unknown-but-not-null" value to the value of the My workaround was a lie, so I'm editing with the working solution given by @apparentlymart. Take the variable wrapped in an object: variable "policy_obj" {
type = object({
json = string
})
default = null
}
resource "some_resource" "x" {
count = var.policy != null ? 1 : 0
policy = var.policy.json
} |
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. |
Current Terraform Version
Use-cases
There are many situations where variable data contains values that can't be known until apply-time. Reasonably in these cases, one can't rely on this data. For example, often we pass a policy document as a variable whose default is
null
. If the policy is not null, we need to create the policy resource. That can be done withcount = var.policy != null ? 1 : 0
. However, if that contains data that cannot be determined until apply time, you get the error with which many of us have wrestled:The "count" value depends on resource attributes that cannot be determined until apply.
There are cases where doesn't make sense though. Even if a value in that policy is unknown before applying, it is still crystal clear that the variable is not
null
.What we need is a simple mechanism like:
but that doesn't choke if there are undetermined values in the variable. There is no question here if this value is null of not, so there should be no issue determining that the count should be precisely one.
Attempted Solutions
There is a workaround in a small few cases, where you can create an "empty" resource (empty policy for example) that can be used where you don't use count, always create the resource, but select which policy (var.policy or empty policy) is actually used for the resource. That is not sufficient though.
Proposal
I'd suggest either improving the
count
feature to succeed in minimal null checks or add an additional feature which can simply detect the presence of a value without considering its contents.References
These are a few examples, but you find many on stackoverflow.com and scattered around. Again, there are many cases where this behavior is perfectly sensible, but in some cases like checking against null, the contents really do not matter.
Minimal Example
Consider the following:
In this scenario, the fact that the policy contains a value (arn) that can't be determined until apply is irrelevant in determining if the policy is null or not. Are there other ways to deal with this that I am overlooking? If not, I hope this can be improved somehow.
The text was updated successfully, but these errors were encountered: