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:
==) 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:
||) 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:
b has a default value of 25. Using the NOT operator to check if 25 is not greater than 30:
b:
a and b, you can compare them like this:
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:When applying with a length of 5:
When applying with a length of 12: