Terraform Files Explained
Learn the purpose of main.tf , variables.tf , outputs.tf , terraform.tfvars , and other common Terraform files used in real projects.
Quick answer
main.tf
Main resources, providers, and infrastructure definitions.
variables.tf
Input variables used to make configuration reusable.
outputs.tf
Values Terraform should show after apply.
terraform.tfvars
Environment-specific values for variables.
Common Terraform project structure
terraform-project/ main.tf variables.tf outputs.tf terraform.tfvars versions.tf providers.tf
main.tf example
resource "random_pet" "server" { length = 2 }
variables.tf example
variable "environment" { type = string description = "Deployment environment" default = "dev" }
outputs.tf example
output "server_name" { value = random_pet.server.id }
terraform.tfvars example
environment = "staging"
Best practices
- Keep file names predictable.
- Do not put secrets in tfvars files committed to Git.
- Use variables for values that change by environment.
- Use outputs for values needed by users or other systems.
- Use versions.tf for Terraform and provider version constraints.
Related guides
- Terraform Learning Path
- Terraform Providers Explained
Understand which Terraform files can expose secrets
Terraform normally loads all .tf files in the active module directory, not only main.tf; organizing files by role helps readers but does not create independent modules. Generated state, plan files and untracked tfvars may contain sensitive data; configure repository ignores and remote state access correctly.
terraform fmt -check -recursive
terraform init
terraform validate
terraform planRelated Melochip article: https://melochip.com/posts/terraform-variables-tfvars-explained. This illustration has not been independently executed.
Practical guidance and verification
The purpose of each file
Terraform reads .tf configuration files in the same directory as one module, regardless of whether a file is called main.tf or network.tf. Common conventions put resources in main.tf, input declarations in variables.tf, outputs in outputs.tf and provider requirements in versions.tf. Keep a readable root module rather than splitting files for no practical reason.
Build a maintainable layout
Group related configuration by concern and extract modules when a capability is genuinely reusable. Commit the dependency lockfile for consistent provider selection. Exclude state files, .terraform directories and private tfvars files from Git. Keep a non-secret example input file for onboarding and record how CI selects the backend and account.
Use outputs responsibly
Outputs expose useful resource identifiers to tooling or dependent modules but may carry sensitive information. Mark sensitive outputs appropriately and restrict access to the backend because masking does not remove values from state. After rearranging files, run terraform fmt, init, validate and plan to detect broken references. See https://developer.hashicorp.com/terraform/language/files.
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026Related articles
Learn Docker environment variables and secrets, including docker run -e, env files, Compose environment, secre...
3 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes monitoring and logs with kubectl logs, events, metrics-server, top commands, Prometheus, Graf...
6 min read · Updated Nov 16, 2026
Learn Kubernetes Ingress with controllers, IngressClass, host and path routing, TLS, DNS, annotations, redirec...
8 min read · Updated Nov 16, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.