When using HashiCorp Terraform as the Infrastructure as Code (IaC) tool of choice, it becomes critical to organize the Terraform code as the Terraform project becomes more complex. One of the most common practices is to split the Terraform project from a single file (typically named main.tf) into multiple smaller files. This helps increase maintainability and readability.

While Terraform Modules offer modularization, sometimes it’s most appropriate to use a simpler approach, especially for smaller projects or when modules just aren’t necessary.

This article will explain the reasons why a Terraform project should be split into multiple files, what the benefits are, and the typical multiple file breakout used.

Why split main.tf into multiple files?

When writing Terraform code, the most common file name used for a single file is main.tf. However, any filename can be used, as Terraform is not sensitive to the file name(s) used.

The main.tf file is often the primary file used in a Terraform configuration project, containing resource declarations, providers, inputs, outputs, variables, and other Terraform configuration code. However, as your infrastructure code increased in complexity, the main.tf file can become very large, making it challenging to navigate and modify.

Splitting out the main.tf file it into multiple smaller files offers several benefits:

  1. Modularity – Breaking down your configuration into smaller, focused files promotes reusability and maintainability.
  2. Organization – Grouping related resources and configurations together enhances readability and makes it easier to locate specific settings.
  3. Collaboration – When multiple team members work on the same project, splitting main.tf reduces the likelihood of merge conflicts and facilitates concurrent development.

Best Practice Terraform File Structure

Before splitting apart the single main.tf file of a Terraform project, it’s important to identify a file structure and naming pattern to use. While Terraform doesn’t enforce any specific file naming conventions, the following multiple file breakout and names are recommended by HashiCorp and used as a standard best practice to follow.

Here are the best practice Terraform file names to use when breaking out the main.tf file into multiple smaller files:

main.tf
providers.tf
variables.tf
outputs.tf
{customname}.tf
  • main.tf – Define locals (aka variables used within the deployment), call modules, and data sources to create all resources. Generally, this contains these primary components of the Terraform deployment.
  • providers.tf – Add the provider definitions for the Terraform Providers your deployment is using (such as azurerm). Often times, this file may be combined within the main.tf file instead of having a separate file since often only one or a couple providers will be in use.
  • variables.tf – Define the variables used by the Terraform deployment. These are the “input parameters” that will be passed into the Terraform code at deployment time.
  • outputs.tf – Define variables to output from the Terraform project or module.
  • {customname}.tf – Your own custom Terraform files. You can optionally separate your Terraform project code into any number of Terraform files. This helps to separate specific sets of resources into separate files for your own team or project organization.

When Terraform commands are run (such as init, plan, and apply), the terraform command-line tool will look at the contents of all the *.tf files within the folder. The reason for this is Terraform treats a folder, and all the files in it, as a single Terraform project. This enables you to break out and organize the Terraform code for your infrastructure deployment in a manner that makes sense for you, your team, and your organization.

Conclusion

Splitting a main.tf file without using modules can significantly improve the maintainability and readability of your Terraform configurations. By adopting a structured approach and breaking down your configuration into smaller, focused files, you can better manage complexity, promote reusability, and streamline collaboration within your team. Start with the best practice recommendations for the multiple file break out and file names, then experiment with different file structures to find what works best for your project. Keep in mind to always strive for clarity and simplicity in your Terraform codebases for improved maintainability.

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
Microsoft MVP HashiCorp Ambassador

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading