How to Format Terraform Code: A Beginner's Guide

How to Format Terraform Code: A Beginner's Guide

Introduction

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define, provision, and manage your infrastructure in a declarative way. However, as your Terraform projects grow, maintaining clean and well-structured code becomes crucial. That's where code formatting comes in. In this blog post, we'll dive into the importance of formatting Terraform code and provide you with practical examples and different commands to help you keep your codebase organized and readable.

Why Format Terraform Code?

Before we dive into how let's briefly discuss why formatting your Terraform code is so important:

  1. Readability: Well-formatted code is easier to read and understand, both for you and your team members. It reduces the chances of errors and makes collaboration smoother.

  2. Consistency: A consistent code style across your project ensures that everyone is on the same page. It's like having a common language that all team members can understand.

  3. Maintenance: As your infrastructure evolves, you'll need to make changes. Properly formatted code is easier to maintain and update, saving you time and effort in the long run.

Now that we understand why formatting is crucial, let's explore how to do it.

Terraform fmt

Terraform provides a built-in command called terraform fmt for formatting your code. This command automatically updates your configuration files to follow the standard Terraform style. Here's how to use it:

terraform fmt

Example:

Let's say you have a Terraform configuration file named main. tf with inconsistent formatting:

resource "aws_instance" "example" {
     ami = "ami-0c55b159cbfafe1f0"
 instance_type = "t2.micro"
}
variable "region" {default = "us-east-1"
}

After running terraform fmt, the code will be automatically formatted to:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

variable "region" {
  default = "us-east-1"
}

As you can see, terraform fmt indents and aligns the code to match Terraform's standard style.

Editor Plugins

To make formatting even more convenient, many popular code editors, like Visual Studio Code and IntelliJ IDEA, offer plugins that can automatically format your Terraform code as you write. These plugins can save you the hassle of running terraform fmt manually.

Here's an example of using the Terraform extension in Visual Studio Code:

  1. Install the Terraform extension.

  2. Open your Terraform configuration file in Visual Studio Code.

  3. Right-click within the file and select "Format Document."

The plugin will automatically format your code according to the Terraform style guide.

Customizing Formatting Rules

While Terraform's built-in formatting is generally sufficient, you can also customize formatting rules to match your team's preferences. This is done through a .tf.json configuration file, where you can define rules for indentation, line length, and more. Here's an example:

{
  "indent": {
    "style": "  ",
    "align_parameters": true,
    "align_values": true
  }
}

To apply custom formatting rules, use the -config flag with terraform fmt:

terraform fmt -config=path/to/your/.tf.json

Conclusion

Properly formatting your Terraform code is a fundamental step towards maintaining a clean and efficient infrastructure as a code project. By using terraform fmt and editor plugins, you can ensure that your code is readable, consistent, and easy to maintain. Don't forget that you can also customize formatting rules to match your team's specific requirements.

So, go ahead and start formatting your Terraform code today! Your future self and your team members will thank you for it. Happy coding!