Time
2 hours 48 minutes
Difficulty
Intermediate
CEU/CPE
3

Video Transcription

00:00
welcome back to the fundamentals of Terra form in this module. We're gonna cover loops and conditional Sze
00:07
before I dive too deep into that up on the screen, I've opened the variables Terra form file in the 07 directory of the course and lab materials.
00:18
And on the screen you'll see I have this this for each loop that I've typed in here early on in the Terra form course, we discuss the difference between a declarative language versus a procedural language and one of the aspects of procedural languages is loops and condition, ALS and flow.
00:38
So
00:39
if you were using a common procedural language and let's say you had an array of integers
00:46
and, um
00:49
or a list all use some of the variable terms in the terra form world. Ah, list of integers or a set of integers. Um
00:59
and you have those values A for each loop, which is available in a lot of common languages. C sharp Java allows you to iterated through the elements in those list
01:11
in that procedural fashion. Right? So you all your statements in here gonna be executed. The VAR name is going to be assigned the first value in the list of integers and then this block of be executed. Then bar name is going to be assigned the second value in the list of injures. The block it sends executed and in the loop pattern continues
01:30
until it has made its way through all of the integers that are defined in this list.
01:36
This is the procedural way of doing things, but it's it's a good way to toe Look at this. The same concept could be applied if you say you had a map of values, right? That's key values. And so what this would probably do is work in this, and then the value would be the key. So
01:55
and this might be, let's say, is a key value to a string.
01:59
So you're gonna loop through every one of the keys in that map,
02:04
and you're gonna perform these actions before each loop concept in in Terra Form works similarly. So it starts to blend some of the lines between purely declare a tive style programming
02:20
and the more traditional procedural Stipe style programming. So
02:27
use it with with some discretion. Don't go wild with it, or you can make a real mess but at the same time can really be a valuable thing when you have to deal with more complex scenarios, especially when you're building out modules that air then used within your company.
02:43
And maybe you wanna have a little more logic and functionality taking place.
02:47
So in this particular example, we have a variable a map file defined here. It's a map of strings and the keys for this our customers 12 and three Not particularly unique, I know. But I wanted to keep this simple. And then the values
03:05
for each key are different regions within azar.
03:08
So in this hypothetical example, let's say we wantto make sure that the infrastructure for customer one is set up in the central US. We have set up same infrastructure for customer, too. But we put that in the West US region, and comparably, we're going to do the same for customer three, except we're gonna put it over in the East Asia region.
03:29
Okay,
03:29
this is the customers variable. So I navigate over to the main terra form file.
03:35
Now we've got a provider. We have a random i d. And let's take a look at this resource group you've seen this snippet quite a few times so far in the in the training, but never quite like this. I've added this for each element to it.
03:50
And by doing this, when Terra form puts together its plan and that applies this template,
03:55
it's gonna logically do a loop through each one of the keys in that customers map,
04:02
right? And then it's gonna apply the values of those keys and interpret them at runtime.
04:10
In this particular case, it's gonna end up creating a total of three different resource groups 1 to 3 different resource groups, such that the name of the resource group is going to be the key.
04:21
And the location of the resource group is the value. So I'm iterating through that map and relying on the keys of the map and the values associated with each one of those keys in that map element to create this resource group or to create three resource groups. But that's what I'm defining here.
04:41
So we're starting to blur those lines again and get in more procedural. I've done something very similar
04:46
in the resource below, which is a container group. You may recall the lab where you went out and you figured out how to use a terra form module to simplify setting up a container group. I'm not using that module because the four each concept that I'm talking about doesn't actually work
05:04
in a module construct. But when you're doing it in a resource is
05:08
declaration. That's when it works.
05:11
Um, and we're going to deploy this hello world module, which was that really simple web page. And we're doing the same thing here we're going to do for each and we're gonna loop through that map of different customers and we're gonna deploy the module and then and we wantto essentially get one container in each one of those three resource groups that are getting created up here
05:30
and we're gonna be making reference to those resource groups.
05:33
This is also a concept we've done, and we covered it back when we were talking about variables and making references to other Resource is between reform. Resource is in creating that dependency tree.
05:44
What you'll notice here, though, is when I'm making the reference to the resource group called r G. I'm not just then going RG dot name because in this context, we've said this R G E is a four each. So what that means is itself now becomes a map.
06:00
So I need to reference particular resource elements in that map
06:04
by the key to which they've been defined. So in this case, I'm doing the loop. I'm going to say, Oh, the key that matches the current key that I'm looping through in the customer's map file and I'm gonna pull out the name and then, comparably, I'm gonna pull out the location to make sure that this particular container
06:23
is put in the appropriate resource group and, of course, put in the appropriate
06:26
location.
06:29
Not much else exciting going on with the container group. But this is This is a new concept in the way that I'm referencing another item created out of the four each. And I'm also creating multiple for each. So what we're gonna do is drop to the command line and have terra form tell us the plan
06:48
based on this file that we've created based on our use of for each and how we've defined this map of three elements,
06:57
and I'm gonna cruise up through it here and just start with the resource groups. So right off the bat we've seen this before, but you'll notice there's the brackets. Customer one. So what it's saying is the RG um which is a map, right? RG is a map. The key customer one
07:15
is going to correlate with this resource group here
07:17
which is in the central US location. There's gonna be a key element customer to which will have a resource group item associated with that that's in the West US location and then finally customer three. And then if I scroll up a little bit further, all notice that it's doing a very similar thing
07:36
for the container groups. We have customer one
07:40
being associated with the resource group called Customer One Resource group and being located in the central US region, as well as other information that's been hard coded in our file moving down. We have customer too, and then we have a customer. Three. So, by using this for each construct up here
07:59
in the terra form file
08:01
ah, it it is actually now going to result in us creating three resource groups, each of those three resource groups having a container and sense with that hello world. So go ahead and kick that off.
08:13
Hopefully you find this for each will be a valuable tool to keep in your belt. In the circumstance that you don't want to keep cutting and pasting the same thing, right, I I could have pasted that resource group definition up here could have done that three times, and we could've cut and paste the container group three times.
08:33
But now we're able to
08:35
to make our terra form file a little bit more dynamic and, um
08:41
and in a sense, a little bit simpler. But it does require a little more analysis when you're reading what's going on. But these kind of things are going to be very, very powerful when you're creating advanced modules for yourself because you maybe have a recurring pattern of constructing different building blocks in the cloud that your team wants two years.
09:00
So let's go ahead and let that run,
09:01
and then we'll return to the video

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