0
0
Terraformcloud~15 mins

File naming conventions (.tf files) in Terraform - Deep Dive

Choose your learning style9 modes available
Overview - File naming conventions (.tf files)
What is it?
File naming conventions for .tf files are the agreed ways to name Terraform configuration files. These files tell Terraform how to build and manage cloud resources. Using clear and consistent names helps people understand what each file does. It also makes managing and updating infrastructure easier and safer.
Why it matters
Without good file naming, Terraform projects become confusing and error-prone. People waste time guessing what files contain or accidentally change the wrong settings. Clear names prevent mistakes and speed up teamwork, especially when many people work on the same cloud setup.
Where it fits
Before learning file naming, you should know basic Terraform concepts like resources and modules. After mastering naming, you can learn about organizing Terraform projects, using modules, and automating deployments.
Mental Model
Core Idea
Good file names in Terraform act like clear labels on folders, guiding you quickly to the right settings without confusion.
Think of it like...
Imagine a messy desk with papers all mixed up versus a desk with labeled folders for bills, letters, and notes. The labeled desk saves time and avoids mistakes, just like good file names in Terraform.
┌───────────────┐
│ terraform/    │
├───────────────┤
│ main.tf       │ ← Main setup instructions
│ variables.tf  │ ← Input values definitions
│ outputs.tf    │ ← What info to show after apply
│ network.tf    │ ← Network resources
│ security.tf   │ ← Security rules
└───────────────┘
Build-Up - 7 Steps
1
FoundationPurpose of .tf Files
🤔
Concept: Understand what .tf files are and why they exist.
Terraform uses files ending with .tf to store instructions for creating cloud resources. Each file contains code written in HashiCorp Configuration Language (HCL). These files together describe the desired cloud setup.
Result
You know that .tf files are the building blocks of Terraform projects.
Understanding that .tf files hold the infrastructure code helps you see why organizing them matters.
2
FoundationBasic Naming Patterns
🤔
Concept: Learn simple, common names used for .tf files.
Common file names include main.tf for the main configuration, variables.tf for inputs, and outputs.tf for outputs. These names tell you what each file controls without opening it.
Result
You can recognize the role of a file just by its name.
Knowing standard names reduces confusion and speeds up reading Terraform projects.
3
IntermediateGrouping by Resource Type
🤔Before reading on: do you think putting all resources in one file or splitting by type is better? Commit to your answer.
Concept: Organize files by resource categories like network, compute, or security.
Instead of one big file, split your .tf files by what they manage. For example, network.tf holds all network-related resources, and security.tf holds firewall rules. This makes it easier to find and update specific parts.
Result
Your project is easier to navigate and maintain.
Grouping by resource type helps teams work in parallel and reduces errors from editing unrelated resources.
4
IntermediateUsing Environment-Specific Files
🤔Before reading on: should environment settings be mixed in one file or separated? Commit to your answer.
Concept: Separate files for different environments like dev, staging, and prod.
Create files like dev.tfvars, staging.tfvars, and prod.tfvars to hold environment-specific values. This keeps your main configuration clean and lets you switch environments easily.
Result
You can deploy the same setup with different settings safely.
Separating environment data prevents accidental changes to production and supports safer testing.
5
IntermediateNaming for Modules and Reusability
🤔
Concept: Name files clearly when using modules to reuse code.
Modules are like mini-projects inside Terraform. Name module files to reflect their purpose, like vpc_module.tf or database_module.tf. This clarity helps when sharing or updating modules.
Result
Modules are easier to find and reuse across projects.
Clear module file names improve collaboration and reduce duplication.
6
AdvancedAvoiding Common Naming Pitfalls
🤔Before reading on: do you think using generic names like config.tf is good or bad? Commit to your answer.
Concept: Why vague or inconsistent names cause problems.
Names like config.tf or stuff.tf don't tell you what is inside. This leads to confusion and mistakes. Always use descriptive names that reflect the file's content or purpose.
Result
Your project stays understandable and maintainable over time.
Avoiding vague names prevents wasted time and costly errors during updates.
7
ExpertDynamic File Loading and Naming Conventions
🤔Before reading on: do you think Terraform loads .tf files in alphabetical order or random? Commit to your answer.
Concept: How Terraform loads files and why naming affects behavior.
Terraform loads all .tf files in a directory automatically, merging their content. The order is alphabetical, so sometimes naming files with prefixes like 01_network.tf and 02_security.tf controls load order. This can affect variable and resource dependencies.
Result
You can control configuration loading order to avoid errors.
Knowing file load order helps prevent subtle bugs and manage complex dependencies.
Under the Hood
Terraform reads all .tf files in a folder and combines their content into one configuration. It parses the files in alphabetical order, merging resources, variables, and outputs. This means the file names indirectly control the order of processing, which can affect how variables and dependencies resolve.
Why designed this way?
Terraform treats all .tf files as parts of a single configuration to allow modularity and flexibility. Alphabetical loading is simple and predictable, avoiding complex explicit ordering. This design lets users organize files as they prefer while keeping the system straightforward.
terraform_project/
├─ 01_variables.tf  ← Variables loaded first
├─ 02_network.tf    ← Network resources
├─ 03_security.tf   ← Security resources
├─ main.tf          ← Main configuration
└─ outputs.tf       ← Outputs

