In this lecture, we will take a look at some of the common operators
and then conditional expressions in Terraform.
In the previous lecture, we saw how to test functions
using the terraform console command.
That's not the only use of it.
We can also make use of arithmetic and logical operations with Terraform
and test the same using the terraform console command.
For example, we can make use of arithmetic operations like these
by making use of arithmetic operators.
Besides arithmetic, Terraform also supports equality,
logical, and comparison operators.
Equality operators take two values of any data type
and produce a Boolean result of true or false.
To check if two values are exactly equal,
we can make use of double equals operator like this.
The != operator does the inverse
and it prints a value of true if the two values are not equal.
In this example, the comparison result produces false
as the value on the left of the operator is a number,
and the one on the right is a string as it is enclosed within double quotes.
The comparison operators of greater than, lesser than, greater than or equal to,
and lesser than or equal to take two values both of type number,
and then it produces a Boolean result.
Let's now look at logical operators.
The first operator is the AND operator, which makes use of the double ampersand symbol.
A logical AND operation accepts two Boolean values
and returns a true only if both the values are true.
For example, here 8 being greater than 7 AND 8 being less than 10 are both true.
Hence, the result is true as well.
If either of the values is false, the result would be false as well.
The logical OR operation returns a value of true
if either value provided to it is true.
For example, 8 being greater than 9 OR less than 10 produces a result of true,
since the second condition is true.
Finally, the NOT operator returns a false if the condition is true
and true if the condition is false.
For example, we have a Boolean variable here called special with the default value of true.
The value of var.special returns a true
but when we use the NOT operator in front of this variable, it results in a false.
Another variable b has a value of 25.
We can add a NOT operator before the comparison b greater than 30.
Although comparison is false, and 25 is not greater than 30,
we get the result of true
as we are making use of the NOT operator on the result of this comparison.
The same operations can be applied to variables in Terraform.
If we consider a .tf file with two variables of type number called a and b,
we can make use of operators like this.
That's great, but how do we make use of this within Terraform?
Say, we want to assign a variable to an argument based on a condition.
Say, if it's greater than a specific value or another variable.
To understand this, let's make use of a simple example.
Let's say we want to generate a new password.
This can be done by making use of the random_puzzle resource type
from the random provider.
This resource will generate a random password with a user-specified length argument.
To print the password on the screen after a terraform apply,
let's also make use of an output variable called password.
We have also defined a variable called length of type number
and used this within the main.tf file.
Note that there is no default value assigned to this variable in the variables.tf file.
We will pass in a value for this manually when we run the terraform apply command.
If we run terraform apply now, a password with the specified requirements will be printed.
Wait, what if we want Terraform to accept a value of length that is at least 8 characters long
failing which use the length of 8 as default?
With common scripting methods such as a bash script,
this can be done by making use of an IF or ELSE statement like this.
The example here has a condition, and two statements.
One for if the condition equals true and the other if it is false.
If the length variable has a value lesser than 8, then we assign it a value of 8.
If not, we use the length as it is.
Once done, we can use this variable to generate the required password.
The same can be done in Terraform by making use of a conditional expression.
The syntax of using this is as follows.
First, we have to specify the condition.
This is followed by a question mark and a value.
Following this, we have a colon and another value.
If the condition is true, the value to the right of the question mark is considered.
If it is false, the value to the right of the colon is considered.
Let's put this to use and update our terraform configuration file
for generating a random password of at least 8 characters.
The value of the length argument can now be updated like this.
The condition used here is to check if the variable is lesser than 8.
If true, use 8 as the value of the length argument.
Else make use of the length variable as the value of length.
If we run terraform init plan and apply now, with the value of length as equal to 5,
you can see that Terraform still makes use of the length as 8
and then generates the password.
For any value greater than 8; 12, for example,
Terraform accepts that as the value and generates the password as expected.
That's it for this lecture.
Head over to the practice tests
and practice working with functions and conditional statements in Terraform.