Files
opentf/website/docs/configuration/expressions/conditionals.html.md
Martin Atkins ab1dd87540 website: Elaborate the "expressions" sub-pages in the language section
When we did the earlier documentation rework for Terraform v0.12 we still
had one big "Expressions" page talking about the various operators and
constructs, and so we had to be a bit economical with the details about
some more complicated constructs in order to avoid the page becoming even
more overwhelming.

However, we've recently reorganized the language documentation again so
that the expressions section is split across several separate pages, and
that gives some freedom to go into some more detail about and show longer
examples for certain features.

My changes here are not intended to be an exhaustive rewrite but I did
try to focus on some areas I've commonly seen questions about when helping
in the community forum and elsewhere, and also to create a little more
connectivity between the different content so readers can hopefully find
what they are looking for more easily when they're not yet sure what
terminology to look for.
2020-12-07 15:39:37 -08:00

2.3 KiB

layout, page_title
layout page_title
language Conditional Expressions - Configuration Language

Conditional Expressions

Hands-on: Try the Create Dynamic Expressions tutorial on HashiCorp Learn.

A conditional expression uses the value of a bool expression to select one of two values.

The syntax of a conditional expression is as follows:

condition ? true_val : false_val

If condition is true then the result is true_val. If condition is false then the result is false_val.

A common use of conditional expressions is to define defaults to replace invalid values:

var.a != "" ? var.a : "default-a"

If var.a is an empty string then the result is "default-a", but otherwise it is the actual value of var.a.

Conditions

The condition can be any expression that resolves to a boolean value. This will usually be an expression that uses the equality, comparison, or logical operators.

Result Types

The two result values may be of any type, but they must both be of the same type so that Terraform can determine what type the whole conditional expression will return without knowing the condition value.

If the two result expressions don't produce the same type then Terraform will attempt to find a type that they can both convert to, and make those conversions automatically if so.

For example, the following expression is valid and will always return a string, because in Terraform all numbers can convert automatically to a string using decimal digits:

var.example ? 12 : "hello"

Relying on this automatic conversion behavior can be confusing for those who are not familiar with Terraform's conversion rules though, so we recommend being explicit using type conversion functions in any situation where there may be some uncertainty about the expected result type.

The following example is contrived because it would be easier to write the constant "12" instead of the type conversion in this case, but shows how to use tostring to explicitly convert a number to a string.

var.example ? tostring(12) : "hello"