0
0
Terraformcloud~5 mins

Input variable precedence order in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform uses variables to customize infrastructure. Sometimes, the same variable can be set in different places. Knowing which setting Terraform uses helps avoid confusion and errors.
When you want to override a default variable value without changing the code.
When you run Terraform on different machines and want different settings on each.
When you want to test changes by temporarily changing a variable value.
When you use automation tools that pass variables to Terraform.
When you want to keep sensitive values out of your main configuration files.
Config File - variables.tf
variables.tf
variable "region" {
  description = "The cloud region to deploy resources"
  type        = string
  default     = "us-east-1"
}

variable "instance_type" {
  description = "Type of instance to use"
  type        = string
  default     = "t2.micro"
}

This file defines two variables: region and instance_type. Each has a default value. These defaults are used if no other value is provided.

Commands

This command runs Terraform to create resources. It sets the region and instance_type variables directly on the command line, which overrides defaults and other settings.

Terminal
terraform apply -var='region=us-west-2' -var='instance_type=t3.small' -auto-approve
Expected OutputExpected
Acquiring state lock. This may take a few moments... Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + instance_type = "t3.small" + region = "us-west-2" Do you want to perform these actions? Terraform will perform the actions described above. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-var - Set variable values directly on the command line
-auto-approve - Skip interactive approval prompt

This command runs Terraform using a variable file named custom.tfvars. Values in this file override defaults but are overridden by command line variables.

Terminal
terraform apply -var-file=custom.tfvars -auto-approve
Expected OutputExpected
Acquiring state lock. This may take a few moments... Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + instance_type = "t2.medium" + region = "us-east-2" Do you want to perform these actions? Terraform will perform the actions described above. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-var-file - Load variables from a file
-auto-approve - Skip interactive approval prompt

This command runs Terraform without extra variable settings. It uses default values from the variable definitions.

Terminal
terraform apply -auto-approve
Expected OutputExpected
Acquiring state lock. This may take a few moments... Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + instance_type = "t2.micro" + region = "us-east-1" Do you want to perform these actions? Terraform will perform the actions described above. Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip interactive approval prompt
Key Concept

If you remember nothing else from this pattern, remember: command line variables override variable files, which override default values in the code.

Common Mistakes
Setting a variable in a .tfvars file but also setting it on the command line and expecting the file value to be used.
Terraform uses the command line variable value first, ignoring the .tfvars file for that variable.
Use only one method to set a variable or understand that command line variables have higher priority.
Not providing a value for a variable without a default and expecting Terraform to use a default.
Terraform will fail because it requires a value for variables without defaults.
Always provide a value via command line, variable file, or default for required variables.
Summary
Terraform variables can be set in multiple places with a clear priority order.
Command line variables have the highest priority and override variable files and defaults.
Variable files override default values defined in the Terraform code.