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:
- Modularity – Breaking down your configuration into smaller, focused files promotes reusability and maintainability.
- Organization – Grouping related resources and configurations together enhances readability and makes it easier to locate specific settings.
- Collaboration – When multiple team members work on the same project, splitting
main.tfreduces 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 asazurerm). Often times, this file may be combined within themain.tffile 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.
Original Article Source: Terraform: Split main.tf into seperate files written by Chris Pietschmann (If you're reading this somewhere other than Build5Nines.com, it was republished without permission.)
Microsoft Azure Regions: Interactive Map of Global Datacenters
Create Azure Architecture Diagrams with Microsoft Visio
IPv4 Address CIDR Range Reference and Calculator
Book Launch: Ultimate Guide to Microsoft Certification
Terraform Functions and Expressions Explained





