I think there might be an issue with the way Terraform handles list elements when in conditional statement compared to unconditional logic.
Actual problem has been encountered with Azure network security rule definitions but simplified code reproduces the problem.
Consider 2 local variables:
a = {
val = 1, arr = null
}
b = {
val = null, arr = [1]
}
if these 2 values are placed in an array unconditionally
it works fine but if same array is built conditionally
c = local.v ? [local.a, local.b] : []
it crashes Terraform 0.14 and in later versions reports error
│ │ local.a is object with 2 attributes
│ │ local.b is object with 2 attributes
To reproduce the issue attempt to execute 'terraform plan' on following:
locals {
v = true
a = {
val = 1, arr = null
}
b = {
val = null, arr = [1]
}
c = concat(
[local.a,local.b],
local.v ? [local.a,local.b]: []
)
}
When v = false plan succeeds, when v = true plan fails
Possible solution to change definition of a to
a = {
val = 1, arr = []
}
would pass Terraform check but will fail on apply as Azure network security rule only supports one of the 2 values to be provided, for example source_port_range and source_port_ranges (https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_rule#source_port_range)
I think there might be an issue with the way Terraform handles list elements when in conditional statement compared to unconditional logic.
Actual problem has been encountered with Azure network security rule definitions but simplified code reproduces the problem.
Consider 2 local variables:
if these 2 values are placed in an array unconditionally
it works fine but if same array is built conditionally
it crashes Terraform 0.14 and in later versions reports error
│ │ local.a is object with 2 attributes
│ │ local.b is object with 2 attributes
To reproduce the issue attempt to execute 'terraform plan' on following:
When v = false plan succeeds, when v = true plan fails
Possible solution to change definition of a to
would pass Terraform check but will fail on apply as Azure network security rule only supports one of the 2 values to be provided, for example source_port_range and source_port_ranges (https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_rule#source_port_range)