Enabling VS Code Terraform extension code completion on Windows and Ubuntu (WSL 2)
Tags: terraform, vscode, windows, wsl2
Published: 2020-09-15 14:46:56

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 hashicorp/vscode-terraform %}

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 hashicorp/terraform-ls no-readme %}

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](https://dev-to-uploads.s3.amazonaws.com/i/m3dr1fsk9f64eor2r6z7.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/po9d3xif5jee8bns10xe.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/5v9mpggk7fzw5ffd3q6t.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/aqmobwgazks7sslse9ot.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/zwab25veujtv5tr784f0.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/7sjs6xa7tkvuwxrxxj3y.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/wzemnk4qlg07q0jq2jqi.png)

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

```json
{
    "[terraform]": {
        "editor.formatOnSave": true
    }
}
```

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