我如何处理以下 variables
var 可以为 null 的情况
locals {
tf_variables = (var.variables == null) ? null : jsondecode(var.variables)["variables"]
}
resource "tfe_variable" "this" {
for_each = local.tf_variables
...
}
我在打
Error: Invalid for_each argument
on ..\..\main.tf line 63, in resource "tfe_variable" "this":
63: for_each = local.tf_variables
The given "for_each" argument value is unsuitable: the given "for_each"
argument value is null. A map, or set of strings is allowed.
最佳答案
您可以将 null
替换为空集、列表或映射。
将您的本地更改为此应该有效:
locals {
tf_variables = (var.variables == null) ? [] : jsondecode(var.variables)["variables"]
}
另外,您不需要在那里使用 jsondecode
,因为 var.variables
必须已经是序列化的 HCL 对象。
关于terraform - 使用 for_each 处理 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64826787/