Time
2 hours 48 minutes
Difficulty
Intermediate
CEU/CPE
3

Video Transcription

00:02
all right. So everything ran smooth here. We could see there's some outputs produced at the very end
00:08
of certain public eye peas. And this is the girl that corresponds to each of the three containers that were deployed. And we can see central, US, West, US and East Asia us because we wanted to have a container deployed in each one of those regions for one of three different customers.
00:25
Let's take a look at the outputs file That was the result of this. This is a good example of a four loop, very comparable to the four each loop, the syntax and structures a little bit different
00:36
in this situation. We have the container group, right, And that's a map of the map that has three elements in it. The keys being customer, one customer to customer three. And you can see what I've done here is I'm saying four c c being container. Just a single acronym.
00:52
Ah, single letter variable here in that container group. So for each one of those those items that reside in this container group map, I wanted to make the value of this output public at these very well be the fully qualified domain name of that container. So this is another good example here
01:10
of using a four loop in your outputs if you wantto
01:12
return. An array of outputs are in this circumstance and returning on array of strings. And I'm determining the values in that array by looping through the materials a map or looping through a list or a set of some other things. Right? Because when we're going in this dynamic Ralph, I'm getting a little more procedural. Now we're
01:33
dealing a lot more with
01:34
multi dimensional elements, collections that have many different items that belong to them.
01:42
Last but not least, I want to talk about conditional Sze, specifically the if statement
01:48
and I want to talk about the count attributes. So we talked about the four each attributes for a resource.
01:55
But what I didn't review in the file that that existed there and you might have noticed if you were exploring herself was the count resource and some of these other used the use of conditional Sze.
02:07
So the accountant in condition als go hand in hand very common. You can use count really that the value of that you set for count is going to reflect in that terra form is going to create that many instances of whatever
02:25
particular resource item you defined,
02:29
but in a very common to see using count to conditionally create a resource or notch. So if the count to zero
02:38
that resource isn't gonna be created. If the count is one than the resource, one instance of that resource will be created. And so, in this circumstance, what I've put together, as I have said, if the length of the customers map
02:51
is greater than zero,
02:53
there goes question mark. If that's true, then the count for this particular resource group, which I've called the No Customers Resource group,
03:01
is going to be zero.
03:04
Otherwise, the count is gonna be one. So that little Colin is saying, you know, it's gonna goes condition, question, mark, value of true colon
03:13
value if false. So we were going to If if it's not greater than zero, then we will be creating this customer's resource group. So in this theoretical example, we're saying we always want to deploy something, even if we don't have customers right. Anytime we run this terra form script,
03:30
I did the same thing below for another container group.
03:35
Um, and
03:38
it's gonna created under the same circumstances if there are no customers.
03:43
And just like before, when we were dealing with the container group and dealing with the four each that were coming from a resource group up here, we wanted to make a reference to that resource group to determine what's the name in the location that this container group should reside and we used brackets. And then we had to reference it by keys,
04:02
because when we're using for each and we're making reference to a resource item
04:06
that has the four each construct, that resource item is a map
04:12
in the count perspective. It's a little bit different in that that resource item is not a map. Rather, it's it's a defined list of things. So in this circumstance I'm putting a number zero because I know the count is either gonna be zero or one, and the first element
04:30
in the no customers
04:32
list is going to be element zero. So I'm referencing the resource group created here because it's account. It's now a collection or a list or a set, Um,
04:45
and I'm gonna use the first element there and then the same strategy for location. So I wanted to bring that full circle for you so you could see the use of count the use of for each as well as how you have to reference resource is
05:01
that are created. If they have counter, they have for each.
05:05
When you're making cross references to him, you have to reference them either by new Miracle Index or buy a key. This is a This is a little little unique and now we're really pushing the edge of terra form. But let's let's take a moment and and make a little change here and actually get rid of all the customers,
05:25
I'm gonna delete all the customers,
05:27
and I'm gonna ask Terra Form to let me know. Okay, As a result of doing this, what is your plan of action here? It's going through refreshing state, and what we would expect is there's no customers. So we were expected. It's gonna get rid of all those customer resource groups,
05:44
all the containers that pertain to the customer resource groups. Right?
05:47
So there's the six items that's going to destroy customer three customer to customer, one right, and then
05:55
because now this
05:57
This criteria for count the Vare customers is not greater than zero. So we're expecting the count to be one. So it is going to create the resource group called No customers. And again here you see, it's referencing of by a zero.
06:13
If I had count equals two, then then there would be a one and there would be a zero and there would be a one.
06:17
But we're not going to do that.
06:19
And then similar. The No Customers Containers group will also be created. And it's going to be destroying the other groups that we previously defined with our with our map. I actually don't want to do that right now. I encourage you to go ahead and run a terra formed, destroy
06:38
um t clean up your your azar instance and get rid of all those containers and resource groups that we just created. But in summary, I hope you cease. Um, several big things that we've learned. We learned about the four each, which is a brand new construct to the language. We learned about the count construct, which has been there quite a while.
06:55
We learned about leveraging condition ALS to conditionally
06:58
um, create something by sending a count to zero or one. That's a very common and powerful strategy. And then we talked about the four loop here in the in the outputs when we were looking at that and how that's all put together. So hopefully this provides you with some value
07:15
when you're getting into creating and solving more advanced problems and creating those
07:20
reusable modules.

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