Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way AND/OR conditional operator in terraform?

Tags:

Is there a way to use something like this in Terraform?

count = "${var.I_am_true}"&&"${var.I_am_false}"

like image 407
user2062360 Avatar asked Sep 13 '16 22:09

user2062360


People also ask

How do you do if condition in Terraform?

Terraform doesn't support if-statements, so this code won't work. However, you can accomplish the same thing by using the count parameter and taking advantage of two properties: If you set count to 1 on a resource, you get one copy of that resource; if you set count to 0, that resource is not created at all.

What are the two types of conditional operators?

There are three conditional operators: && the logical AND operator. || the logical OR operator. ?: the ternary operator.

What is difference between if-else and conditional operator?

A conditional operator is a single programming statement, while the 'if-else' statement is a programming block in which statements come under the parenthesis. A conditional operator can also be used for assigning a value to the variable, whereas the 'if-else' statement cannot be used for the assignment purpose.


1 Answers

This is more appropriate in the actual version (0.12.X)

The supported operators are:

Equality: == and !=
Numerical comparison: >, <, >=, <=
Boolean logic: &&, ||, unary !

https://www.terraform.io/docs/configuration/interpolation.html#conditionals

condition_one and condition two:

count = var.condition_one && var.condition_two ? 1 : 0 

condition_one and NOT condition_two:

count = var.condition_one && !var.condition_two ? 1 : 0 

condition_one OR condition_two:

count = var.condition_one || var.condition_two ? 1 : 0 
like image 117
NicoKowe Avatar answered Sep 28 '22 19:09

NicoKowe