Formatting Terraform Code With the Terraform fmt Command


The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

The terraform fmt command does not insert a newline into your code. Instead, it is designed to format the output of commands and make them more readable regardless of how you wrote that code in Terraform.

The “terraform fmt command example” is a terraform command that allows formatting terraform code. The command can be used to format the code in different ways, including indenting and adding line breaks.

Formatting Terraform Code With the Terraform fmt Command

Do you despise reading ugliness in code? If that’s the case, you should also avoid authoring one. The HashiCorp Terraform language follows the same style guidelines as most other computer languages.

A single missing bracket or excessive indentation may make your Terraform configuration files difficult to read, maintain, and comprehend, resulting in a negative experience. You may, however, use the Terraform fmt command to correct all of these code discrepancies at once.

Continue reading to discover how to use a single command to rebuild Terraform configuration files while maintaining appropriate code formatting. You can even see how the changes will look before you commit them! Let’s begin improving your infrastructure provisioning experience!


If you want to follow along with this lesson, make sure you have the following:

How to Install Terraform on Windows is a related topic.

  • Use a coding editor like Visual Studio Code (VSCode), but feel free to use whichever editor you like.

A Tutorial on What You Need to Know About Visual Studio Code

Terraform Configuration File Creation

You should have a Terraform configuration file(s) to utilize as the target for the Terraform code formatting before continuing. To construct the working folders and configuration files for your project, follow the steps below.

1. On your computer, open a new terminal window.

2. In your home directory, create a folder called terraform-s3-demo. Your Terraform files will be stored in this folder. To create a functional folder, use the command below.

# mkdir /terraform-s3-demo/terraform-s3-demo/terraform-s3-demo/terraform-s3-demo/terraform-s3-demo/terraform-s3 # cd /terraform-s3-demo /terraform-s3-demo /terraform-s3-demo /terraform-s3-demo /terraform-s3-demo

3. Next, create a new file called with an empty name. Your Terraform configuration code will be stored in this file.

# Make a new configuration file, touch, in the current directory.

4. Now, in your code editor, open the file. If you’re using VSCode, enter code. to open the current working directory in the editor.

To modify the Terraform file, open it in a text editor. To modify the Terraform file, open it in a text editor.

5. Paste the configuration below, which describes the establishment of an AWS S3 bucket, into the file, and save it. For each code block, look at the inline comments.

Note that the name of the bucket must be unique throughout the whole AWS infrastructure. To make it unique, modify the bucket name in the code from terraform-demo-bucket-8972376 to anything other, such as –terraform-demo-bucket-68574321.

Note: The configuration code below is intentionally broken to show how to apply the proper formatting in following parts.

# Declaring the Provider Requirements terraform { required_providers { aws = { source = “hashicorp/aws” version = “~> 3.0” } } } # Configuring the AWS Provider (aws) with region set to ‘us-east-2’ provider “aws” { region = “us-east-2” } # Creating the encryption key which will encrypt the bucket objects resource “aws_kms_key” “mykey” { deletion_window_in_days = “20” } # Granting the Bucket Access resource “aws_s3_bucket_public_access_block” “publicaccess” { bucket = block_public_acls = false block_public_policy = false } # Creating the S3 Bucket and apply encryption resource “aws_s3_bucket” “demobucket” { ## Change the “bucket” name into a unique value before provisioning. bucket = “terraform-demo-bucket-8972376” force_destroy = false server_side_encryption_configuration { rule { apply_server_side_encryption_by_default { kms_master_key_id = aws_kms_key.mykey.arn sse_algorithm = “aws:kms” } } } # Keeping multiple versions of an object in the same bucket versioning { enabled = true } }

Terraform fmt Command Improves Code Readability

You’ve already completed the boilerplate configuration file required to establish an AWS S3 bucket. The terraform fmt command has a number of flags that may be used to assess if a file is in the canonical format or style.

Checking for Inconsistent Formatting in Files

Follow these procedures to see whether files in your current working directory have formatting issues.

