Home > Back-end >  Terraform local variable not working with my "or" operator
Terraform local variable not working with my "or" operator

Time:11-10

I have terraform module with files: main.tf variables.tf

Within the variables I have defined:

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"
}

I get this error:

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.

CodePudding user response:

Your conditional is not valid. It's missing the comparison of var.environment and "integrate". Here's the fixed version:

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"
}

CodePudding user response:

I have found a solution to my own problem:

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

Turns out the || is only for variables not strings thus you need to use contains.

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

  • Related