5.3 Outputs and Locals

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

2 hours 48 minutes
Video Transcription
There's another way to override the values of variables besides just editing the Terra form file directly and changing the default.
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,
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
and when. And this is using environment variables. And one of the valuable things for this is the
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,
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.
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
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,
the default value location, which is West. Us, too.
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.
But if I want to
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
uppercase TF underscore var underscore
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
that is going to be read by terra form when evaluating the plan or even X executing the plan.
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
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,
the value is going to be East us
now that we've covered input variables, defining them in in a variables file, giving them default values, giving them types.
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
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
that are going to be the result of running this terra form
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
to show you
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.
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.
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
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.
And the value of that is, is it
joining off the VAR location and a V net extension?
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
with either an environment variable with a T F bars. You want these to be more or less calculated values
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
I wanna have a convention
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.
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
and not allow that calculation to be readily overridden
through either a t f bars or through through an environment variable when the terror form script itself is applied.
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
because we're going to create this for customer one
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,
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.
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