Skip to main content
In this guide, we will explore common operators and conditional expressions in Terraform. You will learn how to use arithmetic, equality, comparison, and logical operators interactively in the Terraform console and within configuration files.

Arithmetic Operators

Terraform supports basic arithmetic operations. You can use the Terraform console to evaluate expressions as shown below:

Equality and Comparison Operators

Terraform provides equality operators that compare values from any data type and return a boolean result. Use the double equals operator (==) to test for equality:
In the examples above, although the numeric value 8 equals 8, comparing the number 8 with the string “8” results in a false for equality (==) and true for inequality (!=). Comparison operators such as greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=) work with numbers only. For example:

Logical Operators

Terraform also supports logical operators to combine boolean expressions. Use the logical AND operator (&&) when you need both conditions to be true:
Similarly, the logical OR operator (||) returns true if at least one condition is true, and the NOT operator (!) inverts a boolean value. Consider the following examples using a boolean variable:
Below is the Terraform configuration for the variable used in the example:
Another example involves a numeric variable. Suppose variable b has a default value of 25. Using the NOT operator to check if 25 is not greater than 30:
Here is the configuration for variable b:
You can also compare variables. If you define two number variables a and b, you can compare them like this:
Configuration for variables a and b:

Using Conditional Expressions for Dynamic Configuration

Conditional expressions in Terraform allow you to assign values based on a condition in a concise manner. The syntax follows:

Example: Enforcing a Minimum Password Length

Imagine you need to generate a new password with a minimum length requirement. The following example shows how to use Terraform’s random password resource from the random provider. Initially, define the resource without any condition:
And define the variable in a separate file:
When running the Terraform apply command, you would supply the length manually:
However, to enforce a minimum length of 8 characters, update the resource using a conditional expression:
The variable definition remains the same:
Now, when you apply Terraform commands, the configuration enforces a minimum length. For example:
When applying with a length of 5:
When applying with a length of 12:
This demonstrates how conditional expressions can enforce configuration constraints and dynamically adjust resource properties in Terraform. That concludes this article on conditional expressions. Proceed to the practice tests and explore further topics such as functions and advanced conditional statements in Terraform. For more detailed guides and best practices, check out the following links:

Watch Video

Practice Lab