WEBVTT

00:00.380 --> 00:03.440
-In this lecture,
we will take a look at some more functions

00:03.500 --> 00:05.160
that are available in Terraform.

00:07.660 --> 00:10.500
We have used a few Terraform functions
throughout this course,

00:11.020 --> 00:14.280
most notably,
the file function to read data from a file.

00:15.220 --> 00:20.220
We have also used the length to determine
the number of elements in a given list or a map.

00:22.020 --> 00:27.120
We also had a look at the toset function,
which is used to convert a list to a set.

00:29.500 --> 00:31.420
Although we have used functions before,

00:31.480 --> 00:34.380
we have done so by using it
in the configuration files

00:34.440 --> 00:37.160
without seeing
what the functions do specifically

00:37.480 --> 00:40.860
and how it transforms
and combines values provided to it.

00:41.920 --> 00:44.860
Fortunately, Terraform provides
an interactive console

00:44.940 --> 00:48.320
that can be used for testing functions
as well as interpolations.

00:50.040 --> 00:54.560
To get into this interactive console,
we use the command terraform console.

00:56.200 --> 00:58.800
The interactive console
loads the state associated

00:58.860 --> 01:01.040
with the configuration directory by default

01:01.280 --> 01:04.580
allowing us to load any values
that is currently stored in it.

01:05.060 --> 01:08.560
It also loads variables that are stored
within configuration files.

01:10.420 --> 01:14.080
This allows us to experiment
with the functions and interpolations

01:14.200 --> 01:17.540
that can later be made use of
within the configuration files.

01:18.620 --> 01:22.460
For example, we can test the file function
in the console like this

01:22.920 --> 01:25.780
and it should return the contents
of the main.tf file.

01:27.520 --> 01:30.420
Using the length functions
shows that the number of elements

01:30.500 --> 01:32.960
in the list variable called region is 3.

01:33.620 --> 01:38.140
Finally, we can see that the toset function
converts the same variable

01:38.240 --> 01:41.420
from a list to a set
and removes the duplicates.

01:42.640 --> 01:45.300
Remember that a set
cannot have duplicate values

01:45.460 --> 01:47.080
so during this transformation,

01:47.180 --> 01:51.020
the toset function
removes the duplicate value for us-east-1

01:51.180 --> 01:53.500
which is present in the list variable twice.

01:55.080 --> 01:56.660
Besides the three functions,

01:56.740 --> 02:00.360
there are dozens of other useful
built in functions available in Terraform.

02:01.280 --> 02:04.400
We will take a look
at some of the commonly used ones next.

02:08.360 --> 02:11.640
There are several categories of functions
available in Terraform.

02:12.080 --> 02:16.580
However, in this course, we will only take a look
at some of the commonly used ones.

02:18.340 --> 02:21.120
Specifically, ones belonging
to Numeric Functions,

02:21.200 --> 02:25.100
String Functions, Collection Functions,
and Type Conversion functions.

02:29.920 --> 02:34.760
Numeric Functions, as the name suggests,
is used to transform and manipulate numbers.

02:35.620 --> 02:38.280
For example, we can use the max function

02:38.340 --> 02:41.200
to return the greatest number
from a set of numbers.

02:41.880 --> 02:45.460
The min function does the reverse
and returns the smallest number.

02:46.860 --> 02:51.840
To use variables as arguments for a function,
we have to use a slightly different syntax.

02:52.860 --> 02:57.200
For example, consider a block
for a variable called "num" like this.

02:58.880 --> 03:01.920
To use this variable
with the min or max function,

03:02.140 --> 03:06.220
the values inside the set can be expanded
to separate arguments.

03:06.760 --> 03:09.180
This is done by using the expansion symbol

03:09.240 --> 03:12.580
by making use of three periods
or full stops like this.

03:13.680 --> 03:17.920
Some other commonly used numeric functions
are the ceil and the floor functions.

03:19.400 --> 03:21.960
The ciel function rounds
to the closest whole number

03:22.060 --> 03:25.140
that is greater than or equal
to the current argument.

03:26.600 --> 03:30.860
For example, ceil of 10.1
wil return the value of 11,

03:31.120 --> 03:33.160
same as ceil 10.9

03:33.340 --> 03:38.060
as 11 is the closest whole number greater than
either of these two floating numbers.

03:39.280 --> 03:42.180
Floor does the opposite
and returns the closest whole number

03:42.240 --> 03:45.240
that is lesser than or equal to the argument
that is provided.

03:46.700 --> 03:49.280
Let us now look at some string functions.

03:49.860 --> 03:53.740
These functions are used to transform
or manipulate string type data.

03:55.260 --> 03:58.580
The first one we will take a look at
is the split function.

03:59.100 --> 04:01.560
This function is used to transform a string

04:01.660 --> 04:04.740
to a list of elements
by making use of a separator.

04:07.180 --> 04:09.600
The syntax to do this is the split function

04:09.680 --> 04:12.820
followed by a separator and the string
enclosed within brackets.

04:14.060 --> 04:16.960
Again, this can be tested
using the terraform console.

04:19.180 --> 04:21.460
Notice that the string we have used here.

04:21.560 --> 04:25.260
has a few dummy AMI IDs
that are separated by a comma.

04:27.120 --> 04:30.140
To split this string
to a list containing 3 elements,

04:30.300 --> 04:32.700
we can use the split function
with the separator

04:32.780 --> 04:35.740
which is comma,
and the complete string, like this.

