TerraformPilot

Software Development

Terraform VSCode Extension: Syntax Highlighting, Autocomplete, and Formatting

Install and configure the HashiCorp Terraform VSCode extension. Enable syntax highlighting, autocomplete, format on save, validation

LLuca Berton3 min read

Install the Extension

#
  1. Open VSCode
  2. Press Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (macOS)
  3. Search for "HashiCorp Terraform"
  4. Click Install

Or install from the command line:

code --install-extension hashicorp.terraform

Terraform extension

The extension ID is hashicorp.terraform. It's the official extension maintained by HashiCorp.

What You Get

#
FeatureDescription
Syntax highlightingColor-coded .tf and .tfvars files
AutocompleteResource types, attributes, functions, variables
Go to definitionCtrl+Click on a variable or module to jump to its definition
Hover documentationHover over a resource type to see docs
Format on saveAuto-runs terraform fmt when you save
ValidationRed squiggles for syntax errors
Code lensReference counts above variables and outputs
Module supportAutocomplete 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

#
ShortcutAction
Ctrl+SpaceTrigger autocomplete
Ctrl+ClickGo to definition
F12Go to definition
Alt+F12Peek definition (inline)
Shift+F12Find all references
Ctrl+Shift+FFormat document
Ctrl+`Open integrated terminal
Ctrl+Shift+PCommand palette

Using the Integrated Terminal

#

Run Terraform commands directly in VSCode:

  1. Open terminal: Ctrl+`
  2. Run commands:
terraform init
terraform plan
terraform apply

VSCode

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.

#
ExtensionIDPurpose
Terraform doc snippetsrun-at-scale.terraform-doc-snippetsSnippet templates
TFLintmegnitas.tflintLinting integration
GitLenseamodio.gitlensGit blame and history
YAMLredhat.vscode-yamlFor Ansible/CI files
Remote - SSHms-vscode-remote.remote-sshEdit files on remote servers
Error Lensusernamehw.errorlensInline 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

#
  1. Run terraform init in the project directory first
  2. Check the language server is running: look for "Terraform LS" in the status bar
  3. Restart the language server: Ctrl+Shift+P → "Terraform: Enable Language Server"

Format on save not working

#
  1. Make sure hashicorp.terraform is set as the default formatter (see settings above)
  2. Check that terraform binary is in your PATH: which terraform
  3. 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.

#Terraform#HashiCorp#DevOps#Infrastructure as Code#Visual Studio Code

Share this article