✨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.
- Sensitive values (e.g., API keys) can be passed via
For Example: you want different variations of oats:
Post-Workout Oats → Extra protein, fewer carbs.
Breakfast Oats → Balanced with nuts and honey.
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?
Aspect | variable.tf | terraform.tfvars |
Purpose | Declares variables and their attributes. | Assigns values to declared variables. |
Content | Variable definitions (name, type, default). | Key-value pairs for variable values. |
Usage | Defines what variables exist. | Provides actual values for variables. |
File Type | Terraform configuration file. | Variable definitions file. |
Example | variable "instance_type" { type = string } | instance_type = "t2.large" |
Common Mistakes & Best Practices
Common Mistakes
Forgetting to Assign Values in
terraform.tfvars
Mistake: Defining variables in
variables.tf
but not assigning values interraform.tfvars
.Fix: Always provide values in
terraform.tfvars
or use default values invariables.tf
.
Hardcoding Values in
variables.tf
Instead of Usingterraform.tfvars
Mistake: Assigning static values in
variables.tf
, making code less flexible.Fix: Keep
variables.tf
for definitions and useterraform.tfvars
for real values.
Not Using Descriptive Variable Names
Mistake: Naming a variable
var1
instead ofinstance_type
.Fix: Use clear, meaningful names to improve readability.
Committing
terraform.tfvars
to GitMistake: 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.
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 invariables.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.