How to Use Terraform fmt -recursive Command [Examples]

terraform

🚀 Level Up Your Infrastructure Skills

You focus on building. We’ll keep you updated. Get curated infrastructure insights that help you make smarter decisions.

Keeping Terraform code tidy and consistent across large projects can be tricky. The terraform fmt -recursive command makes this easier by automatically reformatting all configuration files in a directory tree. 

In this article, we’ll explore how to use it efficiently, along with examples and best practices for clean, standardized Terraform code.

What is the Terraform fmt -recursive command?

The terraform fmt -recursive command formats all Terraform files (*.tf) in the current directory and all subdirectories. It enforces consistent style by automatically fixing indentation and spacing according to Terraform’s standard formatting rules. 

This is useful in larger projects with nested module structures or multiple directories, as it ensures uniform formatting across the entire codebase. Without -recursive, terraform fmt only processes files in the current directory. Without -recursive, terraform fmt only processes files in the current or specified directory.

The terraform fmt -recursive command does not change Terraform’s logic or configuration behavior, only its visual structure.

Note: It does not format JSON variants such as .tf.json or .tfvars.json.

Example 1: Formatting your entire project

Running this from the root of your repository will format every .tf and .tfvars file it finds. It’s perfect for large setups where Terraform code lives across multiple environments or modules.

terraform fmt -recursive

In CI pipelines, pairing it with terraform fmt -recursive -check helps enforce consistent formatting automatically.

Pro tip: Add terraform fmt -recursive to a pre-commit hook or CI job. It’s one of those low-effort automations that quietly keeps your team’s Terraform codebase consistent and easy to read.

Example 2: Formatting all modules in a repository

Let’s say you’re working on a Terraform repository with multiple modules and nested directories:

infrastructure/
├── main.tf
├── variables.tf
├── modules/
│   ├── network/
│   │   └── main.tf
│   └── compute/
│       └── main.tf

Running:

terraform fmt -recursive

will walk through each subdirectory, modules/network, modules/compute, and any others, and automatically reformat every .tf, .tfvars, and .tftest.hcl file it finds.

This is perfect before committing or opening a pull request, ensuring everyone’s code follows Terraform’s canonical style. You’ll avoid merge conflicts caused by indentation or spacing differences and maintain cleaner diffs for reviewers.

Example 3: Formatting a specific subdirectory

If you only want to reformat Terraform files inside a specific folder you can limit the scope by specifying a path. 

In this example, Terraform will still apply the -recursive behavior within that folder, formatting everything it finds inside modules and its submodules.

terraform fmt -recursive ./modules/

This approach is especially handy when you’re working on a shared module library or testing local changes. It keeps your formatting consistent without touching unrelated environments or configurations. 

Example 4: Checking formatting before applying changes

You can also use the command in dry-run mode to verify what would be changed, without actually modifying the files. Just add the -check flag:

terraform fmt -recursive -check

This tells Terraform to scan all directories recursively and report any files that don’t match the expected format. For larger infrastructure teams, it’s a powerful safeguard in CI pipelines, because it enforces consistent formatting before running terraform plan or terraform apply.

To see exactly what would change, combine it with -diff:

terraform fmt -recursive -check -diff

If the command exits with a non-zero status, you’ll know which files need attention. You can then fix them locally by running the same command without -check.

Key points

terraform fmt -recursive is your shortcut to clean, consistent Terraform code. It formats all .tf, .tfvars, .tftest.hcl, and .tfmock.hcl files across subdirectories with one simple command — saving time, reducing PR noise, and keeping your infrastructure code easy on the eyes.

Keep in mind that in larger repositories, this command can take a bit of time to complete, so a little patience goes a long way.

Terraform is really powerful, but to achieve an end-to-end secure GitOps approach, you need to use a product that can run your Terraform workflows. Spacelift takes managing Terraform to the next level by giving you access to a powerful CI/CD workflow and unlocking features such as:

  • Policies (based on Open Policy Agent)
  • Multi-IaC workflows
  • Self-service infrastructure
  • Integrations with any third-party tools

If you want to learn more about Spacelift, create a free account today or book a demo with one of our engineers.

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Manage Terraform better with Spacelift

Build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization, and many more.

Learn more

Terraform Functions Cheat Sheet

Grab our ultimate cheat sheet PDF for all
the Terraform functions you need on hand.

Share your data and download the cheat sheet