'How to configure alignment and indentation of terraform code inside VS Code?
I use VS Code to develop terraform code. My current plugin for terraform is:
Name: Terraform
Id: hashicorp.terraform
Description: Syntax highlighting, linting, formatting, and validation for Hashicorp's Terraform
Version: 1.4.0
Publisher: HashiCorp
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Consider the following code:
output "sql_server" {
description = "A dictionary of objects containing various Azure Sql Server properties per respective location."
value = {
for k, instance in azurerm_sql_server.instance : k =>
{
resource_group_name = instance.resource_group_name
fully_qualified_domain_name = instance.fully_qualified_domain_name
name = instance.name
location = instance.location
is_primary = instance.location == var.primary_location
admin_login = instance.administrator_login
}
}
}
I would like it to be reformatted like this when saving the file:
output "sql_server" {
description = "A dictionary of objects containing various Azure Sql Server properties per respective location."
value = {
for k, instance in azurerm_sql_server.instance : k =>
{
resource_group_name = instance.resource_group_name
fully_qualified_domain_name = instance.fully_qualified_domain_name
name = instance.name
location = instance.location
is_primary = instance.location == var.primary_location
admin_login = instance.administrator_login
}
}
}
Two things happen here:
- Indentation of 4 spaces was applied
- All the assignments within the same block were aligned
There is a lot of information on the web on how to do it, but I am must be especially stupid for being unable to make it work, so I would like to get a very concrete answer containing the following details:
- What terraform plugin to use
- What exactly to write in my settings.json
My current user settings.json file is:
{
"workbench.startupEditor": "welcomePage",
"editor.minimap.enabled": false,
"terminal.integrated.scrollback": 1000,
"git.enableSmartCommit": true,
"editor.detectIndentation": false,
"window.zoomLevel": -1,
"diffEditor.renderSideBySide": false,
"extensions.ignoreRecommendations": false,
"workbench.colorTheme": "PowerShell ISE",
"powershell.codeFormatting.whitespaceBeforeOpenBrace": false,
"git.autofetch": true,
"terminal.integrated.rendererType": "dom",
"terraform.path": "C:\\Users\\mkharitonov\\.terraform\\terraform.exe"
}
My local workspace settings.json file is:
{
"git.ignoreLimitWarning": true,
"powershell.codeFormatting.openBraceOnSameLine": false,
"powershell.codeFormatting.whitespaceBeforeOpenBrace": false,
"editor.formatOnPaste": true,
"editor.formatOnSave": true
}
Solution 1:[1]
It is recommended to use 2 spaces for terraform code instead of tabs or 4 spaces. You might see it in the Style Conventions
To configure the VS Code:
- the terraform should be installed on your Mac
- edit the
settings.jsonand add the following:
"[terraform]": {
"editor.formatOnSave": true
}
After this all your *.tf files will be automatically formatted on save.
Solution 2:[2]
Solution for macOS + VSCode
If you have problems with auto-formatting you may try doing so:
Install HashiCorp Terraform plugin
Install Terraform version manager:
brew install tfenvAsk
tfenvto use the specific version used by your project, for instance:tfenv use 0.12.20Add the following in your
settings.json:"[terraform]": { "editor.formatOnSave": true }
Solution 3:[3]
The other answers are fine and work. However, it is really annoying to have a 4 character indent and have the auto-save process snap it back to 2 characters.
For whatever reason, VSCODE and the Terraform plugin default to 4 spaces. This can be changed by adding these blocks to your settings.json:
"[terraform]": {
"editor.tabSize": 2
},
"[terraform-vars]": {
"editor.tabSize": 2
},
These will keep your indents to the recommended 2 spaces when you are editing and avoid the violent snap from 4 to 2 spaces when the auto-save process saves your file.
Solution 4:[4]
Easy, install both the Prettier and the extension made by HashiCorp Terraform.
If you need to you can go into your vscode settings.json and add a section for terraform.
"[terraform]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "hashicorp.terraform",
"editor.tabSize": 2, // optionally
},
"[terraform-vars]": {
"editor.tabSize": 2 // optionally
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ????? ??????? |
| Solution 2 | |
| Solution 3 | Doug |
| Solution 4 | jasonleonhard |
