Terraform Logical AND Operator, Logical OR Operator
Logical AND Operator
Sometimes you might need a logical ‘AND’ operator in Terraform. I recently had
a module where I had a variable for create_dns
, which was either true or false,
and I had another variable for create_tableau
, which was also either true or
false. I only want to create some resources if BOTH of those variables are true,
which is a logical ‘AND’ operator.
count = "${max(var.create_dns + var.create_tableau - 1, 0)}"
This will evaluate to 1 if both create_dns
AND create_tableau
are true,
otherwise no resource will be created.
Logical OR Operator
Or maybe you need a logical ‘OR’ operator, and you want to create one resource if EITHER of the above variables are true.
count = "${max(var.create_dns, var.create_tableau)}"
This will evaluate to 1 if either of those variables are true, and 0 if neither of them are true. This same idea could work for any number of variables to consider.