Terraform - Merge dictionaries

by
Jeremy Canfield |
Updated: July 19 2023
| Terraform articles
merge can be used to combine dictionaries. Let's say you have the following files on your Terraform server.
├── main.tf (root module)
├── data.tf
├── locals.tf
├── outputs.tf
├── providers.tf
├── terraform.tfstate
├── variables.tf
├── child (directory)
│ ├── main.tf (child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
Let's say locals.tf file has the following.
locals {
fruit = {
a = "apple"
b = "banana"
}
veggy = {
c = "carrot"
d = "pepper"
}
}
And let's say outputs.tf in the same directory as your main root module (main.tf) has the following. In this example, the local.fruit and local.veggy dictionaries will be merged.
output "my-food" {
value = merge(local.fruit,local.veggy)
}
The terraform output command should return the following.
my-food = {
"a" = "apple"
"b" = "banana"
"c" = "carrot"
"d" = "pepper"
}
Did you find this article helpful?
If so, consider buying me a coffee over at