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
- Windows:
🧩 Installing the HashiCorp Terraform Extension
Once VS Code is installed:
- Open VS Code and go to the Extensions tab (
Ctrl+Shift+XorCmd+Shift+X). - Search for “HashiCorp Terraform”.
- 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:
| Extension | Purpose |
|---|---|
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.