Combine two objects

Hi all. I wanted to ask you for some help.

I have several maps of objects:

locals {
  obj_a = {
    table_name1 = {
      description = ""
      routes = [
        {
          destination_prefix = "127.0.0.1/24"
          next_hop_address   = "127.0.0.1"
        },
        {
          destination_prefix = "127.0.0.2/24"
          next_hop_address   = "127.0.0.2"
        },
       {...}
      table_name2 = {...}

locals {
  obj_b = {
    table_name3 = {
      description = ""
      routes = [
        {
          destination_prefix = "127.0.0.1/24"
          next_hop_address   = "127.0.0.1"
        },
        {...}
      table_name4 = {...}

is there any possibility to combine this in one variable? something like that:

module "route_tables" {
  source = "git@"

  table = local.obj_a, local.obj_b
}

function concat did not help :frowning:

Thanks in advance

Hi @mr-nsh,

The answer here will depend on what exactly you mean by “combine”. The concat function can combine lists in the sense of concatenating them together in the given order, but that doesn’t make sense for maps which have no order.

Can you show an example of what you’d expect the final value to look like after being combined in the way you imagine? Then we can talk about ways to get that result automatically, instead of hard-coding it.

Final value as a sequence or union of the contents of two or more local.obj_#

those. I can’t figure out how to pass multiple local.obj as value var.table

Thanks.

Hi @mr-nsh,

Please write out the exact value you intend to produce using the Terraform language syntax. Then I can tell you one or more options for how to produce that result in terms of local.obj_a and local.obj_b.

Hi @apparentlymart ,

it would be convenient to just list local.obj separated by commas so that the module would process them one by one:

table = local.obj_a, local.obj_b

but if we need to form a third object, then it should look like this:

locals {
  obj_result = {
    table_name1 = {
      description = ""
      routes = [
        {
          destination_prefix = "127.0.0.1/24"
          next_hop_address   = "127.0.0.1"
        },
        {
          destination_prefix = "127.0.0.2/24"
          next_hop_address   = "127.0.0.2"
        },
       {...}
      table_name2 = {...}
      table_name3 = {
      description = ""
      routes = [
        {
          destination_prefix = "127.0.0.1/24"
          next_hop_address   = "127.0.0.1"
        },
        {...}
      table_name4 = {...}
      }
    }
  }
}