Skip to main content
coalesce(vals...) evaluates its arguments in order and returns the first one that is not null.

Signature

coalesce(vals... any) any
All arguments must have the same type.

Example

Pick the deployment region from multiple variable sources, falling back through a priority chain:
variable "target_region" {
  description = "Explicit deployment region (optional)"
  type        = string
  default     = null
}

variable "default_region" {
  description = "Fallback region from team defaults (optional)"
  type        = string
  default     = null
}

computed "region" {
  description = "Resolved deployment region"
  expression  = coalesce(var.target_region, var.default_region, "us-east-1")
}

task "deploy" {
  description = "Deploy to the resolved region"
  commands    = ["aws ecs update-service --region ${computed.region} --cluster prod --service api --force-new-deployment"]
}
If --var target_region=eu-west-1 is passed, that value is used. If only default_region is set, that is used. Otherwise us-east-1 is the fallback.

Notes

  • coalesce skips null values. It does not skip empty strings, false, or 0.
  • If all arguments are null, coalesce returns an error.
  • coalesce works on scalar values. For picking the first non-empty list, use concat with compact.