Terraform Variables Precedence and Order

In this blog post, we will discuss Terraform variable precedence and order in detail to help you manage your infrastructure more efficiently.

When working with Terraform, it’s essential to understand how variables are prioritized and how they affect your infrastructure as code (IaC).

Understanding Terraform Variables

Terraform variables allow you to customize your infrastructure configuration by providing input values. They help reduce hard-coded values and promote reusability across multiple environments. There are several ways to declare and use variables in Terraform, and understanding the precedence of these variables will help you avoid conflicts and unexpected behavior.

Types of Terraform Variables

  1. Input Variables: Input variables serve as parameters for a Terraform module, allowing users to customize aspects of the module without modifying its main configuration.
  2. Output Variables: Output variables return values from a module, making it easy to extract data from one module and use it in another.
  3. Local Variables: Local variables are limited to a specific module and can be used to simplify complex expressions or avoid duplication.

Terraform Variable Precedence and Order

Terraform evaluates variables according to a defined order of precedence, which determines which value will be used if a variable is declared in multiple locations. The following is the order of precedence for variable assignment:

  1. Environment variables
  2. Terraform.tfvars file and .auto.tfvars files
  3. Variable defaults in the module configuration

Let’s take a closer look at each of these methods.

Environment Variables

Environment variables are the highest priority source for setting Terraform input variables. You can set environment variables in the format TF_VAR_<variable_name> in your system. Terraform automatically detects and applies these values, making them useful for CI/CD pipelines or other automated processes.

Terraform.tfvars and .auto.tfvars Files

Terraform automatically loads values from terraform.tfvars and .auto.tfvars files. These files enable you to set input variable values without modifying the main configuration file.

  • terraform.tfvars: Terraform loads values from this file if it exists in the root module directory. The file must be named exactly terraform.tfvars and use the HCL syntax.
  • .auto.tfvars: Terraform loads values from any file with the .auto.tfvars extension. Unlike terraform.tfvars, these files can have any name, allowing you to organize your variables more easily.

If there are conflicting values in these files, Terraform will use the last value defined in alphabetical order.

Variable Defaults in Module Configuration

Variable defaults are the lowest priority source for setting input variable values. You can define a default value in the variable block of your module configuration using the default argument. If no other value is specified, Terraform will use the default value.


variable "region" {
  type    = string
  default = "us-west-2"


Understanding Terraform variable precedence and order is crucial for managing your infrastructure as code effectively. By mastering these concepts, you can avoid conflicts and unexpected behavior, making your IaC deployments more reliable and maintainable. Remember that environment variables take precedence, followed by values from terraform.tfvars and .auto.tfvars files, and finally, default values in the module configuration.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.