Chapter 1 · Practice 1 of 4
Use the standard file layout for every root module
From Structure and style
Why it matters
When every configuration splits its blocks the same way, people find things without searching. The Terraform style guide recommends:
| File | Holds |
|---|---|
terraform.tf | One terraform block with required_version and required_providers |
providers.tf | All provider blocks |
backend.tf | The backend configuration |
main.tf | Resources and data sources |
variables.tf | Variables, in alphabetical order |
outputs.tf | Outputs, in alphabetical order |
locals.tf | Locals used across files |
As a configuration grows, split main.tf by logical group such as network.tf, storage.tf and compute.tf rather than letting one file reach thousands of lines.
Do
- Keep exactly one
terraformblock, interraform.tf - Split large
main.tffiles by area: network, storage, compute
Avoid
- Scattering provider blocks across several files