Demystifying Terraform: Variables.tf vs Terraform.tfvars
Terraform, a powerful infrastructure-as-code tool, offers a plethora of features for managing your infrastructure. But amidst the plethora of configuration files, two often cause confusion: variables.tf
and terraform.tfvars
. This article aims to clarify the differences between these files and provide a comprehensive understanding of their purpose.
Understanding the Need for Variables
Let's imagine you're deploying a virtual machine (VM) to the cloud using Terraform. This VM requires details like its name, region, and instance type. Instead of hardcoding these values directly in your Terraform configuration files, it's much better to use variables. Variables allow you to:
- Reduce code repetition: Reuse the same configuration for multiple VMs with different values.
- Improve maintainability: Easily modify variables without altering the core Terraform code.
- Enhance security: Store sensitive information separately from your code, like passwords and API keys.
Variables.tf: The Variable Declaration
variables.tf
is the file where you declare your Terraform variables. It acts as a blueprint, defining the name, type, and optional default value for each variable. Here's a simple example:
variable "instance_type" {
type = string
default = "t2.micro"
}
variable "region" {
type = string
default = "us-east-1"
}
In this example, we define two variables: instance_type
and region
. Both are of type string
and have default values. This ensures that even if we don't provide values explicitly, Terraform will still have defaults to use.
Terraform.tfvars: Providing Variable Values
terraform.tfvars
is where you supply the actual values for the variables declared in variables.tf
. This file is separate from your main Terraform configuration, ensuring that sensitive data is not directly included in your code.
Here's an example of how to use terraform.tfvars
to assign values to our variables:
instance_type = "t3.small"
region = "eu-west-1"
This terraform.tfvars
file overrides the default values set in variables.tf
.
Key Differences:
- Location:
variables.tf
resides within your Terraform code, whileterraform.tfvars
sits alongside your main configuration files. - Purpose:
variables.tf
declares variables, defining their type and default values.terraform.tfvars
assigns specific values to these variables. - Security:
terraform.tfvars
is a good practice for storing sensitive data separately from the main configuration files.
Additional Tips
- Variable Scope: Variables declared in
variables.tf
can be used throughout your entire Terraform configuration. - Environment Variables: You can use environment variables to set values for your variables in
terraform.tfvars
using the syntaxvar.environment_variable_name
. - Multiple tfvars Files: You can have multiple
tfvars
files for different environments, likedev.tfvars
,prod.tfvars
, and use-var-file
flag during Terraform execution to specify the appropriate file.
Conclusion
Understanding the role of variables.tf
and terraform.tfvars
is crucial for effective Terraform management. By leveraging these files, you can create modular, flexible, and secure Terraform configurations. This practice promotes reusability, minimizes code duplication, and facilitates easy updates and deployments.
References: