Understanding variable.tf and terraform.tfvars in Terraform

Tf Series

✨When working with Terraform, initially I found variables.tf and terraform.tfvars a bit confusing. So, I decided to break it down in a way that makes sense to me, with an oat meal example🤤✨

What : Are variables.tf and terraform.tfvars?

Think of Terraform as your meal-prep plan for infrastructure. You don’t want to rewrite everything each time you make oats—you just want to define a base recipe and change the ingredients as needed.

  • variables.tf → This is your oats recipe card that lists all the ingredients you might need.

  • terraform.tfvars → This is your meal plan where you specify how much of each ingredient to use for a particular day.

Lets take and example of High-Protein Oats Preparation

  • variables.tf = A list of possible ingredients: oats, protein powder, milk, nuts, honey, fruits.

  • terraform.tfvars = The specific recipe for today: 50g oats, 1 scoop whey, 200ml almond milk, 10 almonds, 1 tsp honey.


Why :

  • Separation of Concerns:

    • variable.tf defines what variables exist.

    • terraform.tfvars defines what values those variables should take.

  • Reusability:

    • Variables make your Terraform code reusable across different environments (e.g., dev, staging, prod).
  • Security:

    • Sensitive values (e.g., API keys) can be passed via terraform.tfvars without hardcoding them in the main configuration.

For Example: you want different variations of oats:

  1. Post-Workout Oats → Extra protein, fewer carbs.

  2. Breakfast Oats → Balanced with nuts and honey.

  3. Light Evening Oats → Less quantity, more fiber.

Instead of writing a new recipe each time, you keep a standard variables list (variables.tf) and just update the specific meal plan (terraform.tfvars) based on your needs. This is exactly how Terraform works—you write reusable code and change values as needed.


When :

  • Use variable.tf:

    • When you need to define variables for your Terraform configuration.

    • When you want to enforce types (e.g., string, number, list) or add descriptions for better documentation.

  • Use terraform.tfvars:

    • When you want to provide specific values for variables.

    • When you want to manage environment-specific configurations (e.g., different instance types for dev and prod).

How :

Step 1: Define Variables in variables.tf

variable "protein_source" {
  description = "The type of protein to use in oats"
  type        = string
  default     = "whey"
}

Step 2: Provide Values in terraform.tfvars

protein_source = "pea protein"

Step 3: Run Terraform Commands

terraform init
terraform apply

What’s the Difference?

Aspectvariable.tfterraform.tfvars
PurposeDeclares variables and their attributes.Assigns values to declared variables.
ContentVariable definitions (name, type, default).Key-value pairs for variable values.
UsageDefines what variables exist.Provides actual values for variables.
File TypeTerraform configuration file.Variable definitions file.
Examplevariable "instance_type" { type = string }instance_type = "t2.large"

Common Mistakes & Best Practices

Common Mistakes

  1. Forgetting to Assign Values in terraform.tfvars

    • Mistake: Defining variables in variables.tf but not assigning values in terraform.tfvars.

    • Fix: Always provide values in terraform.tfvars or use default values in variables.tf.

  2. Hardcoding Values in variables.tf Instead of Using terraform.tfvars

    • Mistake: Assigning static values in variables.tf, making code less flexible.

    • Fix: Keep variables.tf for definitions and use terraform.tfvars for real values.

  3. Not Using Descriptive Variable Names

    • Mistake: Naming a variable var1 instead of instance_type.

    • Fix: Use clear, meaningful names to improve readability.

  4. Committing terraform.tfvars to Git

    • Mistake: Storing sensitive data like passwords in terraform.tfvars and pushing it to Git.

    • Fix: Add terraform.tfvars to .gitignore and use environment variables or secret managers for sensitive values.

  5. Ignoring Precedence of Variable Values

    • Mistake: Defining the same variable in multiple places without knowing which one Terraform will use.

    • Fix: Understand the order of precedence (CLI > environment variables > terraform.tfvars > default values in variables.tf).


Best Practices

♾️variables.tf for structure and terraform.tfvars for values – Keeps code modular and reusable.
♾️terraform.tfvars for different environments Example: terraform.dev.tfvars, terraform.prod.tfvars.
♾️ Follow naming conventions – Keep variable names meaningful and easy to understand.
♾️ Use default values wisely – Set defaults only when they make sense, but allow overrides via terraform.tfvars.
♾️ Secure sensitive values – Use .gitignore, environment variables, or a secret manager for passwords and API keys.


This blog is my intake on variables.tf and .tfvars. Moreover, there are other data type we can explore like string, bool, numbers, list, map in terraform for deeper understanding and experiment along with it.