Create map object instead of list object

Hello,

I have following variable:

locals {
  instance = {
    foo = {
      replicas      = 3
      instance_type = "m7a.large"
    }
    bar = {
      replicas      = 3
      instance_type = "m7a.xlarge"
    }
  }  
}

And have an output:

output "instance_list" {
  value = flatten([for k, v in local.instance : [
    for i in range(1, v.replicas + 1) : {
      name          = format("%s-%s", k, i)
      instance_type = v.instance_type
    }
  ]])
}

Which produce following:

  + instance_list = [
      + {
          + instance_type = "m7a.xlarge"
          + name          = "bar-1"
        },
      + {
          + instance_type = "m7a.xlarge"
          + name          = "bar-2"
        },
      + {
          + instance_type = "m7a.large"
          + name          = "foo-1"
        },
      + {
          + instance_type = "m7a.large"
          + name          = "foo-2"
        },
    ]

But instead of list I’m need produce map object like this:

  + instance_list = {
      + {
          + instance_type = "m7a.xlarge"
          + name          = "bar-1"
        },
      + {
          + instance_type = "m7a.xlarge"
          + name          = "bar-2"
        },
      + {
          + instance_type = "m7a.large"
          + name          = "foo-1"
        },
      + {
          + instance_type = "m7a.large"
          + name          = "foo-2"
        },
    }

How to achieve this?

You pasted the same thing twice, for what you’re getting, and what you want.

@maxb not same. :slight_smile:

Now: instance_list = [ {}, {}, {}, {} ]
Need: instance_list = { {}, {}, {}, {} }

Ah. But, this syntax is incorrect and has no valid meaning in Terraform.

Hi @igor-nikiforov,

As @maxb noted, what you’ve shown as your desired output is impossible, because the map you’ve illustrated doesn’t have any keys for the elements.

Perhaps you could say more about what you intend to do with this value once you’ve constructed it and then that might clarify what result you actually want even if you aren’t sure how to describe it as hypothetical Terraform plan output.