VS Code Setup for Terraform
Introduction
One of the first questions I get when teaching Terraform is, “What editor should
I use?” My answer is always, “Use what you are comfortable with. If you don’t
have a preference, I recommend Visual Studio Code.” Over the years, I have used
everything from vi
to Notepad++
to Sublime Text
to Atom
to
Visual Studio Code
. Visual Studio Code is easy to pick up for new users and
easy to customize to your liking.
I am not a developer by trade; I’m more of a hack-it-together person. I copy a lot of code from the internet or use GenAI and modify it to fit my needs. So, I only work in an IDE sometimes. I need something easy to use and easy to customize.
TL;DR
For those of you like me who want the quick and dirty, here are the steps to set up your VS Code for Terraform development:
- Install Visual Studio Code - Download
- Create a profile for Terraform - Profiles
- Clone my Terraform template repository - Terraform Template
- Open the repository in VS Code - This will prompt you to install the recommended extensions and import the settings.
- Go forth and Terraform!
Visual Studio Code Profiles
One of the reasons I like Visual Studio Code is that it supports profiles. I can create a profile for each type of work I do. I have profiles for Terraform, Python, Ansible, and Markdown. Each profile has its settings, extensions, and keybindings. By using profiles, I can tailor the editor to the work I am doing.
Creating a Profile
To create a profile, click on the gear
icon in the lower-left corner of the VS
Code window. Click on the Profiles
option in the’ Settings’ window.
In the Profiles tab, you can see your current profiles and create new ones. To
create a new profile, click the New Profile
button.
This creates a new Untitled
profile.
You can now name your profile by editing the name field. You also have the option to copy the settings from an existing profile. This is useful if you want to create a new profile similar to an existing one or just import your default settings.
VS Code Extensions for Terraform
I am a bit of an extension collector. I have a lot of extensions, but some don’t play well together, so I use profiles to help limit the number of extensions I have enabled at any one time.
I use four extensions for Terraform development in Visual Studio Code:
You’re probably looking at the list: “Why do you need a case preserver?” How many times have you decided to change the name of a variable and then had to go back and change the case of the variable in multiple places? This extension will preserve the case of the variable when you change it. It’s a small thing, but it saves me a lot of time.
VS Code Settings for Terraform
I like to use a few settings when working with Terraform. You can use the VS
Code UI to set these, but sharing them in a JSON
file in the .vscode.
directory of your working directory is easy.
Here are my Terraform-specific VS Code settings:
{
"[sentinel]": {
"editor.defaultFormatter": "hashicorp.terraform"
},
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform"
},
"[tfvars]": {
"editor.defaultFormatter": "hashicorp.terraform"
},
"editor.bracketPairColorization.enabled": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.guides.bracketPairs": "active",
"editor.inlineSuggest.enabled": true,
"editor.linkedEditing": true,
"editor.multiCursorModifier": "alt",
"editor.renderControlCharacters": true,
"editor.renderWhitespace": "all",
"editor.rulers": [
{
"color": "#A5FF90",
"column": 80
},
{
"color": "#FF628C",
"column": 100
}
],
"editor.stickyScroll.enabled": true,
"editor.suggestSelection": "first",
"editor.tabCompletion": "on",
"editor.tabSize": 2,
"files.associations": {
"*.hcl": "terraform",
"*.nomad": "terraform",
"*.policy": "sentinel",
"*.sh.tmpl": "shellscript"
},
"files.trimTrailingWhitespace": true,
"terraform.indexing": {
"delay": 500,
"enabled": false,
"exclude": [".terraform/**/*", "**/.terraform/**/*"],
"liveIndexing": false
},
"terraform.languageServer.enable": true
}
Not all of these settings are needed, but they are the ones I like to use. You can adjust them to fit your needs.
editor.defaultFormatter
- This sets the default formatter for the editor. I like to use the HashiCorp Terraform formatter for all of my Terraform,tfvars
, and Sentinel policy files.editor.bracketPairColorization.enabled
- This will colorize the brackets in your code to make it easier to see where they start and end.editor.formatOnPaste
,editor.formatOnSave
,editor.formatOnType
- These settings will format your code when you paste it, save the file, or type in the editor.editor.guides.bracketPairs
- This will highlight the bracket pair you work within.editor.inlineSuggest.enabled
- This will enable inline suggestions in the editor.editor.linkedEditing
- This will link the editing of a variable name to all instances of that variable in the file.editor.multiCursorModifier
- This sets the keybinding for creating multiple cursors.editor.renderControlCharacters
- This will render control characters in the editor.editor.renderWhitespace
- This will render whitespace in the editor.editor.rulers
- This will add rulers to the editor in specified columns. I don’t like to have lines that are too long, so I set my rulers at 80 and 100 columns.editor.stickyScroll.enabled
- This will enable sticky scrolling so that the block header stays at the editor’s top as you scroll through the code section.editor.suggestSelection
- This sets the default suggestion selection to the first suggestion.editor.tabCompletion
- This will enable tab completion in the editor.editor.tabSize
- This sets the tab size to 2 spaces.files.associations
- This will associate the specified file extensions with the specified language. I like to use the Terraform language server for all of my Terraform,nomad
, and Sentinel policy files. I also work with a lot of shell script templates that get processed by Terraform, so I associate the.sh.tmpl
files with theshellscript
language.files.trimTrailingWhitespace
- This will trim trailing whitespace from the end of lines.terraform.indexing
will set the indexing options for the Terraform configs we are working with.terraform.languageServer.enable
will enable the Terraform language server.
Conclusion
I hope this helps you get started with Terraform development in Visual Studio.