how to check a variable string with two values in terraform.
I want to check a string in a variable with two string values something like the below code.
virtual_network_type = var.vnet_type == "External" || "Internal" ? var.vnet_type : null
But terraform throws below error message
Error: Invalid operand
│
│ on ../../modules/test/test-instance.tf line 51, in resource "azurerm_api_management" "apim_demo":
│ 51: virtual_network_type = var.vnet_type == "External" || "Internal" ? var.vnet_type : null
│
│ Unsuitable value for right operand: a bool is required.
╵
Can we do this in terraform?
A construct like var.vnet_type == "External" || "Internal"
is something that few languages will accept, really.
var.vnet_type == "External" || var.vnet_type == "Internal"
On the other hand, is something that will give you a better result.
Another solution for those kind of use cases, if you don't want to repeat the variable name is to construct a list of the acceptable values and evaluate if the variable is contained in the list.
Something like:
contains(["External", "Internal"], var.vnet_type)
So,
virtual_network_type = var.vnet_type == "External" || var.vnet_type == "Internal" ? var.vnet_type : null
virtual_network_type = contains(["External", "Internal"], var.vnet_type) ? var.vnet_type : null