Bootstrap FreeKB - Terraform - Merge dictionaries
Terraform - Merge dictionaries

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter b39516 in the box below so that we can be sure you are a human.