Process Flow - Why expressions add logic
Start: Define variables
Evaluate condition in 'if'
Use value A
Output final value
Terraform uses expressions with conditions to decide which value to use, adding logic to configuration.
variable "env" { default = "prod" } output "instance_type" { value = var.env == "prod" ? "t3.large" : "t3.micro" }
| Step | Expression Evaluated | Condition Result | Value Chosen | Output |
|---|---|---|---|---|
| 1 | var.env == "prod" | True | "t3.large" | "t3.large" |
| 2 | Output instance_type value | - | - | "t3.large" |
| 3 | End of evaluation | - | - | - |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| var.env | "prod" | "prod" | "prod" |
| instance_type | - | "t3.large" | "t3.large" |
Terraform expressions add logic by using conditional (ternary) operators. Syntax: condition ? value_if_true : value_if_false Evaluates condition once, picks value accordingly. Allows dynamic configuration based on variables. Simplifies infrastructure decisions in code.