DEV Community


Enabling VS Code Terraform extension code completion on Windows and Ubuntu (WSL 2)

shibayan profile image Tatsuro Shibamura Updated on ・4 min read

As I'm writing more and more Terraform definitions, I've decided to make Terraform definition input completion work for a more comfortable environment. I know it's a bit late, but I've been writing definitions without using code completion.

Bicep the DSL of ARM Template, seems to be attracting some interest. But the hard part of ARM Template is still intact from the sample, and the fact that it makes me aware of the API version hasn't changed, so I will continue to use Terraform in the future.

Getting back to Terraform, I'm sure everyone who writes Terraform definitions in Visual Studio Code has included this extension. It's an extension that became official Hashicorp a while ago.

GitHub logo hashicorp / vscode-terraform

A Visual Studio Code extension for Hashicorp Terraform

Terraform Visual Studio Code Extension

HashiCorp Terraform

The HashiCorp Terraform Visual Studio Code (VS Code) extension adds syntax highlighting and other editing features for Terraform files using the Terraform Language Server.


  • Manages installation and updates of the Terraform Language Server (terraform-ls), exposing its features
    • Completion of initialized providers: resource names, data source names, attribute names
    • Diagnostics to indicate HCL errors as you type
    • Initialize the configuration using "Terraform: init" from the command palette
    • Run terraform plan and terraform apply from the command palette
    • Validation diagnostics using "Terraform: validate" from the command palette or a validateOnSave setting
  • Includes syntax highlighting for .tf and .tfvars files -- including all syntax changes new to Terraform 0.12
  • Closes braces and quotes
  • Includes for_each and variable syntax shortcuts (fore, vare, varm)

Getting Started

IMPORTANT: After installing, you must perform a terraform init to provide terraform-ls with an up-to-date provider schemas…

Language Server is distributed in a separate repository. But Terraform extension will install and update it automatically, so you don't have to worry about it except for checking Changelog.

GitHub logo hashicorp / terraform-ls

Terraform Language Server

Basically I've been writing Terraform definitions in VS Code on Windows 10, but I've left some unfamiliar errors appearing, and I felt it was time to fix them properly.

I've tried to see if it works on Ubuntu on WSL 2 as well, and here are the steps.

Using on Windows 10

The official tutorial shows how to use Chocolatey to install Terraform on Windows, but I don't like Chocolatey personally so I decided to manage it manually.

I create a directory under the user profile and pass the PATH to it. Scoop looks good, but I don't really like it either.

Install Terraform CLI

If winget is included by default and Terraform is added, I would consider moving to it, but the chaos around the Windows package manager is so chaotic that it will take some time to get a definitive version.

If the PATH to the Terraform CLI has been passed, you should be able to open VS Code and not get any errors.

However, if you open VS Code without executing terraform init, you will get the following warning, so run it in the appropriate place to download and initialize the required Provider.

Getting terraform not initializing error

After executing the command, I had to restart VS Code to reflect it. I quietly restarted VS Code.

After I restarted it, the familiar list of candidates is now displayed when I type a letter or press Ctrl+Space, as shown below. It's nice to see the types displayed as well.

Terraform code completion

Code completion does not work for some parameters such as ip_restriction, but I think there is a problem with the schema definition on the Provider side.

The code completion for Resource is also working, so you can find what you need in the list of candidates by putting a name like that.

If you are using Visual Studio, this is a function that is familiar to you.

Resource name completion

There are still some limitations, but you want to be able to use it exactly as it will be a huge productivity boost.

Using on Ubuntu (WSL 2)

Recently, many people write code using VS Code and WSL 2, so I checked to see if Terraform completion works.

First we install the Terraform CLI using apt-get, but you can copy and paste the commands in the tutorial.

Once the CLI is installed, we can install the Terraform extension to VS Code, which can be installed when VS Code is running with a WSL 2 connection.

Install Extension to WSL 2

After that, I just run terraform init in the right place to download and initialize the Provider, restart VS Code, and then I can use input completion. It was a very simple procedure.

Terraform code completion on WSL 2

At a glance, it looks the same as the one running on Windows 10, but it is running on Ubuntu on WSL 2. You can see in the title bar that it's on WSL 2.

If you check the process, you can see that the Language Server is running on WSL 2.

Running Language server on WSL 2

It's a great sense of mystery and technology, but I'm thinking now that I can mix Windows and Ubuntu in VS Code without worrying too much about the differences, which is great.

Extra: Formatting when saving

In the old days you could quickly turn it on from the settings screen, but now the Terraform extension requires you to manually add the following setting to .vscode/settings.json.

    "[terraform]": {
        "editor.formatOnSave": true
Enter fullscreen mode Exit fullscreen mode

This makes it more comfortable because it automatically corrects indentation and other formatting when it's time to save.

Discussion (1)

Editor guide
brettcurtis profile image
Brett Curtis

Love WSL2 with vscode and Terraform extension, hate having to init each folder tho 😂 especially when you have many folders for testing.