If you haven’t already, open a terminal and browse to the /*terraform-s3-demo* directory.

After that, execute the command shown below. The -check option will produce a list of filenames that need to be formatted.

The program returned the filename, as seen below, suggesting that this file contains faulty code.

Checking for errors in the formatting Checking for errors in the formatting

To check a file under sub-directories (if any), use the terraform fmt command with the -recursive option to process the files under those sub-directories, for example: terraform fmt -recursive -check

Changes in Formatting Can Be Seen Before They’re Made

So you were able to go through the files and figure out which ones needed to be formatted. But wouldn’t it be wonderful to know which sections of the file are formatted incorrectly?

Fortunately, using the terraform fmt command with the -diff parameter enables you to see the changes before committing them. Copy and paste the command below into the terminal.

terraform fmt -check -diff terraform fmt -check -diff terraform fmt

You should see something like the picture below after executing the program. The filename is shown at the top of the screen, along with which lines do not follow the right formatting.

The lines with the red arrow start with “-” and represent the original (current configuration) code. The suggested code modifications are shown on the lines starting with “+” and the green arrow.

Proposed formatting changes are being reviewed. Proposed formatting changes are being reviewed.

Using Proper Formatting When Rewriting Configuration Files

You now know how to identify which files have inconsistencies in the code and how to preview the modifications. What’s the best way to use the Terraform configuration format now? Run the program below without any options to rebuild the configuration files.

View your code in the editor again after executing the command to validate the changes. The formatting of the configuration file before and after is shown in the figure below.

Confirming the result of the terraform fmt command Confirming the result of the terraform fmt command

Using Terraform’s fmt Formatted Configuration to Provision Resources

Using the Terraform fmt command, you’ve now formatted your configuration file. But how can you know whether the changed settings are still functional? Why not use it to create a new Amazon Web Services S3 bucket?

Terraform normally employs a three-command technique in order to supply a Terraform configuration:

  • terraform init — initializes the working directory, including installing the provider inside it.
  • Terraform plan — depending on your Terraform settings, develop an execution plan.
  • terraform apply – carry out the execution plan suggested by the terraform plan command.

To begin provisioning, follow these steps.

1. Open a terminal and go to /terraform-s3-demo/terraform-s3-demo/terraform-s3-demo/terraform-s3-demo/terraform-s3

2. Run the command below to initialize (init) the working directory. Installing the plugins and providers required to work with resources is part of the startup process.

If everything works properly, the message Terraform has been successfully started should appear in the output, as seen below.

The terraform code is being initialized. The terraform code is being initialized.

3. To establish an execution plan, use the command below. This step is optional, however it is strongly suggested to guarantee that your configuration files are error-free. Additionally, completing this step provides you with the resource blueprint that Terraform will use to provide resources in your infrastructure.

If everything went well, you should see a message in the output that says Plan: X to add, Y to alter, Z to destroy.

terraform plan is a command that can be used to create a terraform map. terraform plan is a command that can be used to create a terraform map.

4. Finally, use the command below to execute the execution plan. This program compiles a terraform state file from each configuration (*.tf files) in the working directory and delivers it to AWS, who then create the AWS S3 bucket.

The -auto-approve option disables the confirmation question and authorizes the plan execution automatically.

apply terraform -auto-approve

Terraform has successfully generated the AWS bucket in the AWS account, as you can see.

terraform apply is a command that allows you to apply terraform to your environment. terraform apply is a command that allows you to apply terraform to your environment.

5. In the us-east-2 region, you should now have an AWS S3 bucket. However, use the AWS Management Console to double-check that the bucket exists.

Open your preferred web browser and go to to log in to your AWS S3 Management Console account.

As you can see in the screenshot below, the Terraform-created S3 bucket has now been added to the AWS S3 buckets in the us-east-2 region.

The new S3 bucket has been confirmed. The new S3 bucket has been confirmed.


You learnt how to use the Terraform fmt tool to provide uniform formatting to your configuration files in this article.

It’s critical to have a consistent style or structure when creating Terraform configuration files. The improved readability of your code will help you and any other developer who will later work on your Terraform files.

So, if you’re bored of ugly code, use the Terraform fmt command to make it attractive.

The “terraform formatter” is a tool that allows you to format your Terraform code. The formatting will make it easier to read and follow.

Frequently Asked Questions

How do I run terraform FMT in Vscode?

A: To run Terraform in VScode, you will need to first install the terraform command line tool. You can find this by navigating to Tools -> Command Line Tool, and then installing it from there.

What is terraform init and format?

A: Terraform is an open-source tool for creating and managing infrastructure as code. It provides a declarative language to describe the desired state of your infrastructure, along with capabilities for automatically building it from source control repositories.
Format refers to how terraform converts this configuration into a binary representation that can be transferred across systems or installed on them in place via tools like packer

How do I use terraform code?

A: Terraform is a code editor that allows you to create, edit and run scripts with the Earth Engine. The terraforming tutorial will help you out if youre having trouble understanding it!

Related Tags

  • terraform fmt vscode
  • terraform fmt -check
  • terraform format code
  • terraform fmt single file
  • terraform fmt not working

Table of Content