When using expressions (ternaries) with variables inside of the sum function, it unexpectedly results in a validation error:
locals {
count = 1 + sum([
var.enable_a ? 1 : 0,
var.enable_b ? 1 : 0,
])
}
Results in this error:
Invalid value for "list" parameter: argument must be list, set, or tuple of number values.
This code however works (ternaries with no variables):
locals {
count = 1 + sum([
false ? 1 : 0,
true ? 1 : 0
])
}
Likewise, this (somewhat unrelated) workaround works fine:
locals {
count = 1 + (var.enable_a ? 1 : 0) + (var.enable_b ? 1 : 0)
}
Terraform v0.14.0
variable "enable_a" {
type = bool
default = false
}
variable "enable_b" {
type = bool
default = true
}
locals {
count = 1 + sum([
var.enable_a ? 1 : 0,
var.enable_b ? 1 : 0,
])
}
output "count" {
value = local.count
}
Terraform should output count=2.
A validation exception was thrown.
terraform plan the included file.Hi @lukiffer! Thanks for reporting this.
I think the problem here is that the sum function implementation isn't correctly handling the situation where one of the given elements (rather than the list as a whole) is unknown:
gocty.FromCtyValue will return an error if asked to decode an unknown number into a float64 value, because Go has no way to represent an unknown float. Instead, I think this ought to check first whether the number is known and, if not, return early with cty.UnknownVal(cty.Number) as the return value to indicate that the result isn't known yet either.
While looking again at this function I think it would also run into the same problem if given a number whose magnitude is too big to fit into a float64, because that too would fail decoding into a float64. Instead, it could perhaps compute the sum by using the cty.Value.Add method, in a similar way as for cty's Add function (which is, internally, the implementation of the Terraform language's + operator). I notice it's also doing a special check to handle a panic from trying to add together opposing infinities, which I suppose sum should also do for completeness. even though infinities don't show up often in the Terraform language.
Perhaps we could address these things all in a single PR, to reduce the amount of back-and-forth to review and merge them.
This bug was fixed in #27249, along with the other improvements Martin suggested above. The fix will be available as part of the upcoming 0.14.3 release. Thanks again for reporting it!
$ terraform apply -auto-approve
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
count = 1
$ terraform apply -auto-approve -var enable_a=true
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
count = 2
$ terraform apply -auto-approve -var enable_a=true -var enable_b=true
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
count = 3
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.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.