WEBVTT

00:00.280 --> 00:04.480
In this lecture, we will take a look
at some of the common operators

00:04.480 --> 00:07.520
and then conditional expressions
in Terraform.

00:08.640 --> 00:11.400
In the previous lecture,
we saw how to test functions

00:11.400 --> 00:13.620
using the terraform console command.

00:14.960 --> 00:16.760
That's not the only use of it.

00:17.060 --> 00:21.040
We can also make use of arithmetic
and logical operations with Terraform

00:21.040 --> 00:24.260
and test the same using
the terraform console command.

00:25.140 --> 00:29.180
For example, we can make use of
arithmetic operations like these

00:29.180 --> 00:32.020
by making use of arithmetic operators.

00:34.540 --> 00:38.080
Besides arithmetic, Terraform
also supports equality,

00:38.080 --> 00:40.420
logical, and comparison operators.

00:42.780 --> 00:46.260
Equality operators take two values
of any data type

00:46.260 --> 00:49.080
and produce a Boolean result of true or false.

00:50.020 --> 00:52.620
To check if two values are exactly equal,

00:52.620 --> 00:55.880
we can make use of double equals operator
like this.

00:58.020 --> 01:01.440
The != operator does the inverse

01:01.440 --> 01:05.180
and it prints a value of true
if the two values are not equal.

01:06.080 --> 01:09.540
In this example, the comparison result
produces false

01:09.540 --> 01:12.600
as the value on the left
of the operator is a number,

01:12.600 --> 01:16.840
and the one on the right is a string
as it is enclosed within double quotes.

01:19.120 --> 01:24.160
The comparison operators of greater than,
lesser than, greater than or equal to,

01:24.160 --> 01:28.440
and lesser than or equal to take two values
both of type number,

01:28.640 --> 01:31.120
and then it produces a Boolean result.

01:33.780 --> 01:36.220
Let's now look at logical operators.

01:37.880 --> 01:43.660
The first operator is the AND operator,
which makes use of the double ampersand symbol.

01:45.220 --> 01:48.840
A logical AND operation
accepts two Boolean values

01:48.840 --> 01:52.100
and returns a true
only if both the values are true.

01:53.040 --> 01:59.460
For example, here 8 being greater than 7
AND 8 being less than 10 are both true.

01:59.680 --> 02:01.720
Hence, the result is true as well.

02:02.340 --> 02:06.320
If either of the values is false,
the result would be false as well.

02:07.560 --> 02:10.640
The logical OR operation
returns a value of true

02:10.640 --> 02:13.440
if either value provided to it is true.

02:14.020 --> 02:20.020
For example, 8 being greater than 9
OR less than 10 produces a result of true,

02:20.020 --> 02:22.020
since the second condition is true.

02:23.180 --> 02:27.840
Finally, the NOT operator returns a false
if the condition is true

02:27.840 --> 02:30.260
and true if the condition is false.

02:31.180 --> 02:36.640
For example, we have a Boolean variable here
called special with the default value of true.

02:37.600 --> 02:40.480
The value of var.special returns a true

02:42.200 --> 02:47.080
but when we use the NOT operator in front
of this variable, it results in a false.

02:49.720 --> 02:52.820
Another variable b has a value of 25.

02:53.780 --> 02:58.060
We can add a NOT operator
before the comparison b greater than 30.

02:58.460 --> 03:02.600
Although comparison is false,
and 25 is not greater than 30,

03:02.600 --> 03:04.340
we get the result of true

03:04.340 --> 03:08.560
as we are making use of the NOT operator
on the result of this comparison.

03:11.340 --> 03:14.880
The same operations can be applied
to variables in Terraform.

03:15.560 --> 03:21.020
If we consider a .tf file with two variables
of type number called a and b,

03:21.020 --> 03:24.080
we can make use of operators like this.

03:25.720 --> 03:29.820
That's great, but how do we make use of this
within Terraform?

03:30.340 --> 03:34.800
Say, we want to assign a variable to an argument
based on a condition.

03:35.120 --> 03:38.960
Say, if it's greater than a specific value
or another variable.

03:41.580 --> 03:44.740
To understand this,
let's make use of a simple example.

03:45.200 --> 03:47.920
Let's say we want to generate a new password.

03:49.060 --> 03:52.700
This can be done by making use of
the random_puzzle resource type

03:52.700 --> 03:54.300
from the random provider.

03:55.620 --> 04:00.760
This resource will generate a random password
with a user-specified length argument.

04:01.920 --> 04:05.060
To print the password on the screen
after a terraform apply,

04:05.060 --> 04:08.420
let's also make use of an output variable
called password.

04:10.660 --> 04:14.300
We have also defined a variable
called length of type number

04:14.560 --> 04:17.220
and used this within the main.tf file.

04:18.060 --> 04:23.180
Note that there is no default value assigned
to this variable in the variables.tf file.

04:23.400 --> 04:27.820
We will pass in a value for this manually
when we run the terraform apply command.

04:28.980 --> 04:34.420
If we run terraform apply now, a password with
the specified requirements will be printed.

04:35.400 --> 04:41.740
Wait, what if we want Terraform to accept a value
of length that is at least 8 characters long

04:41.740 --> 04:44.920
failing which use the length of 8 as default?

04:46.200 --> 04:49.120
With common scripting methods
such as a bash script,

04:49.120 --> 04:53.540
this can be done by making use of
an IF or ELSE statement like this.

04:54.640 --> 04:58.020
The example here has a condition,
and two statements.

04:58.900 --> 05:03.540
One for if the condition equals true
and the other if it is false.

05:04.500 --> 05:09.580
If the length variable has a value lesser than 8,
then we assign it a value of 8.

05:09.580 --> 05:12.080
If not, we use the length as it is.

05:13.060 --> 05:16.700
Once done, we can use this variable
to generate the required password.

05:19.620 --> 05:24.040
The same can be done in Terraform
by making use of a conditional expression.

05:27.420 --> 05:30.120
The syntax of using this is as follows.

05:30.800 --> 05:33.180
First, we have to specify the condition.

05:33.420 --> 05:36.240
This is followed by a question mark and a value.

05:36.760 --> 05:40.080
Following this, we have a colon
and another value.

05:41.340 --> 05:45.760
If the condition is true, the value to the right
of the question mark is considered.

05:46.200 --> 05:49.960
If it is false, the value to the right
of the colon is considered.

05:52.500 --> 05:55.780
Let's put this to use and update
our terraform configuration file

05:55.780 --> 05:59.480
for generating a random password
of at least 8 characters.

06:00.740 --> 06:04.400
The value of the length argument
can now be updated like this.

06:06.040 --> 06:10.160
The condition used here is to check
if the variable is lesser than 8.

06:12.320 --> 06:15.800
If true, use 8 as the value
of the length argument.

06:15.800 --> 06:19.500
Else make use of the length variable
as the value of length.

06:20.640 --> 06:26.060
If we run terraform init plan and apply now,
with the value of length as equal to 5,

06:26.060 --> 06:29.520
you can see that Terraform
still makes use of the length as 8

06:29.520 --> 06:31.240
and then generates the password.

06:31.240 --> 06:35.000
For any value greater than 8; 12, for example,

06:35.000 --> 06:39.400
Terraform accepts that as the value
and generates the password as expected.

06:42.300 --> 06:43.760
That's it for this lecture.

06:43.760 --> 06:45.520
Head over to the practice tests

06:45.520 --> 06:49.380
and practice working with functions
and conditional statements in Terraform.

