What is a conditional expression (ternary) in Terraform?
A conditional expression in Terraform chooses one of two values based on a condition. It works like a simple if-else, written as: condition ? value_if_true : value_if_false.
Click to reveal answer
beginner
How do you write a conditional expression to set a variable to "small" if size is less than 10, otherwise "large"?
You write: size < 10 ? "small" : "large". This means if size is less than 10, use "small", else use "large".
Click to reveal answer
intermediate
Can Terraform conditional expressions be nested?
Yes, you can nest conditional expressions inside each other to handle multiple conditions, but keep it simple to avoid confusion.
Click to reveal answer
intermediate
What types of values can be returned by Terraform conditional expressions?
Terraform conditional expressions can return any type: strings, numbers, booleans, lists, or maps, as long as both possible values are the same type.
Click to reveal answer
beginner
Why use conditional expressions in Terraform configurations?
They let you write flexible and reusable code by choosing values based on conditions, like environment type or resource size, without repeating code.
Click to reveal answer
What is the correct syntax for a Terraform conditional expression?
2. Which of these is the correct syntax for a Terraform conditional expression?
easy
A. condition : true_val ? false_val
B. condition ? true_val : false_val
C. condition ? (true_val, false_val)
D. if (condition) then true_val else false_val
Solution
Step 1: Recall Terraform conditional syntax
Terraform uses the format: condition ? true_val : false_val.
Step 2: Compare options
Only condition ? true_val : false_val matches this syntax exactly.
Final Answer:
condition ? true_val : false_val -> Option B
Quick Check:
Correct ternary syntax = D [OK]
Hint: Look for question mark and colon in correct order [OK]
Common Mistakes:
Using if-then-else syntax from other languages
Swapping question mark and colon
Using commas inside the expression
3. Given this Terraform code:
variable "env" { default = "test" }
output "message" {
value = var.env == "prod" ? "Deploy to production" : "Deploy to staging"
}
What will be the output value?
medium
A. Error: unknown variable
B. "Deploy to production"
C. "test"
D. "Deploy to staging"
Solution
Step 1: Check variable value
var.env is set to "test" by default.
Step 2: Evaluate condition
var.env == "prod" is false, so the false branch "Deploy to staging" is chosen.
Final Answer:
"Deploy to staging" -> Option D
Quick Check:
Condition false -> false branch output = A [OK]
Hint: Check variable value before condition evaluation [OK]
Common Mistakes:
Assuming default is "prod"
Confusing true and false branches
Expecting variable value as output
4. Identify the error in this Terraform conditional expression:
output "result" {
value = var.enabled ? "yes" "no"
}
medium
A. Missing colon ':' between true and false values
B. Variable var.enabled is undefined
C. Quotes around values are incorrect
D. Ternary operator not supported in outputs
Solution
Step 1: Review ternary syntax
The correct syntax requires a colon ':' between true and false values.
Step 2: Identify missing colon
The expression has "? \"yes\" \"no\"" missing the ':' separator.
Final Answer:
Missing colon ':' between true and false values -> Option A
Quick Check:
Syntax error due to missing ':' = A [OK]
Hint: Check for '?' and ':' pair in ternary expressions [OK]
Common Mistakes:
Omitting the colon ':'
Misplacing quotes
Assuming ternary not allowed in outputs
5. You want to assign a resource name based on environment, but if the environment variable is empty or null, use "default" instead. Which expression correctly does this in Terraform?