如何在 Terraform 中参数化 prevent_destroy 生命周期配置?
How to parameterize prevent_destroy lifecycle configuration in Terraform?
我在本地定义了一个变量,称为 local.protect
,并在 variables.tf
中用 default = true
和 type = bool
定义。如何绕过对 prevent_destroy
参数使用变量约束?我以为我可以 local
.ize 它(例如,locals {protect = var.protect}
),但这也不起作用。
│ Error: Variables not allowed
│
│ on main.tf line 105, in resource "aws_eip" "backend_eip":
│ 105: prevent_destroy = local.protect
│
│ Variables may not be used here.
╵
╷
│ Error: Unsuitable value type
│
│ on main.tf line 105, in resource "aws_eip" "backend_eip":
│ 105: prevent_destroy = local.protect
│
│ Unsuitable value: value must be known
在main.tf
中:
resource "aws_eip" "backend_eip" {
vpc = true
depends_on = [module.vpc.igw_id]
lifecycle {
prevent_destroy = local.protect # line 105
}
}
在variables.tf
中:
variable "protect" {
type = bool
description = "Whether (true) or not (false) to protect EIP from deletion via `terraform destroy`."
default = true
}
这里的用例是能够在运行时为一组资源(如五个 EIP)同时设置此标志。
正如@jordanm所说,
You can't. github.com/hashicorp/terraform/issues/22544 the last comment here contains a workaround, but not a great one.
我在本地定义了一个变量,称为 local.protect
,并在 variables.tf
中用 default = true
和 type = bool
定义。如何绕过对 prevent_destroy
参数使用变量约束?我以为我可以 local
.ize 它(例如,locals {protect = var.protect}
),但这也不起作用。
│ Error: Variables not allowed
│
│ on main.tf line 105, in resource "aws_eip" "backend_eip":
│ 105: prevent_destroy = local.protect
│
│ Variables may not be used here.
╵
╷
│ Error: Unsuitable value type
│
│ on main.tf line 105, in resource "aws_eip" "backend_eip":
│ 105: prevent_destroy = local.protect
│
│ Unsuitable value: value must be known
在main.tf
中:
resource "aws_eip" "backend_eip" {
vpc = true
depends_on = [module.vpc.igw_id]
lifecycle {
prevent_destroy = local.protect # line 105
}
}
在variables.tf
中:
variable "protect" {
type = bool
description = "Whether (true) or not (false) to protect EIP from deletion via `terraform destroy`."
default = true
}
这里的用例是能够在运行时为一组资源(如五个 EIP)同时设置此标志。
正如@jordanm所说,
You can't. github.com/hashicorp/terraform/issues/22544 the last comment here contains a workaround, but not a great one.