Terraform files and module boundaries
A module is a directory of configuration files
Terraform reads the configuration files at the top level of a selected module directory together. Native configuration uses .tf; the JSON form uses .tf.json. A module is not one file, and Terraform does not treat main.tf as a script that runs before every other file. In the ordinary CLI workflow, the directory selected for the run is the root module. Use a fresh, reviewed directory from the previous lesson.
Example: divide one module into readable files
The following is a complete static layout for the built-in resource used in the previous lesson. The exact CLI pin is inherited from the previous batch and remains provisional until support and execution are verified. No external provider is declared.
lesson/ versions.tf main.tf outputs.tfversions.tf:
# versions.tfterraform { required_version = "= 1.14.8"}main.tf:
resource "terraform_data" "lesson" { input = "training"}outputs.tf:
output "label" { value = terraform_data.lesson.output}An output is a named result of configuration. terraform_data.lesson.output refers to the value exposed by the declared resource. After a successful future apply, the intended output label is training; it is not an observed execution result. Terraform uses references to understand relationships, rather than relying on file names for execution order. The next chapter explains initialization and apply.
Conventions, not execution instructions
Teams often put resource declarations in main.tf, input declarations in variables.tf, output declarations in outputs.tf, and version constraints in versions.tf. You can use other ordinary names, such as inventory.tf. The file split helps humans review changes. Save text as UTF-8. A file named README.md is documentation; a .tfvars file supplies input values under particular loading rules and is not an ordinary module configuration file.
Do not define the same resource with the same type and local name in two ordinary files of a module. Terraform reports a duplicate declaration instead of treating the second as an update.
Example: the JSON configuration form
The native resource block from main.tf can be expressed in JSON as shown below. The block type and labels are represented by nested property names.
{ "resource": { "terraform_data": { "lesson": { "input": "training" } } }}Save this alternative as main.tf.json instead of main.tf, not alongside it. Both declare the same resource. JSON syntax is useful for generated configuration, but arbitrary JSON is not necessarily valid Terraform configuration. Expression strings and nested blocks have Terraform-specific mapping rules, so use the documented mapping rather than a generic text conversion.
Example: find the module boundary
Consider a fictional root directory containing main.tf, outputs.tf, README.md, and modules/labels/main.tf. The two top-level .tf files belong to the root module. The nested configuration is separate and is included only if explicitly called as a child module. A module block is the mechanism for such a call; its source and interface are taught later. Running a separate CLI operation from the nested directory would instead select it as a root for that run.
Special override files are an exception to ordinary duplicate rules. Names such as override.tf, override.tf.json, demo_override.tf and demo_override.tf.json receive special merge handling after normal configuration loading. Multiple override files are processed in lexical order. Avoid them in beginner projects: they make it harder to see the effective declaration.
Check formatting without changing files
In an isolated folder containing reviewed native configuration, terraform fmt -check -diff checks style and displays differences without rewriting files. Plain terraform fmt can rewrite files; -recursive expands the scope to nested directories. A formatting check does not establish semantic validity or successful provisioning. Full validation and the lifecycle commands belong to the next chapter. No CLI command was executed for this lesson.
Practice: classify files in the worksheet as root configuration, nested configuration, variable inputs or documentation. Explain why the native and JSON alternatives cannot coexist under the same resource identity. Related reference: Terraform files and module boundaries.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Terraform files and module boundaries — knowledge check
Check the concepts and decisions introduced in this lesson.
Which files normally belong to the root module?
Does outputs.tf run after main.tf because of its filename?
What happens if main.tf and extra.tf.json define the same resource identity as ordinary files?
How is a nested modules/labels directory included in a root configuration?
Which command checks native formatting without rewriting the file?