VIM

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


" 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 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

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

tar -xvf terraform-lsp_0.0.11-beta2_linux_amd64.tar.gz

Move file terraform-lsp to /usr/local/bin/

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

{
	"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:

List attributes available:

When attribute is required:

Notes:

  • Currently there are two terraform lsp, Terraform-lsp and 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

References:

Terraform-lsp
Terraform
langserver.org
coc-nvim