Time
2 hours 48 minutes
Difficulty
Intermediate
CEU/CPE
3

Video Transcription

00:00
in this section, We're going to discuss tear for modules.
00:05
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
00:22
put in this particular resource group.
00:25
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.
00:38
Um,
00:39
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
00:58
I can do is
01:00
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.
01:18
Please go ahead and open up Folder three specifically the main terra form file in folders. Three
01:25
First thing you'll notice here is we have a module declaration.
01:27
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
01:46
Relatives who are other teller form scripts and until we're referencing it from our other tell terra form scripts,
01:52
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.
02:05
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.
02:20
This is what you defining for this particular module. What are the inputs on and what do they mean? So we can see here
02:27
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.
02:46
So if I take a look at the main terra form file in our model, this is really defining the meat behind our module.
02:53
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.
03:14
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
03:32
when the tear form script runs
03:35
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
03:46
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.
03:58
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,
04:16
let's say we want to require that an environment
04:20
tag be associated with this resource group as well as a customer I d tag associated with this resource group.
04:27
So when we're defining tags for this particular resource,
04:30
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,
04:49
both environment and customer, i d are defined variables. And this way we're enforcing the way our resource groups get created.
04:58
Um,
04:59
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
05:16
in other to reform scripts that are relying on this particular module.
05:19
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.
05:39
Now, doing data input validation in terra form for modules or intern anyway is kind of tricky.
05:46
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.
06:05
And then if we go back into the main
06:09
terra form file for this particular module, you'll see
06:13
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,
06:29
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
06:46
is it match either deaf que a stage or production
06:50
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,
07:05
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
07:24
have one of several
07:26
predefined values and nothing else. So feel free to use this strategy if you encounter a similar situation.

Up Next

Fundamentals of Terraform

In this Terraform training course, students will learn the basics of the tool through hands-on labs, become comfortable with its core language features, learn how Terraform fits into the cloud tooling landscape.

Instructed By

Instructor Profile Image
James Leone
Cloud, IoT & DevSecOps at Abbott
Instructor