Integrate Terraform Language Server Protocol With vim
Tags: vim, terraform, hashicorp, devops
Published: 2020-08-20 20:52:53

---
title: "Integrate Terraform Language Server Protocol With vim"
published: true
description: 
tags:
  - vim
  - terraform
  - hashicorp
  - DevOps
---

![VIM](https://dev-to-uploads.s3.amazonaws.com/i/qagmfm7pqy4so8o7z1mv.png)
​
We write large-scale infrastructure with terraform. We spend hours creating modules, writing resources, developing infrastructure. To feel more comfortable, surely we need a langserver in our editor. I'll show how to integrate a terraform langserver in vim - making it a Swiss Army Knife.

The Language Server protocol is used between a tool (the client) and a language smartness provider (the server) to integrate features. Like auto complete, go to definition, find all references and alike into the tool. This post explains how to integrate **terraform-lsp** on vim.


### Requirements 
- Go >= 1.14
- nodejs >= 10.12
- vim >= 8.0

### Install coc-nvim With vim-plug

```bash

" Use release branch (recommend)
Plug 'neoclide/coc.nvim', {'branch': 'release'}

" Or build from source code by using yarn: https://yarnpkg.com
Plug 'neoclide/coc.nvim', {'do': 'yarn install --frozen-lockfile'}

```
In .vimrc or init.vim, then restart Vim and run :PlugInstall.

### Download terraform-lsp

Download a [RELEASE](https://github.com/juliosueiras/terraform-lsp/releases) from github, extract file and move binary to /usr/local/sbin/

In this example I use Release `v0.0.11-beta2`

Download file with wget 

```bash
wget https://github.com/juliosueiras/terraform-lsp/releases/download/v0.0.11-beta2/terraform-lsp_0.0.11-beta2_linux_amd64.tar.gz
```


Extract file with tar
```bash
tar -xvf terraform-lsp_0.0.11-beta2_linux_amd64.tar.gz
```


Move file **terraform-lsp** to `/usr/local/bin/`
```bash
sudo mv terraform-lsp /usr/local/bin/
```

### Create coc-nvim Config
Add snippet to `coc-setting.json` find file on ~/.vim/coc-setting.json or open file with `:CocConfig`


```json
{
	"languageserver": {
		"terraform": {
			"command": "terraform-lsp",
			"filetypes": [
				"terraform",
				"tf"
			],
			"initializationOptions": {},
			"settings": {}
		}
	}
}
```

Now create new terraform files and resources to check autocomplete, attributes and more.

### Usage:
​List resources available:
​​![Resources](https://dev-to-uploads.s3.amazonaws.com/i/a642r2djzou3cylx1o83.png)
![](/images/list-resources.png)
​
List Attributes available:
![Attibutes](https://dev-to-uploads.s3.amazonaws.com/i/m36dkb2ur0t9wc0t2epe.png)

​​When an attribute is required:
​​![Error](https://dev-to-uploads.s3.amazonaws.com/i/zbrnucph8oz9kvoj4nrt.png)
![](/images/Errors.png)
​
### Notes:

- Currently there are two terraform lsp, [Terraform-lsp](https://github.com/juliosueiras/terraform-lsp) and [Terraform-ls](https://github.com/hashicorp/terraform-ls).

- Aim is a unified lsp for terraform, but for now there's two concurrent developments, terraform-lsp for experimental features and terraform-ls for stability

Original post: https://braybaut.dev/posts/integrate-terraform-language-server-protocol-with-vim/

### References:

 [Terraform-lsp](https://github.com/juliosueiras/terraform-lsp)   
 [Terraform](https://www.terraform.io/)  
 [langserver.org](https://langserver.org/)   
 [coc-nvim](https://github.com/neoclide/coc.nvim)