How to Use the Command 'terraform fmt' (with Examples)
Terraform is a powerful Infrastructure as Code (IaC) tool used to define and provision data center infrastructure. The terraform fmt
command is specifically designed to ensure that Terraform configuration files conform to standard style conventions, enhancing readability and maintainability of the code. This command formats all Terraform configuration files in a consistent manner. Here, we’ll explore different use cases of this command, explaining how to execute it and why each method is beneficial.
Use Case 1: Format the Configuration in the Current Directory
Code:
terraform fmt
Motivation:
When you are actively developing infrastructure code using Terraform, it’s common to make changes to multiple configuration files. Keeping consistently formatted code is essential for easier collaboration, review, and understanding of your infrastructure codebase. Running terraform fmt
in the current directory ensures that all .tf
files in that directory adhere to Terraform’s style standards, with minimal effort.
Explanation:
terraform
: This calls the Terraform CLI which is required to execute any Terraform-related commands.fmt
: Represents the specific formatting tool provided by Terraform to structure your configuration files according to best practices.
Example Output:
main.tf
variables.tf
This indicates that main.tf
and variables.tf
have been formatted. If these files listed had needed reformatting, they were adjusted to fit Terraform’s style conventions.
Use Case 2: Format the Configuration in the Current Directory and Subdirectories
Code:
terraform fmt -recursive
Motivation:
Complex projects often involve multiple modules, each with its own set of configuration files arranged in various subdirectories. Instead of manually formatting each directory, terraform fmt -recursive
automates the process, ensuring uniform formatting across your entire project tree. This is particularly useful in large projects where tracking down every configuration file manually would be inefficient.
Explanation:
terraform
: Invokes the Terraform CLI.fmt
: Specifies the format command.-recursive
: A flag that tells the command to apply formatting not only to the current directory but also to all nested directories.
Example Output:
module1/main.tf
module2/variables.tf
The output indicates that main.tf
inside module1
and variables.tf
inside module2
were formatted, showcasing that changes have been recursively applied to files within subdirectories.
Use Case 3: Display Diffs of Formatting Changes
Code:
terraform fmt -diff
Motivation:
For developers and infrastructure engineers, understanding what changes automatic formatting applies is essential to maintain the accuracy and intention of code. By showing line-by-line diffs, this command helps you scrutinize what terraform fmt
intends to modify, offering transparency and control over your codebase.
Explanation:
terraform
: Calls the essential Terraform CLI.fmt
: Activates the format function for your configuration files.-diff
: This flag tells Terraform to show what changes will be made as a diff. Useful for review and debugging purposes.
Example Output:
~ resource "aws_instance" "example" {
instance_type = "t2.micro"
- ami = "ami-123456"
+ ami = "ami-654321"
}
This mock diff output shows that the ami
value within an aws_instance
resource was adjusted, indicating changes that terraform fmt
would apply.
Use Case 4: Do Not List Files That Were Formatted to stdout
Code:
terraform fmt -list=false
Motivation:
Sometimes, the volume of formatted files listed in the output can be overwhelming, especially in large projects. If you’re only interested in the action of formatting, without the clutter of individual file names being displayed, this option helps streamline your console output, focusing attention on what matters most.
Explanation:
terraform
: The command begins with invoking the Terraform CLI.fmt
: Triggers the formatting of your Terraform files.-list=false
: This flag suppresses the list of files that are usually displayed after formatting, clearing your console of unnecessary details.
Example Output:
(successful execution with no additional file list output)
The operation completes without showing file names, indicating that only the result matters, not the specific files altered.
Conclusion
Utilizing the terraform fmt
command in your Terraform projects is a practice that fosters readability and consistency in infrastructure code. With various options such as recursion, diff display, and list suppression, developers and infrastructure engineers can apply formatting in a manner that suits their workflow, thereby improving cohesion across development teams and simplifying the review process. Whether you’re managing a small project or a sprawling infrastructure, terraform fmt
helps to automate and maintain your configuration style effortlessly.