Home Infrastructure as Code (IaC) Setting Up Visual Studio Code for Terraform

Setting Up Visual Studio Code for Terraform

371
0

Visual Studio Code (VS Code) is one of the most popular editors for Infrastructure as Code (IaC).
It’s lightweight, cross-platform, and has excellent support for Terraform through extensions, formatting, and linting tools.

This guide helps you set up VS Code for Terraform development on Windows, macOS, or Linux — without needing Azure or any cloud configuration yet.


💻 Installing Visual Studio Code

  • Download and install Visual Studio Code for your operating system.
  • Alternatively, use your system’s package manager:
    • Windows: winget install Microsoft.VisualStudioCode
    • macOS: brew install --cask visual-studio-code
    • Ubuntu: sudo snap install code --classic

🧩 Installing the HashiCorp Terraform Extension

Once VS Code is installed:

  1. Open VS Code and go to the Extensions tab (Ctrl+Shift+X or Cmd+Shift+X).
  2. Search for “HashiCorp Terraform”.
  3. Click Install.

This extension provides syntax highlighting, IntelliSense, code completion, and validation for .tf files.


⚙️ Recommended Additional Extensions

Enhance your Terraform workflow with these useful VS Code extensions:

ExtensionPurpose
Prettier – Code Formatter (Identity: esbenp.prettier-vscode)Consistent auto-formatting for all files.
Error Lens (Identity: usernamehw.errorlens)Shows validation and syntax issues inline.
WSL (Identity: ms-vscode-remote.remote-wsl)Only for WSL users – Lets Windows users run VS Code directly inside their Linux environment using WSL.
GitLens (Identity: eamodio.gitlens)Optional – Helps track changes and version control for Terraform code.
Markdown All in One (Identity: yzhang.markdown-all-in-one)Optional – Great for writing module or project documentation.
Remote – SSH (Identity: ms-vscode-remote.remote-ssh)Optional – Enables secure connection to remote Linux servers for Terraform or other development work.
Dev Containers (Identity: ms-vscode-remote.remote-containers)Optional – Allows development inside isolated, containerized environments — useful for advanced setups.

💡 Integrating the VS Code Terminal

Use VS Code’s integrated terminal (`Ctrl+“) to run Terraform commands directly inside your editor.

💡 Note for Windows WSL users:
If you’re using WSL, select WSL as your terminal instead of PowerShell or Bash. You can do this either from the menu — Terminal → Select Default Profile → WSL: Ubuntu — or directly from the dropdown on the right side of the terminal panel.


🧪 Testing & Validation (Local Only)

Creating the project folder

We can test our Terraform setup locally without connecting to Azure.
Create a new project folder called terraform-local-demo and open that folder in VS Code.
You can also create the folder directly from within VS Code using the File → New Folder option.

💡 Note for Windows users:
If you’re on Windows, open PowerShell or Command Prompt in your project folder and type:

code .

This command opens the current folder directly in Visual Studio Code.

If you’ve installed the WSL extension (ms-vscode-remote.remote-wsl), you can also open the folder from your Linux environment by typing the same command inside your WSL terminal. This launches VS Code connected to your WSL workspace.

Creating the test file

Within our project folder, create a simple main.tf file inside it.

terraform {
  required_providers {
    null = {
      source = "hashicorp/null"
    }
  }
}

provider "null" {}

resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "echo Hello, Terraform!"
  }
}

Testing from the VS Code Terminal

Use VS Code’s integrated terminal (`Ctrl+“) to run Terraform commands directly inside your editor. If you’re using WSL, select WSL as your terminal instead of PowerShell or Bash. You can do this either from the menu — Terminal → Select Default Profile → WSL: Ubuntu — or directly from the dropdown on the right side of the terminal panel.

Then, in VS Code’s terminal: run the following commands one at a time, reviewing the output after each:

terraform init

Note: Terraform init initializes the working directory and downloads required providers.

terraform validate

Note: Terraform validate our configuration for syntax and logical errors.

terraform plan

Note: Terraform plan reviews the actions Terraform will perform before applying them.

terraform apply

Note: Terraform apply applies the configuration and executes the defined actions.
When prompted, type yes to proceed.


If you see “Hello, Terraform!”, your VS Code and Terraform setup are working perfectly 🎉.


🧱 Configuring Terraform Linting and Formatting

Terraform comes with built-in commands to check and format your code.

Enable auto-format on save in VS Code:
Go to Settings → Text Editor → Formatting → Format on Save, and turn it on.

You can also manually check your Terraform file formatting from the integrated terminal:

terraform fmt -check

You can then validate your Terraform configuration to ensure it’s syntactically correct:

terraform validate

This ensures your code is properly formatted and valid before any deployment.

💡 Tip: You can combine both commands in one line if you prefer:

terraform fmt -check && terraform validate

This ensures your code is properly formatted and syntactically valid before any deployment.


✅ Summary

By now, you’ve:

  • Installed VS Code and Terraform extensions
  • Configured formatting and validation
  • Tested Terraform locally (no cloud setup required)
  • Optionally integrated VS Code with WSL for Linux-style workflows

You’re ready to move on to the next section — ☁️ Terraform Azure Integration Fundamentals,
where you’ll connect Terraform with Azure and begin real-world cloud deployments.

Previous articleUnderstanding Files and Directories Created by Terraform Commands
Next articleUnderstanding the Terraform Null Provider
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here