How to Auto Format Terraform Code in Visual Studio Code on Save

How to Auto Format Terraform Code in Visual Studio Code on Save

How to Auto Format Terraform Code in Visual Studio Code on Save

Formatting your code makes it easier to read and understand for yourself and for your fellow co-workers who will collaborate on it. And better readability rewards us with other perks like easier-to-maintain code.

Terraform being our Infrastructure as Code tools use similar best practices as code. Terraform has a command called terraform fmt that can format your code well.

However, nothing beats the comfort of getting your terraform code formatted on save in autopilot mode.

Right !!!

In this post, you will learn to auto format terraform code in Visual Studio Code on save.

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Why Formatting Your Terraform Manually with terraform fmt is not enough?

If you are not a very frequent user of terraform, it’s okay to just run the terraform fmt command and it formats your terraform file correctly.

Open your terraform project in a terminal. And type terraform fmt. It formats your code correctly.

However, what if you forget to run terraform fmt every time you write new terraform code? You will commit your file in version control and your terraform code will stay easier to read and maintain until you decide to change that.

And the best way to change that is to enable auto-format on save so that you don’t need to worry about it. Your terraform code will be formatted automatically.

Steps to Auto Format Terraform Code in Visual Studio Code on Save

I am breaking down the steps and making it as simple as possible.

  1. Install Visual Studio Code
  2. Install HashiCorp Terraform Extention in VS Code
  3. Configure Terraform Auto Format in VS Code
  4. Write some Terraform Code or Import Existing
  5. Validate Format on Save

Step 1: Install Visual Studio Code

If you already have Visual Studio Code or VS Code installed in your system then move to the next step.

If not, just follow this official page to install it on your system.

Once, VS Code is installed, move to step 2 and we will be adding an extension.

Step 2: Install HashiCorp Terraform Extention in VS Code

Hashicorp Terraform extension provides you with a formatter that formats your code with terraform fmt automatically. And that’s why we’ll install this extension first.

Open VS Code and click on the Extensions icon from the activity bar on the side of the VScode. You can also use a shortcut Ctrl+Shift+X to open the Extensions.

How to Auto Format Terraform Code in Visual Studio Code on Save 1

You will see the list of popular extensions and a search bar as shown below where you can search for an extension. Let’s search for Hashicorp Terraform.

How to Auto Format Terraform Code in Visual Studio Code on Save 2

Once the Hashicorp Terraform appears, select it to see the details as seen above. Click on Install from the detail screen or list screen and the extension gets installed.

Step 3: Configure Terraform Auto Format in VS Code

Time to configure the auto format.

Navigate to File -> Preference-> Settings as shown below screenshot.

How to Auto Format Terraform Code in Visual Studio Code on Save 3

We need to configure format settings. Therefore, search for the format in the Settings screen.

How to Auto Format Terraform Code in Visual Studio Code on Save 4

Check on the Format On Save as shown in the above screen.

But this is not enough to format our terraform code on save. We need to do a bit more. You might have guessed it by now.

And you are right.

We need to choose the formatter that VS Code will use to be able to format the terraform code correctly.

Type formatter in the search bar and select HashiCorp Terraform as the default formatter..

How to Auto Format Terraform Code in Visual Studio Code on Save 5

And we are done, time to test things out.

Step 4: Write some Terraform Code or Import Existing

Close any open window and navigate to Explorer.

Import your existing terraform project or create a new one here.

Create a main.tf file and paste the below content

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
         version = "~> 3.27"
    }
  }
}

provider "aws" {
               profile = "default"
  region  = "ap-south-1"
}

resource "aws_instance" "demo_instance" {
                         ami                    = "ami-06489866022e12a14"
              instance_type          = "t2.micro"
}

Can you notice the absurd formatting on the terraform file? No worries, we are about to validate auto format on Save.

Step 5: Validate Format on Save

Our terraform code currently looks ugly and unformatted.

Unformatted Code

Time to save the file watch the magic.

Just save your file or press CTRL + S and Tadda !!!

Formatted code

Here comes our pretty formatted terraform code 🙂

Conclusion

In this post, we learnt how to auto format terraform code in Visual Studio Code on save.

We also learnt that terraform has a convenient command terraform fmt that can format your code. However, nothing beats the comfort of formatting your code on save.

And then we learnt to format our code automatically by saving a file in our Visual Studio Code.

I hope you found this post useful. Please let me know in the comment section.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

If you liked reading my post, motivate me by

  • Adding a comment below on what you liked and what can be improved.
  • Follow us on
  • Share this post with your friends and colleagues.

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *