Time
2 hours 48 minutes
Difficulty
Intermediate
CEU/CPE
3

Video Transcription

00:00
There's another way to override the values of variables besides just editing the Terra form file directly and changing the default.
00:10
And besides creating a T f of ours and in order of precedence, this kind of sits in the middle. So your your your default value is default, right? Um,
00:24
at the very top of the chain. If you've defined to to the command line argument and told it used this TF bars file. The values in this style are going to trump anything else. But there is 1/3 way of doing this. So let's say I want to do, um
00:41
and when. And this is using environment variables. And one of the valuable things for this is the
00:48
value of an environment. Variable is managed in memory. It's never really stored the disc, whereas TF bars in this circumstance is clearly a file residing on my desk. Maybe I check it into source control or something like that, too. But if I have secrets, if I have keys, defining those in environment variables is very,
01:04
very valuable and powerful in terms of keeping those secrets truly secret. So they're not sitting around in files that could be discovered by somebody with malicious intent.
01:15
So I'm gonna go ahead. I'm going to delete the location from T s bars and I want you to follow along with me and do the same. So at this point, if we were to run the plan
01:26
perfect, I'll just run the plan for you. We run the plan, it's gonna have a resource group called customer one. But the location itself is going to be the, um,
01:40
the default value location, which is West. Us, too.
01:46
So let's take a look at the plan and make sure that, indeed West us to is the location, because that's the default value.
01:53
But if I want to
01:57
define this value for location, let's say for some reason I don't want to put it in a t f. R is found file on. And for this example, what we're gonna do is we're actually going to define a variable and environment variable, and the way this works is if it has to be the The convention is
02:15
uppercase TF underscore var underscore
02:19
the name of the variable. So in this case, we want the variable to be called location. And let's say, instead of making it in West us, too. We want to make it in West us. So now I have to find this environment variable
02:32
that is going to be read by terra form when evaluating the plan or even X executing the plan.
02:40
So if I go ahead and I run the TF bars the plan, it's going to spit this out. And instead of the location being west US two, it's gonna be West us because that's the value that was defined in the environment. Variable
02:57
coming back to the TF bars. If we add the location equals East us again to this, that's gonna take precedence over the value of the environment. Variable. So when I run Terra Form plan again now, with location being defined in the TF bars file,
03:15
the value is going to be East us
03:21
now that we've covered input variables, defining them in in a variables file, giving them default values, giving them types.
03:29
Um,
03:30
we've talked about overriding those default values with either an environment variable or with a T f. Farr's file. Let's go ahead and talk about output variables. So what happens when the terra form script runs and we may wanna pass certain information
03:51
back
03:52
two other Terra Form scripts or to shell scripts that are calling the Terra form scripts. There's a lot of reasons you might want to do this kind of an output, so you'll notice. I have an outputs dot t f file and we'll open that up here and you can see it up top. I have to find two outputs
04:09
that are going to be the result of running this terra form
04:12
package. One is the idea of the resource group, and the other is the virtual network name, and you can see here. The idea is something that's gonna be known after applying. And then the virtual network name is actually something that's going to be determined when, prior to the plan. But for argument's sake, I wanted to
04:31
to show you
04:32
doing two kinds of things. One's a variable that gets determined after it's the execution happens. The other variable is actually going to be a value.
04:43
In this circumstance, we can see here that the value of the variable the name of the virtual network is east us Dash dash V net.
04:50
Okay, so those air output variables we'll get more into those when we dive into modules, but it is a type of variable that's important to cover the next and final variable that I want Thio touch on. If you were looking at at Main Terra Form here, you probably noticed
05:10
that the name of the virtual network it says local dot veena. It doesn't save our dot Veena was his local Davina it and you probably also seen there's a locals got tear form file, and in that file I've actually defined a Value V Net name.
05:29
And the value of that is, is it
05:30
joining off the VAR location and a V net extension?
05:36
What you'll actually want to do is this is a powerful situation when you have variable values. Let's say that you don't want to get you have a convention, and you don't want somebody to override this
05:54
with either an environment variable with a T F bars. You want these to be more or less calculated values
06:00
that air calculated as a result of certain inputs from your into your terror form file, right, certain variable. So in this case, I'm creating and saying
06:13
I wanna have a convention
06:15
so that the virtual network name is gonna be the location Dash V. Net. And so all of my virtual networks, let's stay within the company. We say whenever we create a virtual network resource, we always wanted to be Dash V net. We wanted to be the region that the virtual network exists. Desk Jeanette.
06:33
No,
06:34
you probably don't want to follow that practice, But for this circumstance in this example, I hope it brings true to you what local variables are and and why you'd want to use them to really calculate something based on other input variables
06:51
and not allow that calculation to be readily overridden
06:56
through either a t f bars or through through an environment variable when the terror form script itself is applied.
07:03
So finally, let's run it. We did tear form plan a few times. I want to run terra form plan one more time because we've been playing around with this, and sure enough, I run it again and I get prompted with the resource group names, so I don't want to do that. I'm gonna pass in TF bars
07:23
because we're going to create this for customer one
07:26
in this particular circumstance, and we know what the values of this TF bars are. And sure enough, it has a plan. It's all ready to do it. So let's go ahead. Let's say terra form, Um,
07:39
we're gonna go terra form apply. And we also need to make sure that we passed the VAR file argument so that it takes into account the values defined in this TF bars value file when it's actually executing the apply.
07:55
So let's give this a few minutes while it constructs the resource group puts together the virtual network, and we will come right back.

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