04:38.880 --> 04:41.240
Notice that the output is a list now.

04:43.200 --> 04:47.660
Just like before, let’s also make use
of a variable and test this function.

04:48.540 --> 04:52.000
Here, we var called AMI with the same string.

04:52.900 --> 04:56.040
Passing in the variable
as the second argument to this function,

04:56.220 --> 04:59.200
comma being the first,
we get the same result as before

05:01.060 --> 05:05.760
Notice that in the variable called AMI,
there is a text that is all in upper case.

05:07.840 --> 05:13.380
To convert the string to use only lower case,
we can make use the lower function like this.

05:14.180 --> 05:17.600
To do the opposite
and change it all to upper case letters,

05:17.720 --> 05:19.380
use the function Upper.

05:23.740 --> 05:26.820
To just convert the first letter
of each word in a string,

05:26.880 --> 05:28.900
use the tile function like this.

05:29.420 --> 05:33.320
The substring function is used
to extract a substring from a string

05:33.680 --> 05:36.800
or in other words,
cut a string in to a smaller string

05:36.900 --> 05:39.060
by providing an offset and a length.

05:40.140 --> 05:44.900
The offset defines the index of the character
after which we want to cut the string.

05:45.480 --> 05:49.200
For example, to cut
from the very first character of the string,

05:49.380 --> 05:51.360
we must provide the offset as 0.

05:52.160 --> 05:56.500
To cut from the second character,
use the offset of 1 and so on and so forth.

05:57.280 --> 06:00.260
The length defines
the number of charters from the offset

06:00.320 --> 06:02.240
to cut and convert to a substring.

06:03.660 --> 06:08.940
Excluding the commas, the length of AMI-ID
in this example variable is 7.

06:10.880 --> 06:14.000
As such, we can create substrings
using the length of 7.

06:14.460 --> 06:17.300
The offset can be 0, 8, or 16.

06:22.860 --> 06:25.540
The join function does the opposite of split.

06:25.920 --> 06:29.320
It converts a list to a string
by adding them all together.

06:32.040 --> 06:34.440
Let’s now take a look at collection functions

06:34.500 --> 06:38.380
which are used for collection data types
such as set, list and map.

06:40.160 --> 06:42.700
We have already seen one example
in this category,

06:42.760 --> 06:47.200
which is the length function that we used
to determine the number of elements in a list.

06:48.040 --> 06:52.380
To find the index of a matching element,
we can make use of the index function.

06:53.880 --> 06:56.460
For example, to find the index or location

06:56.520 --> 07:00.660
of the element called AMI-ABC
in the AMI variable,

07:00.860 --> 07:03.400
we use the index function like this.

07:04.560 --> 07:08.120
This function takes two arguments,
the first is the list

07:08.220 --> 07:12.360
and the second is the value of the element
for which we want to find the index.

07:12.960 --> 07:17.800
In this example, the element AMI-ABC
is located at index 1.

07:18.720 --> 07:21.940
Remember that the index
of a list always starts with 0.

07:23.260 --> 07:26.920
To find an element in a list
located at a specific index,

07:27.040 --> 07:29.900
we can use the element function like this.

07:31.180 --> 07:37.420
Here, the element located at index 2
is the AMI ID called AMI-EFG.

07:40.180 --> 07:43.900
To check if a specific element
is present in a list or not

07:44.000 --> 07:46.180
use the contains function like this.

07:47.880 --> 07:51.140
This function will return a value
of true or false.

07:51.920 --> 07:56.440
In the first example,
the element AMI-ABC, all upper case,

07:56.680 --> 07:59.560
is present in the list and hence,
returns a value of true.

08:00.300 --> 08:05.240
In the second example,
the element AMI-XYZ, all upper cases again,

08:05.320 --> 08:09.160
returns a value of false
as such an element is not present in the list.

08:10.700 --> 08:15.640
This is despite the fact that the same value
with all lower cases is present in the list.

08:18.840 --> 08:23.380
To wrap up this lecture, lets have a look
at some functions used for map data types.

08:24.460 --> 08:28.060
Here, we have an updated AMI variable
which is now a map.

08:29.460 --> 08:34.080
This map uses region as a key
and an associated AMI ID as the value.

08:35.120 --> 08:38.220
To convert this map
to a list with just the keys,

08:38.300 --> 08:43.000
we can use the keys function
and supply the map as an argument like this.

08:44.440 --> 08:46.640
To convert it to a list with just the values,

08:46.820 --> 08:50.520
which are the AMI IDs in this example,
use the values function.

08:51.440 --> 08:54.900
Finally, to look up the value
of a specific key in a map,

08:55.000 --> 08:57.700
we can use the lookup function like this.

08:58.740 --> 09:02.120
This function takes two arguments,
the map and the key

09:02.240 --> 09:04.060
for which we want to look up the value.

09:06.880 --> 09:10.080
If the key provided to this function
is not available in the map,

09:10.160 --> 09:11.620
this will result in an error.

09:12.240 --> 09:15.940
However, optionally, we can pass
in a third argument to this function,

09:16.420 --> 09:20.400
this is the default value that will be returned
if the key provided in the argument

09:20.480 --> 09:22.900
does not exist in the map like this.

09:25.820 --> 09:30.320
In this case, the lookup function
will return the value AMI-PQR

09:30.520 --> 09:34.460
even though the key us-west-2
does not exist within the map.

09:35.120 --> 09:39.120
This is because this value
has been supplied to the function as a default.

