Terraform:如何从插值中获取布尔值? [英] Terraform: How to get a boolean from interpolation?
问题描述
我想为 prevent_destroy
元参数使用插值
I want to use an interpolated value for the prevent_destroy
meta-parameter
当我编码时
lifecycle {
# never destroy the IP address of the production stage,
prevent_destroy = "${var.stage_name == "global" ? true : false }"
}
我明白了
* cannot parse 'prevent_destroy' as bool: strconv.ParseBool: parsing "${var.stage_name == "global" ? true : false }": invalid syntax
等效错误
lifecycle {
prevent_destroy = "${var.stage_name == "global" ? 1 : 0 }"
}
当我定义一个本地定义时
When I define a local the definition
locals {
booltest = "${var.stage_name == "global" ? true : false }"
}
好像是经过,但指的是本地
seems to go through, but referring to the local
lifecycle {
prevent_destroy = "${var.booltest}"
}
给我一个
* cannot parse 'prevent_destroy' as bool: strconv.ParseBool: parsing "${var.booltest}": invalid syntax
(也尝试使用 0 和 1)
(also tried with 0 and 1)
如何编码?我的版本是 Terraform v0.11.10
How can this be coded? My version is Terraform v0.11.10
推荐答案
可惜生命周期属性不支持插值:
Unfortunately lifecycle attributes do not support interpolation:
https://github.com/hashicorp/terraform/issues/3116https://github.com/hashicorp/terraform/issues/17294
但是,使用 count
您可以解决这个问题.大致:
However, using count
you may work around this. Roughly:
resource "aws_instance" "indestructible" {
count = "${var.prevent_destroy ? "1" : "0"}"
lifecycle {
prevent_destroy = "true"
}
...
}
resource "aws_instance" "destructible" {
count = "${var.prevent_destroy ? "0" : "1"}"
lifecycle {
prevent_destroy = "false"
}
...
}
我个人会在环境之间使用相同的 prevent_destroy
设置,并在需要时明确销毁它们.
Personally I would use the same prevent_destroy
setting between environments and destroy them explicitly when needed.
这篇关于Terraform:如何从插值中获取布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!