Problem
When running a terraform apply or terraform plan, the operation fails with the following error message:
Error: Call to function "coalesce" failed: no non-null, non-empty-string arguments.
Cause
The coalesce function takes a series of arguments and returns the first one that is not null and not an empty string. This error occurs when all arguments provided to the function are either null or empty strings, leaving the function with no valid value to return.
Example Configuration Causing the Error
The following configuration demonstrates the problem. Both variables passed to coalesce are invalid (one is null, the other is an empty string), which triggers the error.
variable "mynull" {
default = null
}
variable "myempty" {
default = ""
}
output "mycoalesce" {
value = coalesce(var.mynull, var.myempty)
}Running terraform apply with this configuration produces the error.
$ terraform apply Error: Call to function "coalesce" failed: no non-null, non-empty-string arguments. on main.tf line 13, in output "mycoalesce": 13: value = coalesce(var.mynull, var.myempty)
Solutions
There are two primary approaches to resolve this issue.
Solution 1: Ensure Valid Input
The primary solution is to correct your configuration to ensure that at least one of the arguments passed to the coalesce function is a non-null, non-empty string value. This involves inspecting the data sources, variables, or resource attributes that are being passed to the function and ensuring they produce a valid output.
Solution 2: Provide a Default Fallback Value
If inputs may legitimately be null or empty, you can add a default, non-empty string value as the last argument in the coalesce function. This ensures the function always has a valid value to return if all preceding arguments are invalid.
The following example adds "default-value" as the final argument.
variable "mynull" {
default = null
}
variable "myempty" {
default = ""
}
output "mycoalesce" {
value = coalesce(var.mynull, var.myempty, "default-value")
}Running terraform apply with this updated configuration now succeeds.
$ terraform apply Changes to Outputs: + mycoalesce = "default-value" You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: mycoalesce = "default-value"
Additional Information
- For more details on the function's behavior, refer to the official documentation for the
coalescefunction.