Skip to main content
merge(maps...) combines all given maps or objects into one. When multiple arguments contain the same key, the value from the last argument wins.

Signature

merge(maps... map) map

Example

Start with a base set of environment variables and override specific values for the production environment:
variable "env" {
  description = "Target environment"
  type        = string
  default     = "staging"
}

computed "config" {
  description = "Merged environment configuration"
  expression  = merge(
    {
      LOG_LEVEL    = "info"
      CACHE_TTL    = "300"
      MAX_WORKERS  = "4"
    },
    var.env == "production" ? {
      LOG_LEVEL   = "warn"
      MAX_WORKERS = "16"
    } : {}
  )
}

task "run" {
  description = "Start the application with the merged config"
  commands = [
    "LOG_LEVEL=${computed.config["LOG_LEVEL"]} MAX_WORKERS=${computed.config["MAX_WORKERS"]} ./bin/app",
  ]
}
In production, LOG_LEVEL is warn and MAX_WORKERS is 16. In staging, both use the base defaults.

Notes

  • Later arguments override keys from earlier arguments when there are conflicts.
  • merge accepts maps and objects. When merging maps, all maps must have the same value type.
  • merge does not perform deep merging. Nested values are replaced entirely, not merged recursively.