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

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.