conditional expression
An expression that checks a condition and evaluates to one of two values, one for true and one for false.
In more detail
Languages write it differently: Python uses value_if_true if condition else value_if_false, while Terraform and many other languages use condition ? true_value : false_value. It suits short choices, such as a large size in production and a small one elsewhere. Keep both sides short so the choice stays easy to read.
Example
tier = var.production ? "large" : "small"Related terms
- if statement — A statement that runs a block of code only when a condition holds, optionally with an else block for when it does not.
- expression — A piece of code that Python can evaluate to produce a value, such as 2 + 3, name or len(word).
- Terraform expression — Anything that produces a value on the right of an equals sign: a literal, a reference, a function call, a for expression or a conditional.
- try() — The function that returns the first of its arguments that evaluates without an error, used to supply a fallback.
Taught in
- If Else, And Conditional Expressions — Hands-on Python: Complete Video Course & Book
- Comprehensions — Hands-on Python: Complete Video Course & Book
- Functions And Expressions In HCL — Infrastructure as Code with Terraform
- Expressions And Functions: Making Configuration Compute — HashiCorp Terraform Associate (004) Exam Prep