Skip to content

deep_merge

unitstack

Usage

deep_merge(...maps: any) --> object

deep_merge merges its arguments from first to last. When two arguments set the same key, the later value wins, except that nested maps merge key by key and lists are appended. OpenTofu’s merge replaces a nested map whole.

To merge every JSON file in the unit’s directory into its inputs, expand a list into arguments with ...:

terragrunt.hcl
locals {
json_files = sort(fileset(get_terragrunt_dir(), "*.json"))
config = deep_merge([
for f in local.json_files : jsondecode(file("${get_terragrunt_dir()}/${f}"))
]...)
}
inputs = local.config

Parameters

mapsany

The maps or objects to merge, in order. null arguments are skipped, and any other type is an error.

Examples

terragrunt.hcl
locals {
config = deep_merge(
{ service = { retries = 1, mode = "safe" }, ports = [80] },
{ service = { retries = 3 }, ports = [443] },
)
# { service = { retries = 3, mode = "safe" }, ports = [80, 443] }
}