terraform.tfvars vs variables.tf: What is the Difference?
TLDR
The variables.tf file defines input variables and their types, while the terraform.tfvars file provides values for those variables. Use variables.tf for declarations and terraform.tfvars for environment-specific values.
Terraform uses variables to make configurations dynamic and reusable. Two common files for managing variables are variables.tf and terraform.tfvars. This guide will explain the difference between these files and how to use them effectively.
What is variables.tf?
The variables.tf file is used to declare input variables and their types. It defines the structure and constraints for variables.
Example: variables.tf
Generally, variables.tf contains variable declarations with descriptions and types. Here's a simple example:
variable "region" {
description = "The AWS region to deploy resources in."
type = string
}
variable "instance_type" {
description = "The type of EC2 instance."
type = string
default = "t2.micro"
}
What is terraform.tfvars?
The terraform.tfvars file is used to provide values for the variables declared in variables.tf. It is typically used for environment-specific configurations.
Example: terraform.tfvars
A terraform.tfvars file contains key-value pairs that correspond to the variables declared in variables.tf. Here's an example:
region = "us-east-1"
instance_type = "t3.medium"
Key Differences
| Feature | variables.tf |
terraform.tfvars |
|---|---|---|
| Purpose | Declares variables and their types | Provides values for variables |
| Required in Project | Yes | No |
| Environment-Specific | No | Yes |
| Default Values | Can define defaults | Overrides defaults |
How Terraform Uses These Files
- Declaration: Terraform reads
variables.tfto understand the variables and their constraints. - Value Assignment: Terraform reads
terraform.tfvarsto assign values to the variables. - Override: Command-line flags or environment variables can override values in
terraform.tfvars.
Best Practices
- Use
variables.tffor Declarations: Keep all variable declarations invariables.tffor consistency. - Use
terraform.tfvarsfor Environment-Specific Values: Store values that change between environments interraform.tfvars. - Avoid Hardcoding: Do not hardcode values in
variables.tf. Useterraform.tfvarsor other methods to provide values. - Document Variables: Add descriptions to all variables in
variables.tffor better maintainability.
By understanding the difference between variables.tf and terraform.tfvars, you can manage variables in Terraform more effectively, making your configurations dynamic and reusable.
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
Variables Within Variables in Terraform
Learn how to use variables within variables in Terraform to create dynamic and reusable configurations.
Terraform State Management
What is Terraform state, why is it important, and how do you manage state in a team environment?
mid
Terraform Repository Structure Checklist
Best practices for organizing and structuring your Terraform projects for maintainability and scalability.
30-45 minutes