如果我动态创建的列表不为空,则需要引发异常,之后是此https://github.com/hashicorp/terraform/issues/15469,我在下面创建了代码,如果列表不为空,它应该失败并显示错误,如您所见,列表是根据null_resource构建列表,它总是引发断言:
resource "null_resource" "empty_string" {
count = "10"
triggers = {
value = ""
}
}
locals {
compacted_list_length = "${length(compact(null_resource.empty_string.*.triggers.value))}"
}
resource "null_resource" "is_array_empty" {
count = "${local.compacted_list_length}"
"Lsit is not empty" = true
}
output "compacted_list_length" {
value = "${local.compacted_list_length}"
}
如果您要评论以下内容,输出将显示0
resource "null_resource" "is_array_empty" {
count = "${local.compacted_list_length}"
"Lsit is not empty" = true
}
如果将本地人设置为:
locals {
compacted_list_length = 0
}
即使此代码也可以正常工作:
locals {
empty_list = ["", "", ""]
compacted_list_length = "${length(compact(local.empty_list))}"
}
resource "null_resource" "is_array_empty" {
count = "${local.compacted_list_length}"
"Lsit is not empty" = true
}
output "compacted_list_length" {
value = "${local.compacted_list_length}"
}
我的错误在哪里?
答案 0 :(得分:0)
此断言解决方法不适用于动态值,因为它在验证阶段起作用。在此阶段,Terraform不会计算动态值。
此声明变通办法很好地验证了变量值,如documentation中两个示例中所建议的。请注意,在两种情况下,计数值仅基于变量值。这些值在验证阶段可用,而不是动态值。
resource "null_resource" "is_environment_name_valid" {
count = "${contains(var.environment_list, var.env) == true ? 0 : 1}"
"ERROR: The env value can only be: dev, qa or prod" = true
}
resource "null_resource" "is_array_length_correct" {
count = "${length(var.array1) == length(var.array2) ? 0 : 1}"
"array1 and array2 must be the same length" = true
}