Terraform Infrastructure as CodeTerraformTerraform Configuration

Article & Tutorial

Terraform Files Explained: main.tf, variables.tf, outputs.tf, and terraform.tfvars

Learn the purpose of main.tf, variables.tf, outputs.tf, terraform.tfvars, versions.tf and providers.tf in Terraform projects.

Organized infrastructure configuration files producing a cloud resource plan
Organized infrastructure configuration files producing a cloud resource plan
Contents

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.

example
terraform fmt -check -recursive
terraform init
terraform validate
terraform plan

Related Melochip article: https://melochip.com/posts/terraform-variables-tfvars-explained. This illustration has not been independently executed.

Read related Terraform guide

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.

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.