9.1 Terraform Functions

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

Video Transcription
welcome to Module 4.6.
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.
What I'd like to do is open up the actual terra form documentation on the functions.
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.
You can see in the dark temptation that's grouped
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
into a different type for some reason,
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,
you have those again, I'm not gonna cover all of these
absolute value rounding up to the nearest hole number a rounding down to the nearest full number. Um,
we have the max function and the main function
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
actually, the 06 directory opened, and I'm gonna open up the read me
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
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,
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
have value when you're tinkering and learning and kind of exploring functions, manipulations, expressions.
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
the max function. I did a little copy and paste to move things along.
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
complicated and exploring different types and using
having passing values directly from one function to the next. And and, um,
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,
joining multiple strings. Can Cat Nation
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
back in when we're talking about creating custom tear for modules,
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
to merge together Ah, a variety of different tags
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.
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
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
returning you the length of the string, some sort
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
work with the Jason Decode function because I think this is
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
other pseudo configurations into your terra form file or,
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,
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
that incorporates the date time stamp in the file name.
Hashing crypto functions
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,
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
using key vaults hosted in the different
cloud providers.
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
different functions.
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
from which I started the Terra form counsel,
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,
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
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
I'm gonna play around a little bit here and say, OK, let's copy this.
let's go back to the console, Let's paste it
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,
which is the colors value. So if I look at this example
file We could see. Okay, there's the bracket. Then there's the colors. And then everything else is within
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.
So back down the console. Okay, that Jason
decoding the file. And then we drilled into the colors attributes here,
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
that's the length of this particular array.
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
for the zero element
on the colors array,
and it's category, code, color and type. If I look at this, look at that category
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.
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
or something like that. I think this
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
the de facto definition of what are all the functions that Terra form has to offer
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