# Terraform Extension for Visual Studio Code
The HashiCorp [Terraform Extension for Visual Studio Code (VS Code)](https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform) with the [Terraform Language Server](https://github.com/hashicorp/terraform-ls) adds editing features for [Terraform](https://www.terraform.io) files such as syntax highlighting, IntelliSense, code navigation, code formatting, module explorer and much more!
## Quick Start
Get started writing Terraform configurations with VS Code in three steps:
- **Step 1:** If you haven't done so already, install [Terraform](https://www.terraform.io/downloads)
- **Step 2:** Install the [Terraform Extension for VS Code](https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform).
- **Step 3:** To activate the extension, open any folder or VS Code workspace containing Terraform files. Once activated, the Terraform language indicator will appear in the bottom right corner of the window.
New to Terraform? Read the [Terraform Learning guides](https://learn.hashicorp.com/terraform)
See [Usage](#usage) for more detailed getting started information.
Read the [Troubleshooting Guide](#troubleshooting) for answers to common questions.
## Features
- [IntelliSense](#intellisense-and-autocomplete) Edit your code with auto-completion of providers, resource names, data sources, attributes and more
- [Syntax validation](#syntax-validation) Diagnostics using `terraform validate` provide inline error checking
- [Syntax highlighting](#syntax-highlighting) Highlighting syntax from Terraform 0.12 to 1.X
- [Code Navigation](#code-navigation) Navigate through your codebase with Go to Definition and Symbol support
- [Code Formatting](#code-formatting) Format your code with `terraform fmt` automatically
- [Code Snippets](#code-snippets) Shortcuts for commmon snippets like `for_each` and `variable`
- [Terraform Module Explorer](#terraform-module-and-provider-explorer) View all modules and providers referenced in the currently open document.
- [Terraform commands](#terraform-commands) Directly execute commands like `terraform init` or `terraform plan` from the VS Code Command Palette.
### IntelliSense and Autocomplete
IntelliSense is a general term for a variety of code editing features including: code completion, parameter info, quick info, and member lists. IntelliSense features are sometimes called by other names such as autcomplete, code completion, and code hinting.
For Terraform constructs like resource and data, labels, blocks and attributes are auto completed both at the root of the document and inside other blocks. This also works for Terraform modules that are installed in the workspace, attributes and other constructs are autocompleted.
> **Note:** If there are syntax errors present in the document upon opening, intellisense may not provide all completions. Please fix the errors and reload the document and intellisense will return. See [hcl-lang#57](https://github.com/hashicorp/hcl-lang/issues/57) for more information.
Invoking intellisense is performed through the [keyboard combination](https://code.visualstudio.com/docs/getstarted/keybindings) for your platform and the results depend on where the cursor is placed.
If the cursor is at the beginning of a line and no other characters are present, then a list of constructs like `data`, `provider`, `resource`, etc are shown.
![](docs/intellisense1.png)
If inside a set of quotes or inside a block, the extension provides context specific completions appropriate for the location. For example, inside a `resource` block attributes for a given provider are listed.
![](docs/intellisense2.png)
Combining `editor.suggest.preview` with the [pre-fill required fields](#code-completion) setting will provide inline snippet suggestions for blocks of code:
![](docs/intellisense3.png)
Completing the snippet allows you to tab complete through each attribute and block.
### Syntax Validation
The extension provides validation through [`terraform validate`](https://www.terraform.io/cli/commands/validate). This verifies whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It is thus primarily useful for general verification of reusable modules, including correctness of attribute names and value types.
### Syntax Highlighting
Terraform syntax highlighting recognizes language constructs from Terraform version 0.12 to 1.X. Terraform providers, modules, variables and other high-level constructs are recognized, as well as more complex code statements like `for` loops, conditional expressions, and other complex expressions.
![](docs/syntax.png)
Some language constructs will highlight differently for older versions of Terraform that are incompatible with newer ways of expressing Terraform code. In these cases we lean toward ensuring the latest version of Terraform displays correctly and do our best with older versions.
### Code Navigation
While editing, you can right-click different identifiers to take advantage of several convenient commands
- `Go to Definition` (`F12`) navigates to the code that defines the construct where your cursor is. This command is helpful when you're working with Terraform modules and variables defined in other files than the currently opened document.
- `Peek Definition` (`Alt+F12`) displays the relevant code snippet for the construct where your cursor is directly in the current editor instead of navigating to another file.
- `Go to Declaration` navigates to the place where the variable or other construct is declared.
- `Peek Declaration` displays the declaration directly inside the current editor.
### Code Formatting
This extension utilizes [`terraform fmt`](https://www.terraform.io/cli/commands/fmt) to rewrite an open document to a canonical format and style. This command applies a subset of the [Terraform language style conventions](https://www.terraform.io/language/syntax/style), along with other minor adjustments for readability.
See the [Formatting](#formatting) Configuration section for information on how to configure this feature.
### Code Snippets
The extension provides several snippets to accelerate adding Terraform code to your configuration files:
- `fore` - For Each
- `module` - Module
- `output` - Output
- `provisioner` - Provisioner
- `vare` - Empty variable
- `varm` - Map Variable
### Terraform Module and Provider Explorer
List Terraform modules used in the current open document in the Explorer Pane, or drag to the Side Bar pane for an expanded view.
Each item shows an icon indicating where the module comes from (local filesystem, git repository, or Terraform Registry).
![](docs/module_calls.png)
If the module comes from the Terraform Registry, a link to open the documentation in a browser is provided.
![](docs/module_calls_doc_link.png)
List Terraform providers used in the current open document in the Explorer Pane, or drag to the Side Bar pane for an expanded view.
![](docs/module_providers.png)
### Terraform Commands
The extension provides access to several Terraform commands through the Command Palette:
- Terraform: init
- Terraform: init current folder
- Terraform: validate
- Terraform: plan
## Requirements
The Terraform VS Code extension bundles the [Terraform Language Server](https://github.com/hashicorp/terraform-ls) and is a self-contained install.
The extension does require the following to be installed before use:
- VS Code v1.75 or greater
- Terraform v0.12 or greater
## Platform Support
The extension should work anywhere VS Code itself and Terraform 0.12 or higher is supported. Our test matrix includes the following:
- Windows Server 2019 with Terraform v1.1
- macOS 10.15 with Terraform v1.1
- Ubuntu 20.04 with Terraform v1.1
Intellisense, error checking and other language features are supported for Terraform v0.12 and greater.
Syntax highlighting targets Terraform v1.0 and greate