【问题标题】:Terraform local variable not working with my "or" operatorTerraform 局部变量不适用于我的“或”运算符
【发布时间】:2021-11-09 14:01:52
【问题描述】:

我有带有文件的 terraform 模块: 主文件 变量.tf

在我定义的变量中:

variable "environment" {
  type        = string
  description = "The environment the module is being deployed to, e.g: Test, Integrate or Production"
}
locals {
  scale_in_protection = var.environment == "production" || "integrate" ? "true" : "false"
}

我收到此错误:

Releasing state lock. This may take a few moments...
╷
│ Error: Invalid operand
│
│   on ..\..\modules\jitsi\variables.tf line 99, in locals:
│   99:   scale_in_protection = var.environment == "production" || "integrate" ? "true" : "false"
│
│ Unsuitable value for right operand: a bool is required.

【问题讨论】:

    标签: amazon-web-services terraform


    【解决方案1】:

    您的conditional 无效。它缺少var.environment"integrate" 的比较。这是固定版本:

    variable "environment" {
      type        = string
      description = "The environment the module is being deployed to, e.g: Test, Integrate or Production"
    }
    
    locals {
      scale_in_protection = var.environment == "production" || var.environment == "integrate" ? "true" : "false"
    }
    

    【讨论】:

      【解决方案2】:

      我找到了解决我自己问题的方法:

      scale_in_protection = contains(["production", "integrate"], var.environment)  ? "true" : "false"
      

      结果是||仅适用于变量而不是字符串,因此您需要使用 contains。

      https://www.terraform.io/docs/language/functions/contains.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 2020-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-25
        相关资源
        最近更新 更多