Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Terraform variable precedence and priority

This is a informational article on how the variable precedence works in Terraform. This is how the priority is defined for variable when you use it in different ways. In the following diagram, it shows the information of variable priority matrix. Lower the number it has higher the priority and it will always override the sub variables you defined. This documentation will help you to troubleshoot, variable related inforamation.

terraform variable precedence and priority.png

I have here five different ways to pass variables in terraform. From bottom providing default value in variable block variable "name" { default = "dev"}. It is most basic and first variable information you provide in terraform as variable block. It has lowest priority and it will be superseded by Environment Variables, when you define variable $env: in windows PowerShell or export in Linux. Whatever the variable name is you need to prefix TF_VAR in front for the variable name. In my case it is $env:TF_VAR_name="dev4".

In the following priority matrix, variable defined inside file *.terraform.tfvars or *.terraform.tfvars.json (Just a note: json file has less priority than non json and will be superseded by normal tfvars file extension)

Next in the line matrix variables mentioned inside *.auto.tfvars file will always have higher priority and will override higher number variables as per the above diagram.

This is the last variable you can provide in -var as a parameters to terraform with key pair value. It will take over all the variables in the line and has highest supremacy.

Useful Articles
Terraform passing different credentials to different subscriptions with provider alias
Using terraform to clone a virtual machine on VMware vSphere infrastructure
Terraform module clone VMware vSphere Linux and Windows virtual machine
Terraform VMware vSphere Virtual Machine customization clone failed on Windows
Terraform VMware vSphere Virtual Machine cloning Operating system not found

Go Back

Comment

Blog Search

Page Views

12085891

Follow me on Blogarama