terraform.tfvars vs variables.tf difference

2 min read 06-10-2024
terraform.tfvars vs variables.tf difference


Terraform: The Difference Between terraform.tfvars and variables.tf

Terraform, the powerful infrastructure-as-code tool, allows you to define and manage your infrastructure using code. But with great power comes the need for organization and flexibility. This is where terraform.tfvars and variables.tf come into play, offering distinct ways to manage your Terraform variables.

The Scenario: Defining Your Cloud Resources

Imagine you're building a new application and need to provision a virtual machine instance on Google Cloud Platform (GCP). You'll likely need to define several variables such as the instance name, machine type, and network interface. In Terraform, you can define these variables in either a terraform.tfvars file or a variables.tf file.

The Original Code: Defining Variables

# variables.tf
variable "instance_name" {
  type = string
  description = "Name of the VM instance"
}

variable "machine_type" {
  type = string
  description = "Machine type for the instance"
}
# terraform.tfvars
instance_name = "my-app-instance"
machine_type = "n1-standard-1"

This code snippet defines two variables, instance_name and machine_type, within a variables.tf file. Their values are then assigned in a separate terraform.tfvars file.

The Difference: Clarity and Flexibility

While both files store variables, they serve different purposes:

  • variables.tf: This file acts as your variable declaration center. It defines the variable names, types, and descriptions. This makes your code more readable and easier to understand. You can also specify default values for your variables in variables.tf.
  • terraform.tfvars: This file stores the actual values of your variables. It's where you set the specific configurations for your infrastructure, making it a highly practical place to store secrets and sensitive information. It can also be used to define environment-specific settings by creating separate terraform.tfvars files for different environments (e.g., dev.tfvars, prod.tfvars).

Analyzing the Benefits:

  • Organization: Using separate files for variable declaration and assignment enhances the organization of your Terraform code.
  • Flexibility: terraform.tfvars allows you to easily change variable values without modifying your code. This is particularly useful for managing environment-specific configurations.
  • Security: terraform.tfvars files can be excluded from version control systems, keeping sensitive information secure.

Further Clarification:

You can also use Terraform's -var flag during the terraform apply command to directly pass variables at runtime:

terraform apply -var="instance_name=my-app-instance" -var="machine_type=n1-standard-1"

This provides a more flexible approach for scenarios where variable values might change frequently.

Conclusion:

variables.tf and terraform.tfvars work together to provide a robust and flexible system for defining and managing your Terraform variables. Understanding their differences and benefits will allow you to write cleaner, more organized, and more secure Terraform code.

Resources: