0
0
Terraformcloud~10 mins

Conditional expressions (ternary) in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Conditional expressions (ternary)
Evaluate condition
Use true
Result assigned
The ternary expression checks a condition and picks one of two values based on whether the condition is true or false.
Execution Sample
Terraform
variable "env" {
  default = "prod"
}

output "instance_type" {
  value = var.env == "prod" ? "t3.large" : "t3.micro"
}
This Terraform code sets the instance type based on the environment variable using a ternary conditional expression.
Process Table
StepConditionCondition ResultValue if TrueValue if FalseChosen Value
1var.env == "prod"True"t3.large""t3.micro""t3.large"
2var.env == "dev"False"t3.large""t3.micro""t3.micro"
3var.env == "test"False"t3.large""t3.micro""t3.micro"
4var.env == "prod"True"t3.large""t3.micro""t3.large"
💡 Execution stops after evaluating the condition and choosing the value based on true or false.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
var.env"prod""prod""dev""test""prod"
Chosen ValueN/A"t3.large""t3.micro""t3.micro""t3.large"
Key Moments - 2 Insights
Why does the chosen value change when var.env changes?
Because the condition is evaluated each time with the current var.env value, the ternary picks the true or false value accordingly as shown in execution_table rows 1-4.
What happens if the condition is neither true nor false?
The condition must always evaluate to true or false; Terraform will error if it doesn't. The ternary requires a boolean condition as shown in the condition column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the chosen value at step 2?
A"t3.large"
Bnull
C"t3.micro"
D"prod"
💡 Hint
Check the 'Chosen Value' column in row 2 of the execution_table.
At which step does the condition evaluate to true?
AStep 1
BStep 3
CStep 2
DNone
💡 Hint
Look at the 'Condition Result' column in the execution_table.
If var.env is set to "dev", what would the chosen value be?
A"t3.large"
B"t3.micro"
C"dev"
Dnull
💡 Hint
Refer to variable_tracker for var.env and chosen value changes.
Concept Snapshot
Terraform conditional expressions use the syntax: condition ? true_value : false_value
They evaluate the condition once and return true_value if true, else false_value.
Useful for setting resource properties based on variables.
Condition must be boolean.
Example: var.env == "prod" ? "t3.large" : "t3.micro"
Full Transcript
This visual execution shows how Terraform's conditional expressions (ternary) work. The condition is checked first. If true, the expression returns the first value; if false, it returns the second. We traced var.env with values like "prod", "dev", and "test". When var.env is "prod", the condition is true, so "t3.large" is chosen. For other values, the condition is false, so "t3.micro" is chosen. This helps set resource properties dynamically. The execution table shows each step's condition, result, and chosen value. The variable tracker shows how var.env and the chosen value change over steps. Key moments clarify why the chosen value depends on var.env and that the condition must be boolean. The quiz tests understanding of these steps and values. The snapshot summarizes the syntax and behavior for quick reference.