小编Em *_* Ma的帖子

在 Terraform 中使用条件计数语句有更好的替代方法吗?

我很想知道是否有人知道在 Terraform 中使用条件计数语句的更好替代方法。我所说的“条件计数语句”是指根据变量输入等条件,计数将计算以创建 0 或 1 个资源的语句。

一个简单的例子:

resource "xxxxxxxx" "example" {
count = var.example != null ? 1 : 0
}
Run Code Online (Sandbox Code Playgroud)

在这里,只有当 var.example 有值(不为空)时才会创建资源,否则不会创建。

条件计数在实践中通常工作正常,但有时在比上述更复杂的用途中,它会带来在 Terraform Plan 期间出现错误的风险,因为它无法评估预应用计数的结果。

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法可以达到在 Terraform 中按条件创建资源的相同效果?

resources count conditional-statements terraform

2
推荐指数
1
解决办法
2446
查看次数