github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/website/docs/language/expressions/conditionals.mdx (about)

     1  ---
     2  page_title: Conditional Expressions - Configuration Language
     3  description: >-
     4    Conditional expressions select one of two values. You can use them to define
     5    defaults to replace invalid values.
     6  ---
     7  
     8  # Conditional Expressions
     9  
    10  A _conditional expression_ uses the value of a boolean expression to select one of
    11  two values.
    12  
    13  > **Hands-on:** Try the [Create Dynamic Expressions](https://learn.hashicorp.com/tutorials/terraform/expressions?in=terraform/configuration-language&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial.
    14  
    15  ## Syntax
    16  
    17  The syntax of a conditional expression is as follows:
    18  
    19  ```hcl
    20  condition ? true_val : false_val
    21  ```
    22  
    23  If `condition` is `true` then the result is `true_val`. If `condition` is
    24  `false` then the result is `false_val`.
    25  
    26  A common use of conditional expressions is to define defaults to replace
    27  invalid values:
    28  
    29  ```
    30  var.a != "" ? var.a : "default-a"
    31  ```
    32  
    33  If `var.a` is an empty string then the result is `"default-a"`, but otherwise
    34  it is the actual value of `var.a`.
    35  
    36  ## Conditions
    37  
    38  The condition can be any expression that resolves to a boolean value. This will
    39  usually be an expression that uses the equality, comparison, or logical
    40  operators.
    41  
    42  ### Custom Condition Checks
    43  
    44  You can create conditions that produce custom error messages for several types of objects in a configuration. For example, you can add a condition to an input variable that checks whether incoming image IDs are formatted properly.
    45  
    46  Custom conditions can help capture assumptions, helping future maintainers understand the configuration design and intent. They also return useful information about errors earlier and in context, helping consumers more easily diagnose issues in their configurations.
    47  
    48  Refer to [Custom Condition Checks](/language/expressions/custom-conditions#input-variable-validation) for details.
    49  
    50  ## Result Types
    51  
    52  The two result values may be of any type, but they must both
    53  be of the _same_ type so that Terraform can determine what type the whole
    54  conditional expression will return without knowing the condition value.
    55  
    56  If the two result expressions don't produce the same type then Terraform will
    57  attempt to find a type that they can both convert to, and make those
    58  conversions automatically if so.
    59  
    60  For example, the following expression is valid and will always return a string,
    61  because in Terraform all numbers can convert automatically to a string using
    62  decimal digits:
    63  
    64  ```hcl
    65  var.example ? 12 : "hello"
    66  ```
    67  
    68  Relying on this automatic conversion behavior can be confusing for those who
    69  are not familiar with Terraform's conversion rules though, so we recommend
    70  being explicit using type conversion functions in any situation where there may
    71  be some uncertainty about the expected result type.
    72  
    73  The following example is contrived because it would be easier to write the
    74  constant `"12"` instead of the type conversion in this case, but shows how to
    75  use [`tostring`](/language/functions/tostring) to explicitly convert a number to
    76  a string.
    77  
    78  ```hcl
    79  var.example ? tostring(12) : "hello"
    80  ```