2 hours 48 minutes
welcome back to the fundamentals of Terra form in this module. We're gonna cover loops and conditional Sze
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.
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.
if you were using a common procedural language and let's say you had an array of integers
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
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
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
until it has made its way through all of the integers that are defined in this list.
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
and this might be, let's say, is a key value to a string.
So you're gonna loop through every one of the keys in that map,
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
and the more traditional procedural Stipe style programming. So
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.
And maybe you wanna have a little more logic and functionality taking place.
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
for each key are different regions within azar.
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.
this is the customers variable. So I navigate over to the main terra form file.
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.
And by doing this, when Terra form puts together its plan and that applies this template,
it's gonna logically do a loop through each one of the keys in that customers map,
right? And then it's gonna apply the values of those keys and interpret them at runtime.
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.
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.
So we're starting to blur those lines again and get in more procedural. I've done something very similar
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
in a module construct. But when you're doing it in a resource is
declaration. That's when it works.
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
and we're gonna be making reference to those resource groups.
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.
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.
So I need to reference particular resource elements in that map
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
is put in the appropriate resource group and, of course, put in the appropriate
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
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,
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
is going to correlate with this resource group here
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
for the container groups. We have customer one
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
in the terra form file
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.
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.
But now we're able to
to make our terra form file a little bit more dynamic and, um
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.
So let's go ahead and let that run,
and then we'll return to the video