Bird
Raised Fist0
Terraformcloud~10 mins

File naming conventions (.tf files) in Terraform - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - File naming conventions (.tf files)
Start Terraform Project
↓
Create .tf files
↓
Name files by purpose
↓
Terraform loads all .tf files
↓
Apply configuration
↓
End
Terraform reads all .tf files in a folder. Naming files by purpose helps organize and understand the project.
Execution Sample
Terraform
main.tf
variables.tf
outputs.tf
network.tf
security.tf
Example of common Terraform file names showing how to separate configuration by purpose.
Process Table
StepActionFile NamePurposeEffect
1Create main configuration filemain.tfDefines main resourcesTerraform loads main resources
2Create variables filevariables.tfDefines input variablesTerraform knows inputs to expect
3Create outputs fileoutputs.tfDefines outputsTerraform knows what to show after apply
4Create network config filenetwork.tfDefines network resourcesNetwork setup separated for clarity
5Create security config filesecurity.tfDefines security groups and rulesSecurity setup separated for clarity
6Terraform loads all .tf filesAll .tf filesCombined configurationTerraform merges all configs before apply
7Apply terraformN/AExecute planInfrastructure created as defined
8EndN/ACompleteProject deployed successfully
💡 All .tf files loaded and merged; terraform apply completes deployment
Status Tracker
FileCreatedPurpose DefinedLoaded by Terraform
main.tfYesYesYes
variables.tfYesYesYes
outputs.tfYesYesYes
network.tfYesYesYes
security.tfYesYesYes
Key Moments - 3 Insights
Why do we split Terraform configuration into multiple .tf files?
Splitting files by purpose (see execution_table steps 1-5) helps organize code, making it easier to read and maintain.
Does Terraform care about the order or names of .tf files?
No, Terraform loads all .tf files in the folder together regardless of name or order (see execution_table step 6).
Can we name .tf files anything we want?
Yes, but using descriptive names like variables.tf or network.tf helps others understand the project quickly (see execution_table steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which file defines input variables?
Avariables.tf
Bmain.tf
Coutputs.tf
Dnetwork.tf
💡 Hint
Check the Purpose column in execution_table row 2
At which step does Terraform load all .tf files together?
AStep 3
BStep 5
CStep 6
DStep 7
💡 Hint
Look for 'Terraform loads all .tf files' in execution_table
If we add a file named database.tf, what will happen during terraform apply?
ATerraform ignores database.tf
BTerraform loads database.tf and merges its config
CTerraform errors because of unknown file
DTerraform only loads main.tf and variables.tf
💡 Hint
Refer to execution_table step 6 about loading all .tf files
Concept Snapshot
Terraform reads all .tf files in a folder together.
Name files by purpose: main.tf, variables.tf, outputs.tf, etc.
File order and names do not affect loading.
Organizing files improves readability and maintenance.
Add new .tf files anytime; Terraform merges all configs.
Full Transcript
Terraform projects use multiple .tf files to organize infrastructure code. Each file has a purpose, like main.tf for main resources, variables.tf for inputs, and outputs.tf for outputs. Terraform loads all .tf files in the folder together, regardless of their names or order. This means you can split your configuration into logical parts for clarity. When you run terraform apply, it merges all these files and applies the combined configuration. Naming files clearly helps you and others understand the project structure easily.

Practice

(1/5)
1. Which of the following is the most common file name used to define the main Terraform configuration?
easy
A. terraform.tf
B. config.tf
C. setup.tf
D. main.tf

Solution

  1. Step 1: Understand common Terraform file names

    The main Terraform configuration is typically placed in a file named main.tf to keep it clear and standard.
  2. Step 2: Compare options with common practice

    Among the options, only main.tf is widely recognized and recommended for the main configuration.
  3. Final Answer:

    main.tf -> Option D
  4. Quick Check:

    Main configuration file = main.tf [OK]
Hint: Main config file is usually named main.tf [OK]
Common Mistakes:
  • Choosing uncommon names like config.tf
  • Using generic names like terraform.tf
  • Confusing setup files with main config
2. Which file name is conventionally used to declare input variables in Terraform?
easy
A. variables.tf
B. inputs.tf
C. vars.tf
D. params.tf

Solution

  1. Step 1: Identify the standard file for input variables

    Terraform commonly uses variables.tf to declare input variables for clarity and consistency.
  2. Step 2: Check other options for correctness

    Other names like inputs.tf, vars.tf, and params.tf are not standard and may confuse team members.
  3. Final Answer:

    variables.tf -> Option A
  4. Quick Check:

    Input variables file = variables.tf [OK]
Hint: Input variables go in variables.tf file [OK]
Common Mistakes:
  • Using inputs.tf instead of variables.tf
  • Shortening to vars.tf which is non-standard
  • Naming it params.tf which is unclear
3. Given these files in a Terraform project: main.tf, variables.tf, and outputs.tf, what is the purpose of outputs.tf?
medium
A. To define the resources to create
B. To declare input variables
C. To specify values to show after apply
D. To configure backend storage

Solution

  1. Step 1: Understand the role of outputs.tf

    The outputs.tf file is used to declare output values that Terraform will display after applying the configuration.
  2. Step 2: Differentiate from other files

    main.tf defines resources, variables.tf declares inputs, and backend configuration is usually in backend.tf or inside main.tf.
  3. Final Answer:

    To specify values to show after apply -> Option C
  4. Quick Check:

    outputs.tf shows output values [OK]
Hint: Outputs.tf shows results after apply [OK]
Common Mistakes:
  • Confusing outputs.tf with variables.tf
  • Thinking outputs.tf defines resources
  • Assuming outputs.tf configures backend
4. You have a Terraform project with files named main.tf, variables.tf, and output.tf. Why might this violate standard conventions?
medium
A. Because main.tf must contain outputs
B. Because the outputs file is misnamed; it should be outputs.tf
C. Because variables.tf is missing output declarations
D. Because Terraform requires outputs in a .json file

Solution

  1. Step 1: Check file naming conventions for outputs

    The conventional file name for outputs is outputs.tf (plural). Naming it output.tf (singular) violates standard conventions.
  2. Step 2: Understand Terraform file loading

    Terraform loads all .tf files regardless of name for functionality, but standard names improve clarity and teamwork.
  3. Final Answer:

    Because the outputs file is misnamed; it should be outputs.tf -> Option B
  4. Quick Check:

    outputs file = outputs.tf [OK]
Hint: Outputs file should be named outputs.tf [OK]
Common Mistakes:
  • Assuming outputs can be in variables.tf
  • Thinking main.tf must have outputs
  • Expecting outputs in JSON files
5. You want to organize a large Terraform project by splitting configurations into multiple files. Which naming approach best supports teamwork and maintenance?
hard
A. Use descriptive names like network.tf, compute.tf, variables.tf, outputs.tf
B. Name all files config1.tf, config2.tf, config3.tf for simplicity
C. Put all code in main.tf to avoid confusion
D. Use random names to avoid predictable patterns

Solution

  1. Step 1: Consider clarity and teamwork in file naming

    Descriptive file names like network.tf and compute.tf help team members quickly find and understand code sections.
  2. Step 2: Evaluate other naming options

    Generic names like config1.tf or random names cause confusion and make maintenance harder. Putting all code in one file reduces clarity.
  3. Final Answer:

    Use descriptive names like network.tf, compute.tf, variables.tf, outputs.tf -> Option A
  4. Quick Check:

    Descriptive file names improve teamwork [OK]
Hint: Use clear, descriptive file names for easier teamwork [OK]
Common Mistakes:
  • Using numbered or random file names
  • Putting all code in one file
  • Ignoring naming conventions for clarity