DEV Community

Tatsuro Shibamura
Tatsuro Shibamura

Posted on • Updated on

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

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

HashiCorp Terraform VSCode extension

Terraform Extension for Visual Studio Code

The HashiCorp Terraform Extension for Visual Studio Code (VS Code) with the Terraform Language Server adds editing features for Terraform 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

  • Step 2: Install the Terraform Extension for VS Code.

  • 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

See Usage for more detailed getting started information.

Read the Troubleshooting Guide for answers to common questions.

Features

  • IntelliSense Edit your code with auto-completion of providers, resource names, data sources, attributes and more
  • Syntax validation Diagnostics…

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.

https://learn.hashicorp.com/tutorials/terraform/install-cli

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.

Latest comments (1)

Collapse
 
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.