【问题标题】:Terraform Error: Incorrect condition type when running terraform planTerraform 错误:运行 terraform 计划时条件类型不正确
【发布时间】:2020-10-09 02:42:58
【问题描述】:

在代码 .tf 文件中:

resource "aws_vpc_peering_connection" "this_1" {
  count         = var.create_peering_1 ? 1 : 0
  peer_owner_id = var.peer_account_id_1
  peer_vpc_id   = var.vpc_peer_id_1
  vpc_id        = module.vpc.vpc_id
  auto_accept   = var.auto_accept_peering_1
}

variables.tf中的变量:

variable "create_peering_1" {
  description = "Create peering connection, 0 to not create"
  default     = 0
}

我得到的错误:

Error: Incorrect condition type

  on peering_1.tf line 6, in resource "aws_vpc_peering_connection" "this_1":
   6:   count         = var.create_peering_1 ? 1 : 0
    |----------------
    | var.create_peering_1 is 0

The condition expression must be of type bool.

我应该怎么做才能修复这个错误?

【问题讨论】:

  • 非布尔到布尔的隐式转换是旧版本 Terraform 中不受支持的半意外功能。您需要使用明确的布尔类型,例如 truefalse

标签: amazon-web-services boolean syntax-error terraform terraform-provider-aws


【解决方案1】:

0 用作false 会让人困惑

variable "create_peering_1" {
  description = "Create peering connection, false to not create"
  default     = false
  type        = bool
}

【讨论】:

    【解决方案2】:
    variable "create_peering_1" {
    type = bool
    default = true
    

    bool 必须为真或假。

    【讨论】:

    • 我尝试添加它,但它仍然给出错误:错误:variables.tf 第 109 行变量的默认值无效,在变量“create_peering_1”中:109:默认值 = 0 此默认值与变量的类型约束:bool 必需。 @giridhar.j
    • 啊,bool 必须为真或假。当为真时,count 将为 1,当为 false 时,上述代码的 count 将为 0。更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 2020-09-18
    • 2018-10-11
    • 2021-01-30
    • 2021-01-16
    • 1970-01-01
    • 2018-09-06
    • 2019-08-09
    相关资源
    最近更新 更多