Blog Post

Terraform Commands

,

Photo by Element5 Digital on Unsplash

I realized I never created a post to show how to deploy Terraform from VS Code. I haven’t done that in a while because I don’t do it at work. We have Azure DevOps pipelines to handle that, but I like to test my code on the side in my personal environment because I don’t have a pipeline set up to push the code. I don’t need a pipeline in my personal environment.

Now, I feel rusty on Terraform commands and how to run them from VS Code, so I’m writing this blog post so my future self can thank me. I could look it up on someone else’s website or ask an AI, but I would rather document this for myself.

I have some other posts on Terraform here, but none of them have what I need right now: the basics of deploying Terraform from VS Code. And I kind of have it in here already, at least init, plan, apply, but I want an easier cheatsheet.

Prerequisites

The deployment happens through the Terraform CLI, which you can run inside VS Code’s terminal. And you need to have all the Terraform components for this to work. This post assumes you have Terraform installed and your VS Code ready to go.

Most Common Terraform Commands

  1. terraform init – Initialize the working directory, download providers, and set up the backend.
  2. terraform validate – Check the configuration for syntax errors.
  3. terraform fmt – Format Terraform files into standard style.
  4. terraform plan – Show what changes Terraform will make without applying them.
  5. terraform apply – Apply the planned changes to create or update resources.
  6. terraform destroy – Remove all resources defined in the configuration.
  7. terraform output – Display output values from the configuration.
  8. terraform state list – List all resources Terraform is tracking.

But I mainly use a subset of these.

Executing Terraform Commands

If you don’t see your terminal in VS Code, you can go to View –> Terminal to display it. And that opens a terminal at the bottom of the screen.

terraform init – Initialize Your Terraform Project

Before Terraform can create or manage infrastructure, your working directory needs to be prepared.
When you run terraform init, Terraform will:

  1. Set up the backend – This is where Terraform stores the state file that tracks your infrastructure.
    • By default, it’s stored locally in your project folder as terraform.tfstate.
  2. Download providers – Terraform uses providers (AWS, Azure, GCP, Kubernetes, etc.) to talk to different services.
    • It looks at your .tf files, sees which providers you declared, and downloads the correct versions from the Terraform Registry.
  3. Install modules – If your configuration uses modules (reusable Terraform code), terraform init will fetch them.

Important: You must run terraform init at least once before terraform plan or terraform apply.
Also, if you change provider versions or backend settings, you should run it again to refresh everything.

Here’s an example of the initialization output. Your results vary depending on what you are using and whether you initialized before.

terraform plan – Preview What Changes Terraform Will Make

The terraform plan command shows you exactly what Terraform will do before it makes any changes to your infrastructure. It compares the current state of your resources (from the state file) with your configuration files and then displays a list of additions, changes, and deletions it would make to bring your infrastructure in line with your code.

You’ll see:

  • Resources to be created (marked with a plus sign +)
  • Resources to be updated (marked with a tilde ~)
  • Resources to be destroyed (marked with a minus sign -)

Running terraform plan is a good safety step because it lets you confirm that Terraform understood your intentions before you make changes. Many people run plan before they apply to avoid surprises.

In my case, I used to always run terraform plan first, but I don’t anymore because terraform apply itself shows you the same plan and still asks for approval before making changes. This means if you trust your configuration and are comfortable skipping the extra step, you can just run terraform apply directly and approve the plan when prompted.

Before you can run terraform plan, you need to run az login (in my case, since I’m using Azure with Terraform) to authenticate to your subscription.

After you run terraform plan, it will show you things like these, but it all depends on whether you’ve changed things outside Terraform or not, and if you are adding new things. But just to give you an example of some of the results I’m seeing right now. I blew away an entire resource group, but didn’t use Terraform. I don’t recommend doing it that way, especially if you have your resources in Terraform, but I felt like doing that in my personal environment one day.

I don’t want to add all that stuff back in, so in that case, I move any Terraform files, I don’t want to be applied into a subfolder. My ignorefornow subfolder holds anything that’s broken or I don’t want to use currently. I also comment out things in my main.tf file, so they won’t try to create it if I don’t want them back in place.

But the good news is, there aren’t any errors with my plan, so I’m going to move on to apply.

terraform apply – Apply Changes to Create or Update Infrastructure

The terraform apply command is the one that makes changes to your infrastructure. One of the best things about terraform apply is that it first shows you a plan of what it will do. You’ll see a list of resources that will be added, changed, or destroyed before any action is taken, giving you a chance to review everything carefully.

I like this command the most because it combines planning and applying in one step. I like this one the best because it plans and then displays everything it’s going to add, change, or destroy. I always carefully look at the destroy in particular, but also the changes. Additions aren’t so risky.

I moved some of my tf files into the ignorefornow folder and commented out some things from my main.tf, so that I only have a few things to destroy and the one thing I want to add at this point. Now, with terraform apply, it asks you if you want to perform these actions.

I typed yes because I was ready to apply these changes, and it applied my changes successfully.

terraform destroy – Delete Infrastructure Created by Terraform

The terraform destroy command is used to completely remove resources that Terraform manages.
It reads your current state and configuration files and then deletes everything defined in your project.

This command is particularly useful when you want to clean up test environments or remove a bunch of resources at once without manually deleting each one. Because destroy can be destructive (imagine that!), Terraform will first show you a plan of what will be removed and ask for your approval before actually deleting anything.

I rarely use terraform destroy, but it’s handy if you need to start over or avoid leaving unused resources running.

I’m not going to destroy it now, but here’s what it looks like if you wanted to try.

terraform fmt – Format .tf files for Readability and Consistency

The terraform fmt command automatically formats your Terraform configuration files so they follow the standard style and conventions. It ensures that indentation, spacing, and alignment are consistent across all .tf files in your project.

I think this could be one of my new favorite Terraform commands because it’s so simple but so effective. It doesn’t change the logic of your configuration, only the formatting, but it makes a big difference in readability.

You can run it on a single file:
terraform fmt main.tf

Or on your entire project:
terraform fmt

If you have some mess in your code, like below.

But after terraform fmt in the terminal, you have this.

Terraform doesn’t have to be intimidating. By getting comfortable with these top five commands, you have what you need to get going. Start with init, use plan to preview changes, apply to make them real, destroy when you need a clean slate, and fmt to keep your code tidy. It’s not hard once you get in the swing of things.

The post Terraform Commands appeared first on sqlkitty.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating