This guide explores operators and conditional expressions in Terraform, covering arithmetic, equality, comparison, and logical operators for configuration files and the console.
Use this file to discover all available pages before exploring further.
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.
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:
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:
variable "special" { type = bool default = true description = "Set to true to use special characters"}
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:
$ terraform console> ! (var.b > 30)true
Here is the configuration for variable b:
variable "b" { type = number default = 25}
You can also compare variables. If you define two number variables a and b, you can compare them like this:
$ terraform console> var.a > var.btrue
Configuration for variables a and b:
variable "a" { type = number default = 50}variable "b" { type = number default = 25}
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:
variable "length" { type = number description = "The length of the password"}
Now, when you apply Terraform commands, the configuration enforces a minimum length. For example:
When applying with a length of 5:
$ terraform apply -var=length=5Terraform will perform the following actions: # random_password.password-generator will be created + resource "random_password" "password-generator" { + id = (known after apply) + length = 8 }Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Outputs:password = &(1Beiaq)
When applying with a length of 12:
$ terraform apply -var=length=12Terraform will perform the following actions: # random_password.password-generator must be replaced-/+ resource "random_password" "password-generator" { ~ id = "none" -> (known after apply) ~ length = 8 -> 12 # forces replacement. }Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Outputs:password = 8B@}{cUzrZ7
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: