Cloud Infrastructure: Startup to Scale

Terraform Variables Q&A

Erik Reinert
TheAltF4Stream
Cloud Infrastructure: Startup to Scale

Lesson Description

The "Terraform Variables Q&A" Lesson is part of the full, Cloud Infrastructure: Startup to Scale course featured in this preview video. Here's what you'd learn in this lesson:

Erik spends a few minutes answering questions about how Terraform handles variables. The naming convention for the fines in the module is a convention and not required. When Terraform finds a variable declaration, it exposes it to the module so it can be passed down to any resources or submodules.

Preview
Close

Transcript from the "Terraform Variables Q&A" Lesson

[00:00:00]
>> Male: So if you go back into that file.
>> Erik Reinert: Which one?
>> Male: The environment one that we were just in.
>> Erik Reinert: Yep.
>> Male: So like for example, on line 14, the VAR name that's going to be like, where is that coming from?
>> Erik Reinert: So that's coming from the root module.

[00:00:19]
>> Male: Right.
>> Erik Reinert: Right, so let me do this really quickly. Yeah, we’ll just do a quick little Excel draw here, just so that we are all on the same page. So we are using like the term that I really want you to keep in your mind here is just composability, like composing, right?

[00:00:39]
So we first, at the lowest level, have our main.tf, which is our root module, right? This is like you as a developer, this is more than likely what you'll be interacting with.
>> Male: Now, underneath this we have an environment directory with a main TF inside of it.
>> Erik Reinert: This is our environment module.

[00:01:09]
And what happens is the main TF here uses this module and imports it. Then we say, okay, now this has multiple modules in it that are in that main.tf file. So this would be like database, and then this would be network. And so what happens is, in each one of these modules you also can have a variables.tf file, right?

[00:01:44]
What we do is we say, okay, well, what are the variables at the lowest level that we need? Then we basically bubble those up to the top modules and make those variables. For example, if we go back to the environment one here, you'll see that I say availability zones, bastion, ingress, cidr and name for network.

[00:02:08]
Then I say security group, subnets name and VPC name for database. That's because these settings are only specific to configuring an environment. That's it. They're not specific to just configuring a database or just configuring a network. These should be settings that if you wanted to change which network the database or which subnet the database was in, or which VPC this database was in, you could do that in an environment level where everything else still stays.

[00:02:39]
But when we go lower, like, say again, when we go into an actual database, that's when you're actually looking at it from the perspective of the resource. And like, okay, well what are the variables for this? You really would either A hard code these, or B make variables that have defaults.

[00:02:59]
And then you say, okay, here's the things that you care about, and then here's the other things that you may want to use or maybe not want to use. But the main point is to kind of like look at them as small units that we're kind of building on top of each other.

[00:03:15]
And then if we need a variable that goes all the way down to that lower unit, we make sure that a variable is created in the one above it. The variables.tf inside the folder, is that like a convention that Terraform is aware of and then can reference those variables magically type of thing, or is, guess, how do we follow the thread on that?

[00:03:41]
I got you. So we'll start at the top here in the main.tf, right? So, this is the root. This is current. If I open up this, you'll see that I declare source for my module and I say module environment. The thing to note here is that we're looking at a directory.

[00:03:59]
So every module is a directory. If the directory has a variables.tf file, or, I’m sorry, no, not if it has a variables.tf file, but if it has a file with variables in it, it will expose those variables to the module right here, right? So to be clear, there's a lot of.

[00:04:25]
When people started working with Terraform way back in the day, these concepts were hard to teach, which I went through the same thing. I was like, what? And so what we did as a community is we just started naming files like variables TF and Locals tf. But the real reality of it is that you don't have to name any of those files.

[00:04:49]
That it's just as long as the file contains an output, right? And then it reads the entire directory recursively for any of those things. So, yeah, once I went into module environment and I declared some outputs or some variables, right? In this case, variables, that made it so that when Terraform read that directory, it just like recursively inside of it, looked for everything.

[00:05:14]
It found some outputs and now they became available as a parameter when I reuse it other places. Yeah, so you could put this all in main tf. Absolutely. Just lose track of it all. Yes, exactly. Yeah, and what's funny is if you go to the Terraform modules that I've been showing you, they do that, which really is annoying to me because when I'm trying to decipher what is what, you see these huge things.

[00:05:46]
But for example, if we go here really quick, you'll see they only have a main tf. So if we go in here, everything is in here, all the resources and everything are just in the main tf. That's really also what I mean by composability. It's up to you how you want to compose it.

[00:06:05]
But you can compose it any way that you want. The value really of using modules is, in my opinion, to abstract away the need to know what you're provisioning and only care about the settings that you need for it. Another thing I think as well for me is making standardizations that are reusable, like naming conventions, things like that, so that I don't have to worry about did it get named the right thing or blah blah, blah, whatever.

[00:06:41]
I kind of just build those practices into the module themselves.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now