【发布时间】:2022-01-26 15:22:27
【问题描述】:
我在本地定义了一个变量,称为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)一次性设置此标志。
【问题讨论】:
-
你不能。 github.com/hashicorp/terraform/issues/22544 此处的最后一条评论包含一个解决方法,但不是一个很好的解决方法。
-
@jordanm 嘘。我应该删除这个还是你想让它成为答案?
标签: amazon-web-services terraform aws-cli terraform-provider-aws