Terraform Tutorial — VS Code and the HashiCorp Extension

2 minute read

A good editor makes Terraform development faster and less error-prone. VS Code with the HashiCorp Terraform extension gives you syntax highlighting, auto-complete, hover documentation, and format-on-save for .tf files.


Installing the HashiCorp Terraform Extension

  1. Open VS Code
  2. Press Ctrl+Shift+X (or Cmd+Shift+X on macOS) to open the Extensions panel
  3. Search for “HashiCorp Terraform”
  4. Click Install on the extension published by HashiCorp

The extension ID is hashicorp.terraform.


What the Extension Provides

Feature Description
Syntax highlighting HCL keywords, strings, numbers, and comments are color-coded
Auto-complete Type resource "aws_ and see suggestions for all AWS resource types
Hover documentation Hover over a resource type or argument to see docs inline
Go to definition Ctrl+Click on a variable reference to jump to its declaration
Format on save Automatically runs terraform fmt when you save a .tf file
Validation Red underlines for syntax errors and invalid references

Configuring Format on Save

Add these settings to your VS Code settings.json:

{
  "[terraform]": {
    "editor.defaultFormatter": "hashicorp.terraform",
    "editor.formatOnSave": true,
    "editor.formatOnSaveMode": "file"
  },
  "[terraform-vars]": {
    "editor.defaultFormatter": "hashicorp.terraform",
    "editor.formatOnSave": true,
    "editor.formatOnSaveMode": "file"
  }
}

To open settings.json: press Ctrl+Shift+P → type “Preferences: Open User Settings (JSON)”.

With format-on-save enabled, every time you save a .tf file, the extension runs terraform fmt automatically. This ensures consistent formatting across your team.


Useful VS Code Shortcuts for HCL

Shortcut Action
Ctrl+Space Trigger auto-complete manually
Ctrl+Shift+P Open command palette
Ctrl+Click Go to definition
Ctrl+/ Toggle line comment (#)
Ctrl+Shift+/ Toggle block comment (/* */)
Ctrl+D Select next occurrence of current word
Alt+Up/Down Move a line up or down

Alternative Editors

VS Code isn’t the only option. Here are alternatives with good Terraform support:

IntelliJ IDEA / GoLand

The Terraform and HCL plugin by JetBrains provides auto-complete, refactoring, and navigation. Good choice if you’re already a JetBrains user.

Vim / Neovim

Use hashicorp/terraform-ls as an LSP server with your preferred LSP client (e.g., nvim-lspconfig):

-- Neovim LSP config
require('lspconfig').terraformls.setup{}

Combine with vim-terraform for syntax highlighting and :TerraformFmt command.

Emacs

The terraform-mode package provides syntax highlighting. Combine with lsp-mode and terraform-ls for auto-complete.


Beyond the HashiCorp extension, these are useful for Terraform development:

Extension Purpose
HashiCorp Terraform Core HCL support
Terraform doc snippets Quick-access documentation snippets
GitLens See who changed each line (useful for Terraform code review)
YAML (Red Hat) Helpful when working with Ansible alongside Terraform
Remote - SSH Edit files on remote machines where Terraform runs

What’s Next?

Your editor is set up. Now let’s establish the standard directory layout for a Terraform project — where to put each file and what to include in .gitignore.


← Setting Up Cloud Credentials Project Directory Layout →