In Terraform, multiple sources can provide values for input variables. Which source has the highest precedence when determining the final value of a variable?
Think about which method directly overrides others when running Terraform commands.
Values passed directly via the command line using -var have the highest precedence and override values from tfvars files, environment variables, and defaults.
Arrange the following Terraform variable sources from highest to lowest precedence:
- Default value in variable block
- Environment variable with TF_VAR_ prefix
- Command line -var option
- terraform.tfvars file
Remember that command line options override environment variables, which override tfvars files, which override defaults.
The correct order from highest to lowest precedence is: command line -var option, environment variables with TF_VAR_ prefix, terraform.tfvars file, then default values.
You have a variable region with a default value of us-east-1. You set an environment variable TF_VAR_region=us-west-2 and also pass -var='region=eu-central-1' on the command line. What region will Terraform use during the plan?
Which input source has the highest priority?
The command line -var option overrides environment variables and default values, so Terraform uses eu-central-1.
Which Terraform variable input method is the safest to store sensitive data like passwords, considering variable precedence and exposure risk?
Consider which method avoids exposing secrets in command history or version control.
Using environment variables with TF_VAR_ prefix is safer because it avoids storing secrets in files or command history, reducing exposure risk.
You have a root module and a child module. The root module passes a variable instance_type with value t3.micro via -var. The child module has a variable instance_type with a default t2.small. The root module also sets instance_type in a terraform.tfvars file as t3a.small. Which instance_type value will the child module receive during apply?
Remember how variable values are passed from root to child modules and the precedence of input sources.
The root module's command line -var option overrides the terraform.tfvars file and default values. The child module receives the value passed from the root module, which is t3.micro.