# 9.1 Terraform Functions

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or

Difficulty
Intermediate
Video Transcription
00:00
welcome to Module 4.6.
00:02
In this section, we're gonna talk about functions specifically, terra form functions up on the screen. Here, you'll see I have the get hub documentation for this site and I drilled into the number 06 directory, which is associated with this module talking about functions.
00:19
What I'd like to do is open up the actual terra form documentation on the functions.
00:24
It's really a great and comprehensive reference for all the different functions that are obviously built into terra form. I'm not going to go through all of these functions. There are a lot of them, but I do wanna touch on a few of them and even do a little playing around with some of these functions so you can see them in action.
00:43
You can see in the dark temptation that's grouped
00:45
the very, very early on in this training. Um, we discussed variables in different types of variables. You can you'll see some alignment here with the function types, a CZ well as other ways, specifically here's type conversion functions. So if you need to manipulate a variable of one type
01:03
into a different type for some reason,
01:06
you can take a look. A TTE thes. They're gonna be handy here. We have numeric functions. So which include parsing functions, believe it or not. So you can If you had a variable of a certain type that, say a strength and you wanted to parse that in turn into an actual and Inger,
01:23
you have those again, I'm not gonna cover all of these
01:26
absolute value rounding up to the nearest hole number a rounding down to the nearest full number. Um,
01:34
we have the max function and the main function
01:38
these air very straightforward in what they do. Um, good little Segway here, though. Let's ah, let's talk about how we can learn more about functions before we return to look at the different kinds of functions. And what I want to introduce to you is Terra form Consul. So over here I have, um
01:57
actually, the 06 directory opened, and I'm gonna open up the read me
02:02
marked down file, actually, for this particular lesson to save myself some typing kind of cheat a little bit, but what we're gonna do down here in the terminal at the bottom of the screen
02:15
is run terra form. Come Consul. I really recommend this utility when you're playing around with functions or some of the other expressions that we're gonna, um,
02:25
come back to one thing to keep in mind when you're interacting with this, there's no use of variables. So if say, we're playing with functions and we wanna assign the output produced by a particular function to another variable that's not gonna work out and similar reading in variables. So this does
02:44
have value when you're tinkering and learning and kind of exploring functions, manipulations, expressions.
02:51
But it is a limited value, and that's just a shortcoming of the way that council works. But what I wanted to do here is, since we were on the topic of the max function, I wanted to instead, and she ate County Reform Council and we're gonna type in
03:07
the max function. I did a little copy and paste to move things along.
03:10
So here we're gonna run the function. And sure enough, we would expect that the number 12 is returned and the number 12 has been returned by the max function. We're gonna play around with that consulate a little bit more once we've continued to make our way through other functions. And in fact, we're going to get a little more
03:28
complicated and exploring different types and using
03:30
having passing values directly from one function to the next. And and, um,
03:37
well, you'll get to see how a lot of this comes together. Eso string functions You could definitely look through these some regular expression stuff. I was actually surprised to see some of the advanced function capabilities that they do have on Dhe. Then, of course, for splitting for bringing together,
03:54
joining multiple strings. Can Cat Nation
03:58
is something that we've done earlier? This is one that I had to play with, and you might find yourself playing with a little bit. T build out strings. Can Cabinet values, if you may recall, this was a function that we used in the custom module
04:14
back in when we're talking about creating custom tear for modules,
04:19
and we use that because we wanted to upend at the very end of Dash R G to the resource group in that particular module, we were creating collection function, so the collections, these air, the lists, these air, the sets, thes air, the maps, and you may recall, we used at the merge function also in our custom module
04:39
to merge together Ah, a variety of different tags
04:42
and create a map the tags map that we then passed along to the to the the resource when it was getting to find in Azar.
04:51
So you've played with that? There's a whole bunch of other ones manipulations examining keys. So this is an interesting one, right? It returns the suite of keys from a particular hash map. Of course, we have the length function for evaluating an air, are extremely an array or list
05:10
all these different types. How many items are there? And it looks like it'll even and evaluate that a string and interpret that as an array of single characters
05:18
returning you the length of the string, some sort
05:24
different searchers circumstances. You're gonna want to use these functions. So browsing through a mall, keeping them maybe in the back your mind is a good idea. But by no means you need to be a full expert on functions in this particular lab activity. We're going thio
05:40
work with the Jason Decode function because I think this is
05:44
this is kind of interesting ability of parsing a string that maybe in the format of a Jason format, and then it will create an object representation in terra form based on what it's parsed. So I think that could be a useful thing if you're feeding
06:02
other pseudo configurations into your terra form file or,
06:05
of course, reading files, these air the file system functions so reading files from the system from by which the Terra form apply and operation is taking place. Um,
06:17
so the most basic being filed reading file contents but checking for file existence. Ah, variety of things going on their date time, basic manipulations. Formatting, I think, would be a very common thing if you're going to be producing log files. Or you even want to make files that follow certain convention
06:36
that incorporates the date time stamp in the file name.
06:40
Hashing crypto functions
06:42
a lot of creativity on on how you use these in terms of doing. Creating hash is to ensure integrity of things maybe being passed on,
06:50
sent around some contrition, including Arcee encryption decryption, using public private key type circumstance of you have a secret that you're maybe passing from the host to from the from the machine performing the terra form concepts. But I do think there are a few other ways that that are better than that
07:10
using key vaults hosted in the different
07:14
cloud providers.
07:15
But you never know. There may be some nuance with that that doesn't work. So you reach in your back pocket and find the functions here and then I p networking functions. If you really are working with a lot of virtual networks and setting those up, they get pretty deep, including sub net masking and all sorts of things to help you calculate
07:34
different functions.
07:36
Let's play around these a little bit more, and, um, I wanted to dive a little deeper on the file file, function itself on dhe, using that explore that Jason a little bit further. So here we can run the file function. There is an example that Jason file in the same working directory
07:54
from which I started the Terra form counsel,
07:57
and so you can see it really just spits out. The contents of that fact. Will will get into a little more little more zoomed in mode, I think will help you see everything a little bit better,
08:09
uh,
08:11
going back up so we have the file function. So if we want to turn this into a Jason object, we're just gonna move and pass along the file contents that exists. In that example, Jason file to the Jason Decode function, and that's going to return an object, a terra form object
08:31
with all the vile. The values that match the Jason file were explored that object a little bit. Let's say, Well, what is this object? And you can see here just by looking at it that it's has the brackets. So really, the object is Ray is what's being returned. So
08:50
I'm gonna play around a little bit here and say, OK, let's copy this.
08:54
Um,
08:56
let's go back to the console, Let's paste it
09:01
and run the length command on and see How big is this? All right, so it's an array of only one item, which is is kind of interesting If we continue to evaluate Well, let's see. Well, it's not an array. What kind of thing is being returned here? Let's take a look a tw three keys and there's only a single key value,
09:20
which is the colors value. So if I look at this example
09:26
file We could see. Okay, there's the bracket. Then there's the colors. And then everything else is within
09:31
the bracket there. So you know what I bet colors itself is actually gonna be an array and s o. Let's try looking at the colors attributes and seeing what the Reform Council has to stay here.
09:50
So back down the console. Okay, that Jason
09:56
decoding the file. And then we drilled into the colors attributes here,
10:01
and we can see Sure enough, there is a list. It's an array which has all those entries. We could look at the length
10:09
that's the length of this particular array.
10:15
Six.
10:16
Right? So now we're just playing around. We're getting a feeling of what the object types that, um, that are being returned as a result of the decoding the Jason decoding process that's evaluating the file contents. And then last night, at least let's take a look at the keys
10:35
for the zero element
10:37
on the colors array,
10:39
and it's category, code, color and type. If I look at this, look at that category
10:46
code
10:46
color type. So not in the same order A specified in the Jason file. Hopefully that doesn't matter. Hopefully, you're manipulating the object in real time. But more importantly, the reason we went through this activity was to just play around with the Terra form Consul and experiment and understanding how the different functions behave.
11:07
This
11:07
if you're unfamiliar with using a particular function, this approach will really help you string together multiple different functions, play around and get a very rapid feedback on what's going on, as opposed to potentially creating a bunch of terra form files and trying to debug it by adding print statements or
11:26
or something like that. I think this
11:28
this other counsel approach is gonna be very handy tool. And even when we proceed to talk about conditional tze and some of these other loops in expressions that you can use in terra form, that console approach is gonna be very valuable. And then, of course, the
11:45
the de facto definition of what are all the functions that Terra form has to offer
11:50
is gonna be another valuable reference. So that wraps it up about functions in the key points. And I hope it provides you a little insight into being exploratory
Up Next
10.1 Foreach
10.2 Count
11.1 Course Summary