-In this lecture, we will take a look at some more functions
that are available in Terraform.
We have used a few Terraform functions throughout this course,
most notably, the file function to read data from a file.
We have also used the length to determine the number of elements in a given list or a map.
We also had a look at the toset function, which is used to convert a list to a set.
Although we have used functions before,
we have done so by using it in the configuration files
without seeing what the functions do specifically
and how it transforms and combines values provided to it.
Fortunately, Terraform provides an interactive console
that can be used for testing functions as well as interpolations.
To get into this interactive console, we use the command terraform console.
The interactive console loads the state associated
with the configuration directory by default
allowing us to load any values that is currently stored in it.
It also loads variables that are stored within configuration files.
This allows us to experiment with the functions and interpolations
that can later be made use of within the configuration files.
For example, we can test the file function in the console like this
and it should return the contents of the main.tf file.
Using the length functions shows that the number of elements
in the list variable called region is 3.
Finally, we can see that the toset function converts the same variable
from a list to a set and removes the duplicates.
Remember that a set cannot have duplicate values
so during this transformation,
the toset function removes the duplicate value for us-east-1
which is present in the list variable twice.
Besides the three functions,
there are dozens of other useful built in functions available in Terraform.
We will take a look at some of the commonly used ones next.
There are several categories of functions available in Terraform.
However, in this course, we will only take a look at some of the commonly used ones.
Specifically, ones belonging to Numeric Functions,
String Functions, Collection Functions, and Type Conversion functions.
Numeric Functions, as the name suggests, is used to transform and manipulate numbers.
For example, we can use the max function
to return the greatest number from a set of numbers.
The min function does the reverse and returns the smallest number.
To use variables as arguments for a function, we have to use a slightly different syntax.
For example, consider a block for a variable called "num" like this.
To use this variable with the min or max function,
the values inside the set can be expanded to separate arguments.
This is done by using the expansion symbol
by making use of three periods or full stops like this.
Some other commonly used numeric functions are the ceil and the floor functions.
The ciel function rounds to the closest whole number
that is greater than or equal to the current argument.
For example, ceil of 10.1 wil return the value of 11,
same as ceil 10.9
as 11 is the closest whole number greater than either of these two floating numbers.
Floor does the opposite and returns the closest whole number
that is lesser than or equal to the argument that is provided.
Let us now look at some string functions.
These functions are used to transform or manipulate string type data.
The first one we will take a look at is the split function.
This function is used to transform a string
to a list of elements by making use of a separator.
The syntax to do this is the split function
followed by a separator and the string enclosed within brackets.
Again, this can be tested using the terraform console.
Notice that the string we have used here.
has a few dummy AMI IDs that are separated by a comma.
To split this string to a list containing 3 elements,
we can use the split function with the separator
which is comma, and the complete string, like this.
Notice that the output is a list now.
Just like before, let’s also make use of a variable and test this function.
Here, we var called AMI with the same string.
Passing in the variable as the second argument to this function,
comma being the first, we get the same result as before
Notice that in the variable called AMI, there is a text that is all in upper case.
To convert the string to use only lower case, we can make use the lower function like this.
To do the opposite and change it all to upper case letters,
use the function Upper.
To just convert the first letter of each word in a string,
use the tile function like this.
The substring function is used to extract a substring from a string
or in other words, cut a string in to a smaller string
by providing an offset and a length.
The offset defines the index of the character after which we want to cut the string.
For example, to cut from the very first character of the string,
we must provide the offset as 0.
To cut from the second character, use the offset of 1 and so on and so forth.
The length defines the number of charters from the offset
to cut and convert to a substring.
Excluding the commas, the length of AMI-ID in this example variable is 7.
As such, we can create substrings using the length of 7.
The offset can be 0, 8, or 16.
The join function does the opposite of split.
It converts a list to a string by adding them all together.
Let’s now take a look at collection functions
which are used for collection data types such as set, list and map.
We have already seen one example in this category,
which is the length function that we used to determine the number of elements in a list.
To find the index of a matching element, we can make use of the index function.
For example, to find the index or location
of the element called AMI-ABC in the AMI variable,
we use the index function like this.
This function takes two arguments, the first is the list
and the second is the value of the element for which we want to find the index.
In this example, the element AMI-ABC is located at index 1.
Remember that the index of a list always starts with 0.
To find an element in a list located at a specific index,
we can use the element function like this.
Here, the element located at index 2 is the AMI ID called AMI-EFG.
To check if a specific element is present in a list or not
use the contains function like this.
This function will return a value of true or false.
In the first example, the element AMI-ABC, all upper case,
is present in the list and hence, returns a value of true.
In the second example, the element AMI-XYZ, all upper cases again,
returns a value of false as such an element is not present in the list.
This is despite the fact that the same value with all lower cases is present in the list.
To wrap up this lecture, lets have a look at some functions used for map data types.
Here, we have an updated AMI variable which is now a map.
This map uses region as a key and an associated AMI ID as the value.
To convert this map to a list with just the keys,
we can use the keys function and supply the map as an argument like this.
To convert it to a list with just the values,
which are the AMI IDs in this example, use the values function.
Finally, to look up the value of a specific key in a map,
we can use the lookup function like this.
This function takes two arguments, the map and the key
for which we want to look up the value.
If the key provided to this function is not available in the map,
this will result in an error.
However, optionally, we can pass in a third argument to this function,
this is the default value that will be returned if the key provided in the argument
does not exist in the map like this.
In this case, the lookup function will return the value AMI-PQR
even though the key us-west-2 does not exist within the map.
This is because this value has been supplied to the function as a default.