How can we test the condition for two different variables in terraform?

Varsha Chandurkar (Trianz) 21 Reputation points
2021-03-05T18:57:03.787+00:00

I have to add the condition like for the given subscription, the related environment is stg or prod only. I should fail if for the given subscription is deploying into dev.
I have tried to add the validate block in var.tf, but the scope of particular variable is to the block itself.

validation {
condition = (var.subscription_id = "XXX" ? contains(["stg", "prd"], var.env) : contains(["dev", "stg", "prd"], var.env))
error_message = "The environment must be stg or prd."
}

This code throwing error as follows:

Error: Invalid variable validation condition

on vars.tf line 15, in variable "env":
15: condition = (var.subscription_id = "XXX" ? contains(["stg", "prd"], var.env) : contains(["dev", "stg", "prd"], var.env))

The condition for variable "env" must refer to var.env in order to test
incoming values.

Error: Invalid reference in variable validation

on vars.tf line 15, in variable "env":
15: condition = (var.subscription_id = "XXX" ? contains(["stg", "prd"], var.env) : contains(["dev", "stg", "prd"], var.env))

The condition for variable "env" can only refer to the variable itself, using
var.env.

Azure Cloud Services
Azure Cloud Services
An Azure platform as a service offer that is used to deploy web and cloud applications.
669 questions
{count} votes