Files load in this order, merging into one config.
Myth Busters - 4 Common Misconceptions
Quick: Do you think Terraform requires a main.tf file to work? Commit yes or no.
Common Belief:Terraform needs a file named main.tf to run properly.
Tap to reveal reality
Reality:Terraform loads all .tf files in a directory regardless of their names; main.tf is just a common convention, not a requirement.
Why it matters:Believing main.tf is required can limit flexibility and cause confusion when working with projects that use different naming.
Quick: Do you think file names affect the order Terraform processes resources? Commit yes or no.
Common Belief:File names do not affect how Terraform processes resources; order does not matter.
Tap to reveal reality
Reality:Terraform loads files alphabetically, so file names can influence the order of processing, which matters for dependencies.
Why it matters:Ignoring file load order can cause errors when resources depend on variables or other resources defined in different files.
Quick: Do you think putting all resources in one big .tf file is better than splitting? Commit your answer.
Common Belief:Keeping all resources in one file is simpler and better for small projects.
Tap to reveal reality
Reality:Even small projects benefit from splitting files by purpose to improve clarity and reduce mistakes.
Why it matters:Not splitting files can make projects harder to maintain and understand as they grow.
Quick: Do you think environment-specific settings belong inside main .tf files? Commit yes or no.
Common Belief:Environment settings should be mixed inside main .tf files for simplicity.
Tap to reveal reality
Reality:Separating environment-specific values into .tfvars files keeps configurations clean and safer to manage.
Why it matters:Mixing environment data risks accidental changes to production and complicates deployments.
Expert Zone
1
Prefixing file names with numbers can control load order but overusing this can make projects hard to navigate.
2
Using descriptive names helps automated tools and IDEs provide better support and error detection.
3
Some teams adopt naming conventions that reflect deployment stages or cloud providers, adding clarity in multi-cloud setups.
When NOT to use
Avoid strict alphabetical naming control in very large projects where explicit dependency management tools or modules are better. Instead, use Terraform modules and workspaces to manage complexity.
Production Patterns
In production, teams often split files by resource type and environment, use modules with clear names, and automate validation to enforce naming standards. This reduces errors and improves collaboration.
Connections
Software Project Structure
Similar pattern of organizing code files by purpose and function.
Understanding file naming in Terraform is like organizing source code files in software projects, which improves readability and teamwork.
Version Control Branch Naming
Both use naming conventions to communicate purpose and state clearly.
Clear naming in Terraform files and version control branches both prevent confusion and mistakes in collaborative environments.
Library Classification in Libraries
Both organize items by category to make finding and using them easier.
Just as libraries group books by topic, Terraform file naming groups configuration by resource type, aiding quick access and understanding.
Common Pitfalls
#1Using vague file names that don't describe content.
Wrong approach:config.tf networkstuff.tf misc.tf
Correct approach:variables.tf network.tf security.tf
Root cause:Not understanding the importance of descriptive names leads to confusion and errors.
#2Mixing environment-specific variables inside main configuration files.
Wrong approach:variable "region" { default = "us-west-1" } # in main.tf variable "region" { default = "us-east-1" } # for prod in same file
Correct approach:Use dev.tfvars and prod.tfvars files with different region values, keeping main.tf clean.
Root cause:Lack of separation between configuration and environment data causes accidental overwrites.
#3Assuming Terraform processes files in random order.
Wrong approach:Naming files arbitrarily without considering load order, e.g., security.tf before variables.tf.
Correct approach:Prefix files to control order, e.g., 01_variables.tf, 02_security.tf.
Root cause:Not knowing Terraform loads files alphabetically leads to dependency errors.
Key Takeaways
Terraform loads all .tf files in a folder as one combined configuration, so file names help organize but are not required to be specific.
Using clear, descriptive file names improves project clarity, teamwork, and reduces mistakes.
Splitting files by resource type and environment settings supports safer and easier management.
Terraform loads files alphabetically, so naming can control processing order and avoid subtle bugs.
Avoid vague names and mixing environment data inside main files to keep projects maintainable and safe.