Skip to content
A simple tool for Terraform language syntax cleanup, extending terraform fmt
Go
Branch: master
Clone or download

Latest commit

apparentlymart dependencies: go get github.com/hashicorp/hcl/v2@v2.2.0
This includes a fix for a crashing bug in the hclwrite package that we'd
previously got by depending on unmerged commit 84e71e9393a0 directly.
Latest commit 50d656f Dec 17, 2019

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE LICENSE Nov 20, 2019
README.md README Nov 20, 2019
go.mod dependencies: go get github.com/hashicorp/hcl/v2@v2.2.0 Dec 17, 2019
go.sum dependencies: go get github.com/hashicorp/hcl/v2@v2.2.0 Dec 17, 2019
main.go Preserve stack trace printing while recovering HCL parser errors Dec 10, 2019

README.md

terraform-clean-syntax

terraform-clean-syntax is a simple command line tool for performing some small syntax cleanup steps on Terraform .tf configuration files automatically.

Specifically, it currently knows how to clean up the following:

  • Argument values that are just a single template interpolation, like "${foo}", are simplified to the equivalent foo.
  • Variable type constraints using the legacy forms from Terraform 0.11, like "string", "list", or "map", are replaced with their modern type constraint expressions string, list(string) and map(string).

The two changes listed above will both silence some (though not all) of the syntax deprecation warnings emitted by Terraform 0.12.14 and later. This program is conservative, so it may skip certain opportunities for cleanup if they are too complex for it to be sure that the change is safe.

The built in terraform fmt command in Terraform doesn't perform these cleanups automatically at the time of writing, because the Terraform team worried that this would make it difficult for folks to continue maintaining modules that are cross-compatible with both Terraform 0.11 and 0.12. The cleanups made by this program will render a configuration incompatible with Terraform 0.11, so this program should not be used on any module that must retain Terraform 0.11 compatibility.

Usage

After compiling the program using Go 1.12 or later, run it with a single argument that is a file or directory to apply rewriting to:

terraform-clean-syntax .

If given a directory, terraform-clean-syntax will visit all of the .tf files in the directory and recursively search any directories within it.

If given a single file, terraform-clean-syntax will process that file only if its name has the suffix .tf.

This program rewrites configuration files in-place, so it's best to make sure your version control work tree is clean before running so that you can clearly see which changes it is proposing and discard those changes if desired.

This program is a best-effort static analysis tool and it doesn't have intimate understanding of Terraform language syntax, so be sure to review the changes it proposes and test your resulting configuration with terraform validate and/or terraform plan before merging the changes into your codebase.

You can’t perform that action at this time.