Install the Extension
- Open VSCode
- Press
Ctrl+Shift+X(Windows/Linux) orCmd+Shift+X(macOS) - Search for “HashiCorp Terraform”
- Click Install
Or install from the command line:
code --install-extension hashicorp.terraform

The extension ID is hashicorp.terraform. It’s the official extension maintained by HashiCorp.
What You Get
| Feature | Description |
|---|---|
| Syntax highlighting | Color-coded .tf and .tfvars files |
| Autocomplete | Resource types, attributes, functions, variables |
| Go to definition | Ctrl+Click on a variable or module to jump to its definition |
| Hover documentation | Hover over a resource type to see docs |
| Format on save | Auto-runs terraform fmt when you save |
| Validation | Red squiggles for syntax errors |
| Code lens | Reference counts above variables and outputs |
| Module support | Autocomplete works inside modules |
Configure Format on Save
The most useful setting. Add to your VSCode settings.json:
{
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
},
"[terraform-vars]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
}
}
Now every time you save a .tf file, it automatically runs terraform fmt — consistent formatting without thinking about it.
Open settings.json
Ctrl+Shift+P→ “Preferences: Open User Settings (JSON)”- Or: File → Preferences → Settings → click the
{}icon (top right)
Enable Validation
The extension validates your Terraform code in real-time. Make sure validation is enabled:
{
"terraform.validation.enableEnhancedValidation": true
}
This catches errors as you type — before you run terraform plan.
All Useful Settings
{
// Format .tf files on save
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
},
"[terraform-vars]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
},
// Enable enhanced validation
"terraform.validation.enableEnhancedValidation": true,
// Enable code lens (reference counts)
"terraform.codelens.referenceCount": true,
// Path to terraform binary (if not in PATH)
// "terraform.languageServer.path": "/usr/local/bin/terraform",
// Enable experiment features
"terraform.experimentalFeatures.validateOnSave": true,
"terraform.experimentalFeatures.prefillRequiredFields": true
}
Keyboard Shortcuts
| Shortcut | Action |
|---|---|
Ctrl+Space | Trigger autocomplete |
Ctrl+Click | Go to definition |
F12 | Go to definition |
Alt+F12 | Peek definition (inline) |
Shift+F12 | Find all references |
Ctrl+Shift+F | Format document |
Ctrl+` | Open integrated terminal |
Ctrl+Shift+P | Command palette |
Using the Integrated Terminal
Run Terraform commands directly in VSCode:
- Open terminal:
Ctrl+` - Run commands:
terraform init
terraform plan
terraform apply

Tasks (Optional)
Create .vscode/tasks.json for one-click Terraform commands:
{
"version": "2.0.0",
"tasks": [
{
"label": "Terraform Init",
"type": "shell",
"command": "terraform init",
"group": "build"
},
{
"label": "Terraform Plan",
"type": "shell",
"command": "terraform plan",
"group": "build"
},
{
"label": "Terraform Apply",
"type": "shell",
"command": "terraform apply -auto-approve",
"group": "build"
},
{
"label": "Terraform Validate",
"type": "shell",
"command": "terraform validate",
"group": "test"
}
]
}
Run tasks: Ctrl+Shift+P → “Tasks: Run Task” → select task.
Recommended Companion Extensions
| Extension | ID | Purpose |
|---|---|---|
| Terraform doc snippets | run-at-scale.terraform-doc-snippets | Snippet templates |
| TFLint | megnitas.tflint | Linting integration |
| GitLens | eamodio.gitlens | Git blame and history |
| YAML | redhat.vscode-yaml | For Ansible/CI files |
| Remote - SSH | ms-vscode-remote.remote-ssh | Edit files on remote servers |
| Error Lens | usernamehw.errorlens | Inline error highlighting |
Install all at once:
code --install-extension hashicorp.terraform
code --install-extension run-at-scale.terraform-doc-snippets
code --install-extension eamodio.gitlens
code --install-extension usernamehw.errorlens
Autocomplete in Action
The extension provides autocomplete for:
Resource types:
resource "aws_ # Shows: aws_instance, aws_s3_bucket, aws_vpc, ...
Resource attributes:
resource "aws_instance" "web" {
inst # Shows: instance_type
ami # Shows: ami
}
Built-in functions:
locals {
result = leng # Shows: length(), lower(), lookup(), ...
}
Variable references:
instance_type = var. # Shows all declared variables
Module outputs:
subnet_id = module.vpc. # Shows all outputs from the vpc module
Troubleshooting
Autocomplete not working
- Run
terraform initin the project directory first - Check the language server is running: look for “Terraform LS” in the status bar
- Restart the language server:
Ctrl+Shift+P→ “Terraform: Enable Language Server”
Format on save not working
- Make sure
hashicorp.terraformis set as the default formatter (see settings above) - Check that
terraformbinary is in your PATH:which terraform - Try formatting manually:
Ctrl+Shift+P→ “Format Document”
Extension conflicts
If you have the older mauve.terraform extension installed, remove it — it conflicts with the official HashiCorp extension.
code --uninstall-extension mauve.terraform
High CPU usage
The language server can be CPU-intensive on large projects. If needed:
{
"terraform.languageServer.indexing.ignoreDirectoryNames": [
".terraform",
"node_modules"
]
}
Hands-On Courses
Learn by doing with interactive courses on CopyPasteLearn:
Conclusion
Install the HashiCorp Terraform extension (hashicorp.terraform), enable format on save and enhanced validation, and you have a production-ready Terraform IDE. Autocomplete covers resource types, attributes, functions, and module outputs. Add TFLint and GitLens for a complete development environment.




