Check out a free preview of the full Enterprise DevOps & Cloud Infrastructure course:
The "Using the Terraform CLI" Lesson is part of the full, Enterprise DevOps & Cloud Infrastructure course featured in this preview video. Here's what you'd learn in this lesson:

Erik uses the Terraform CLI to initialize the repo. The validate, plan, and apply commands are also introduced.

Get Unlimited Access Now

Transcript from the "Using the Terraform CLI" Lesson

[00:00:00]
>> So again, we've already set up our CLI tools, right? We've already got Terraform installed, and we've already got Doppler installed and everything's set up, right? So what I'm going to do now is, I'm gonna do a terraform init. And I want you to note that I'm doing terraform init without my Doppler command, right?

[00:00:14] Because as it is right now, I'm not talking to terraform cloud really whatsoever. So I'm gonna do terraform init, and then I'm gonna do terraform validate. These two commands are really going to be the two commands you start with when you create terraform automation. You want to initialize after you've created something, so it can download all of the module or all the modules you're using and all the providers you're using.

[00:00:36] And so if you look at my repo, you'll see that I have a .terraform directory, and inside of that I now have modules and providers. That's because Terraform just downloads code at the end of the day. These are the actual repos, it's even got my flake files for Nicks and stuff like that in here, just because it's the literal repository, right?

[00:00:54] Same thing for the provider. If we go all the way to the end, that's the actual 20 megabyte binary for terraform, tfe provider. So it literally just goes out and make sure that it downloads everything for you. That's what init does, right? Init, right? Validate will actually parse your code.

[00:01:13] Now, it will not run your code against the actual infrastructure, so it's not gonna tell you if it'll work from a API level perspective, but from a syntax perspective, it will in fact tell you what's working or what's not, right? So again, if we do validate right, my configuration is valid, so that's great.

[00:01:33] Alright, so now what we want to do is we actually want to apply this against Terraform Cloud, right? So in this scenario, we actually want to use our Doppler command here. And the reason why we're using our Doppler command, because we injected the TFE token into our configuration, right?

[00:01:51] In Doppler. When I run Terraform Commands. Now, it will actually be able to connect the Terraform Cloud. So I'm gonna do terraform plan, and I'm gonna do -out note, a single dash here, not a double dash, single dash and then I'm gonna do Terraform plan. Now, you see when I run my plan, I get two new resources.

[00:02:12] The first is my project, fem-ci-project under my organization, right, and then my workspace as well. And you can see my execution mode's been set. You can see my name's been set, organization. All that good stuff. So at this point, this plan looks good we don't have any errors or any problems like that.

[00:02:32] And so now if I wanted to, I could actually apply this. So to apply it, we really just go up, go to the right or go to the left, sorry, delete everything besides Terraform and the actual file name and put apply in between Terraform and apply. So the idea here is is that we initialized, right?

[00:02:52] We validated, right? We planned and now we're actually going to apply. That cycle is really the whole cycle you're going to be doing in TerraForm for the rest of your experience with it. Again, initialise, validate, plan and apply. So let's go ahead and apply. There we go. So if I go back to Terraform Cloud, refresh my page.

[00:03:28] There we go, you can see I have a brand-new workspace, and then underneath it, you can see I have a project. Now, if I wanted to see that project I could click this and just like before. Now, I can actually select just any projects or any workspaces related to my project that I created, right?

[00:03:46] Now, we have a bit of organization which is really nice. The reason why we do this as well is because in the future, you're probably going to have a lot of workspaces. So this will make it easier for you to navigate when you're like what was that one specific component that I need access to the really only other option you can do is searching workspaces and I'll be honest it's not always helpful when like nine of them are named the same thing with like one environment variable difference or something like that So, I really do recommend using projects, they're super helpful, they're super easy to navigate with, and honestly, I wish we used them at my work, because we don't.

[00:04:18] So that's why I'm telling you to use them. Do as I say, not as I do.