Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the highest precedence source for Terraform input variables?
Command-line flags (e.g., -var or -var-file) have the highest precedence and override all other sources.
Click to reveal answer
intermediate
List the order of precedence for Terraform input variables from highest to lowest.
1. Command-line flags (-var, -var-file)
2. Environment variables (TF_VAR_name)
3. terraform.tfvars files and other tfvars files
4. Default values in variable blocks
Click to reveal answer
beginner
How do environment variables affect Terraform input variables?
Environment variables prefixed with TF_VAR_ set the value of input variables and override values from tfvars files and defaults, but are overridden by command-line flags.
Click to reveal answer
beginner
What happens if a variable is not set by any source in Terraform?
Terraform uses the default value defined in the variable block. If no default is set and the variable is required, Terraform will prompt the user for input.
Click to reveal answer
intermediate
Why is understanding input variable precedence important in Terraform?
It helps avoid unexpected values by knowing which source will override others, ensuring predictable and controlled infrastructure configuration.
Click to reveal answer
Which source has the lowest precedence for Terraform input variables?
AEnvironment variables
BDefault values in variable blocks
Cterraform.tfvars files
DCommand-line flags
✗ Incorrect
Default values in variable blocks have the lowest precedence and are used only if no other source provides a value.
If a variable is set in both an environment variable and a terraform.tfvars file, which value does Terraform use?
AValue from terraform.tfvars file
BValue from default in variable block
CTerraform will throw an error
DValue from environment variable
✗ Incorrect
Environment variables override values set in terraform.tfvars files.
How can you override a Terraform variable value without changing any files?
APass the value using a command-line flag
BUse a terraform.tfvars file
CTerraform does not allow overriding without files
DSet a default value in the variable block
✗ Incorrect
Passing variables via command-line flags overrides all other sources.
What prefix must environment variables have to set Terraform input variables?
ATF_VAR_
BTF_INPUT_
CTERRAFORM_VAR_
DVAR_
✗ Incorrect
Environment variables must be prefixed with TF_VAR_ followed by the variable name.
If a required variable has no default and is not set by any source, what does Terraform do?
AUses a null value
BThrows an error and stops
CPrompts the user to enter a value
DUses an empty string
✗ Incorrect
Terraform prompts the user to input a value for required variables without defaults.
Explain the order in which Terraform applies input variable values from different sources.
Think about which source you would want to override others when running Terraform.
You got /4 concepts.
Describe what happens when a Terraform variable is not set anywhere and has no default value.
Consider how Terraform ensures required variables have values.
You got /3 concepts.
Practice
(1/5)
1. In Terraform, which source has the highest priority when setting the value of an input variable?
easy
A. Terraform variable definition files (.tfvars)
B. Environment variables
C. Command-line flags (CLI flags)
D. Default values in the variable block
Solution
Step 1: Understand Terraform variable precedence
Terraform uses a specific order to decide variable values: CLI flags first, then environment variables, then .tfvars files, and lastly default values.
Step 2: Identify the highest priority source
Since CLI flags are checked first, they override all other sources.
2. Which of the following is the correct way to pass a variable value via CLI flag when running Terraform?
easy
A. terraform apply -var-file=region=us-west-1
B. terraform apply --set region=us-west-1
C. terraform apply -env region=us-west-1
D. terraform apply -var 'region=us-west-1'
Solution
Step 1: Recall CLI flag syntax for variables
The correct syntax to pass a variable via CLI is using -var followed by 'key=value'.
Step 2: Match the correct option
terraform apply -var 'region=us-west-1' uses -var 'region=us-west-1', which is the correct syntax.
Final Answer:
terraform apply -var 'region=us-west-1' -> Option D
Quick Check:
Use -var 'key=value' for CLI variable input [OK]
Hint: Use -var 'key=value' to pass variables via CLI [OK]
Common Mistakes:
Using --set instead of -var
Confusing environment variable syntax with CLI flags
Using -var-file incorrectly for single variables
3. Given the following Terraform variable declaration and usage: <pre>variable "env" {
default = "dev"
}
# Command run:
# terraform apply -var 'env=prod'
# Environment variable:
# TF_VAR_env=staging
# tfvars file content:
# env = "qa"
What value will Terraform use for the variable env during this apply?
medium
A. "prod" (from CLI flag)
B. "dev" (default value)
C. "staging" (from environment variable)
D. "qa" (from tfvars file)
Solution
Step 1: List variable sources and their precedence
Terraform checks CLI flags first, then environment variables, then tfvars files, then defaults.
Step 2: Identify the highest priority value provided
CLI flag sets env=prod, which overrides environment variable (staging), tfvars (qa), and default (dev).
Final Answer:
"prod" (from CLI flag) -> Option A
Quick Check:
CLI flag value "prod" is used [OK]
Hint: CLI flags beat env vars and tfvars files [OK]
Common Mistakes:
Choosing environment variable over CLI flag
Picking tfvars value over environment variable
Assuming default value is used when others exist
4. You run Terraform with the environment variable TF_VAR_region=us-east-1 set, but your configuration uses a region variable with a default value of us-west-2. You also have a terraform.tfvars file setting region = "eu-central-1". However, Terraform still uses us-west-2. What is the most likely cause?
medium
A. You passed a CLI flag overriding all other values
B. The environment variable name is incorrect; it should be TF_REGION
C. The variable is marked as sensitive and ignores external values
D. The terraform.tfvars file is not loaded automatically
Solution
Step 1: Check environment variable naming
TF_VAR_region is correct naming for environment variable to set variable 'region'.
Step 2: Recall precedence order
CLI flags > env vars > tfvars > defaults. Env var us-east-1 should override tfvars and default.
Step 3: Identify cause of default value
To override the env var and use default us-west-2, a CLI flag like -var 'region=us-west-2' must have been passed.
Final Answer:
You passed a CLI flag overriding all other values -> Option A
5. You want to ensure a Terraform variable instance_type always uses the value from a .tfvars file, ignoring any CLI flags or environment variables. Which approach correctly enforces this behavior?
hard
A. Remove default value and only set instance_type in the .tfvars file; avoid passing CLI flags or env vars