我想为prevent_destroy元参数使用内插值
当我编码时
lifecycle {
# never destroy the IP address of the production stage,
prevent_destroy = "${var.stage_name == "global" ? true : false }"
}
Run Code Online (Sandbox Code Playgroud)
我得到
* cannot parse 'prevent_destroy' as bool: strconv.ParseBool: parsing "${var.stage_name == \"global\" ? true : false }": invalid syntax
Run Code Online (Sandbox Code Playgroud)
等效误差为
lifecycle {
prevent_destroy = "${var.stage_name == "global" ? 1 : 0 }"
}
Run Code Online (Sandbox Code Playgroud)
当我定义一个本地定义
locals {
booltest = "${var.stage_name == "global" ? true : false }"
}
Run Code Online (Sandbox Code Playgroud)
好像经过,但指的是当地
lifecycle {
prevent_destroy = "${var.booltest}"
}
Run Code Online (Sandbox Code Playgroud)
给我一个
* cannot parse 'prevent_destroy' as bool: strconv.ParseBool: parsing "${var.booltest}": invalid syntax
Run Code Online (Sandbox Code Playgroud)
(也试过 0 和 1)
这怎么编码?我的版本是Terraform v0.11.10
不幸的是,生命周期属性不支持插值:
https://github.com/hashicorp/terraform/issues/3116 https://github.com/hashicorp/terraform/issues/17294
但是,使用count您可以解决此问题。大致:
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"
}
...
}
Run Code Online (Sandbox Code Playgroud)
我个人会prevent_destroy在环境之间使用相同的设置,并在需要时明确销毁它们。
| 归档时间: |
|
| 查看次数: |
1895 次 |
| 最近记录: |