2018-05-12 18:39:29 +02:00
|
|
|
---
|
2021-11-23 00:47:36 +01:00
|
|
|
page_title: coalesce - Functions - Configuration Language
|
2018-05-12 18:39:29 +02:00
|
|
|
description: |-
|
2019-04-12 19:57:52 +02:00
|
|
|
The coalesce function takes any number of arguments and returns the
|
|
|
|
first one that isn't null nor empty.
|
2021-11-23 00:47:36 +01:00
|
|
|
sidebar_title: coalesce
|
2018-05-12 18:39:29 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# `coalesce` Function
|
|
|
|
|
2019-04-12 19:57:52 +02:00
|
|
|
`coalesce` takes any number of arguments and returns the first one
|
|
|
|
that isn't null or an empty string.
|
2018-05-12 18:39:29 +02:00
|
|
|
|
2020-11-18 01:05:55 +01:00
|
|
|
All of the arguments must be of the same type. Terraform will try to
|
|
|
|
convert mismatched arguments to the most general of the types that all
|
|
|
|
arguments can convert to, or return an error if the types are incompatible.
|
|
|
|
The result type is the same as the type of all of the arguments.
|
|
|
|
|
2018-05-12 18:39:29 +02:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
> coalesce("a", "b")
|
|
|
|
a
|
|
|
|
> coalesce("", "b")
|
|
|
|
b
|
2019-04-12 19:57:52 +02:00
|
|
|
> coalesce(1,2)
|
|
|
|
1
|
2018-05-12 18:39:29 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
To perform the `coalesce` operation with a list of strings, use the `...`
|
|
|
|
symbol to expand the list as arguments:
|
|
|
|
|
|
|
|
```
|
|
|
|
> coalesce(["", "b"]...)
|
|
|
|
b
|
|
|
|
```
|
|
|
|
|
2020-11-18 01:05:55 +01:00
|
|
|
Terraform attempts to select a result type that all of the arguments can
|
|
|
|
convert to, so mixing argument types may produce surprising results due to
|
|
|
|
Terraform's automatic type conversion rules:
|
|
|
|
|
|
|
|
```
|
|
|
|
> coalesce(1, "hello")
|
|
|
|
"1"
|
|
|
|
> coalesce(true, "hello")
|
|
|
|
"true"
|
|
|
|
> coalesce({}, "hello")
|
|
|
|
|
|
|
|
Error: Error in function call
|
|
|
|
|
|
|
|
Call to function "coalesce" failed: all arguments must have the same type.
|
|
|
|
```
|
|
|
|
|
2018-05-12 18:39:29 +02:00
|
|
|
## Related Functions
|
|
|
|
|
2021-11-23 00:47:36 +01:00
|
|
|
* [`coalescelist`](/language/functions/coalescelist) performs a similar operation with
|
2019-04-12 19:57:52 +02:00
|
|
|
list arguments rather than individual arguments.
|