2 hours 48 minutes
in this section, We're going to discuss tear for modules.
I'm gonna take a second here to open up the terra form file from the last lesson. And in this file, we actually are defining two different resource is terra form resources, and we're establishing some inter relationships between these Resource is right. So we're making sure this virtual network is always
put in this particular resource group.
If you can imagine, we have these. These resource is which are like building blocks, and in this case, we're assembling them following a certain pattern. Specifically, we want to make sure the virtual network goes within the resource group.
as your team starts building more and more cloud infrastructure, you're gonna start using patterns over and over again. So rather than having individuals on your team kind of cut and paste blocks of multiple resource is from one terra form file to another. What you
I can do is
leverage the terra form modules themselves, and that's what we're gonna be discussing. Um, and it really helps create some work consistency in the the naming conventions and the patterns used to assemble multiple relieve the resource is to accomplish an end Goal.
Please go ahead and open up Folder three specifically the main terra form file in folders. Three
First thing you'll notice here is we have a module declaration.
In this case, the source to the module is in a subdirectory of the current terra form file. So this is a good example of a custom module that we're creating within our own private use within the team within the company. And we're distributing it and managing it in a subdirectory
Relatives who are other teller form scripts and until we're referencing it from our other tell terra form scripts,
you can see and this module called RG, which happens to be a module to create resource groups, create except several variable inputs name, customer I d, location and environment.
So what I want to do now is to have a little deeper into the custom modules and how you create him and how you define him. Here. You'll see there's the sub director of modules and then RG And beneath that I have several terra formed flocks. 1st 1 we're going to take a look at here is the variables file.
This is what you defining for this particular module. What are the inputs on and what do they mean? So we can see here
We have one called name and holds a short name of the resource group that is going to be created as a result of using this module, another variable called Environment one called Custom I D. And we have two optional variables. We have location with a default value of West us and then, of course, tags, which is a hash map.
So if I take a look at the main terra form file in our model, this is really defining the meat behind our module.
Um, you can see it has a TZAR provider and is creating a resource group. We'll talk about this here in a second. The name is being referenced by the input variable name. And then we've decided in this fictitious organization that we wantto have all the resource groups and in a dash R g.
So just taking a moment to look at the syntax here you can see I am actually dynamically creating the name, and it's composed of the value of the variable name and so terra form sees the dollar sign curly brackets. Bardot named. That's telling it substitute in the actual value of the variable name
when the tear form script runs
and then it's gonna upend the dash. RG. So this is a very common use of creating an interpreter waiting strings in the Hash Corp configuration language
and then below that were just carrying forward the actual value of our location. We're not going to manipulate that value, our upend anything to it.
This one is worth taking a moment to focus on here, the tax. So in this victim fictitious organization were saying, You know, we want we want module. We want resource groups to always have a dash RG in. Our model is helping enforce that. But we're also saying, you know, for every time we make a resource group,
let's say we want to require that an environment
tag be associated with this resource group as well as a customer I d tag associated with this resource group.
So when we're defining tags for this particular resource,
um, we're leveraging a function which we'll talk about in a later model during this training. But it the function is is essentially merging two maps. One is the input map called tags and then the other is this map that we are creating because if you recall, when we looked at the input variables,
both environment and customer, i d are defined variables. And this way we're enforcing the way our resource groups get created.
take a moment to look at the outputs directory, and here we are passing along the Resource Group I. D is one of the output directory options. And then, of course, the resource group object itself is being passed as an output parameter. And that's going to be used in
in other to reform scripts that are relying on this particular module.
Take a look at the locals variables, and here you can see we've defined a set of valid values for the environment. So in our company, we wanted to create a tag on every resource group. But let's say we only want the environment values for that tag to be of a defined input.
Now, doing data input validation in terra form for modules or intern anyway is kind of tricky.
So what I'm showing you here is a technique that you can use to actually validate inputs. If you say OK, I'm asking for a certain strength and I only want the values to be one of this set enumeration. So in this case, death que a stage and production I'm defining a local variable which is going to be that set.
And then if we go back into the main
terra form file for this particular module, you'll see
we are actually leveraging this this local set variable and using this special attributes that all resource is in terra form have with them called Count and again calling a different function here. And what I'm saying is,
does the environment invariable passed in called environment Does this variable? Is the value contained within this set of the valid He envies and you recall again, this is the locals here. So in other words I'm saying does the past in variable called Environment
is it match either deaf que a stage or production
And then, by using the contains function that's gonna return a true or false If it's true, then we want to great this resource group. If it's false, we're gonna count of zero, which means this resource group isn't gonna created it's gonna throw some errors,
not necessarily the most intuitive. There's it's it's not gonna have an heir that says the value of the past invariable called Environment can only be one of these Dev que a stage and production. But it really is the best approach I've seen in terra form to emulate the in new Marais Shin Data type and enforce that variable
have one of several
predefined values and nothing else. So feel free to use this strategy if you encounter a similar situation.
Kali Linux Fundamentals
In this Kali Linux course you will learn about the industry standard tool for penetration ...
1 CEU/CPE Hours Available
Certificate of Completion Offered