# Create a map from two lists

**URL:** <https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580>\
**Category:** Terraform\
**Created:** [April 11, 2020, 5:33am UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580 "2020-04-11T05:33:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hadens](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/hadens/32/5949_2.png) [@hadens](https://discuss.hashicorp.com/u/hadens)\
**Post date:** [April 11, 2020, 5:33am UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/1 "2020-04-11T05:33:02Z")

</div>

Hi,

I’d appreciate any help I can get. I’ve been stuck on this for days and can’t seem to figure it out. My goal is to create an output like this:

**Goal:**

```
Outputs:    

ec2_instances = [
      "i-07a8ed7dda10ecc12": 35.155.XXX.XX,
      "i-06e93292096ad880f": 35.155.XXX.XX,
    ]

```

**This is what the current output (`local.ec2_comb`) looks like:**

```
 ec2_instances = [
  {
"ID" = 0
"IP" = "i-0bcfff94e8d2f9849"
  },
  {
"ID" = 0
"IP" = "i-01fba46588f5ecee7"
  },
  {
"ID" = 1
"IP" = "35.155.XXX.XX"
  },
  {
"ID" = 1
"IP" = "35.155.XXX.XX"
  },

```

**I am using this to create the output:**

```
output "ec2_instances" {
  value = local.ec2_comb
}

locals {
  ec2_ids = flatten(tolist([module.ec2_instances.id, module.ec2_instances_dynamodb.id]))
  ec2_ips = flatten(tolist([module.ec2_instances.public_ip, module.ec2_instances_dynamodb.public_ip]))
  
  pair = concat([local.ec2_ids, local.ec2_ips])

  ec2_comb = flatten([
    for ID, IPs in local.pair : [
      for IP in IPs : {
        ID = ID
        IP = IP
      }
    ]
  ])
}

```

**Output of `locals.ec2_ids`:**

```
ec2_instance_id = [
  "i-0eda41e4452281af2",
  "i-018baefc331b68cb3",
]

```

**Output of `locals.ec2_ips`:**

```
  ec2_instance_public_ips = [
  "35.155.XXX.XX",
  "35.155.XXX.XX",
]

```

**Output of `locals.pair` :**

```
  ec2_instances = [
  [
    "i-0eda41e4452281af2",
    "i-018baefc331b68cb3",
  ],
  [
  "35.155.XXX.XX",
  "35.155.XXX.XX",
  ],
]

```

The reason I am using the `tolist()` function is because the modules create a tuple type value (e.g `module.ec2_instances.id` ) for the EC2 instance.

So in this case, the instance IDs and public IPs are tuples.

Thank you in advance for the help.

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 13, 2020, 5:16pm UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/2 "2020-04-13T17:16:06Z")

</div>

Hi @hadens,

I think there are a few different ways to get this done. My instinct is to flip the problem the other way around and deal with each of the modules separately first and then combine the two together afterwards, like this:

```auto
locals {
  main_instance_addrs = tomap({
    for i, id in module.ec2_instances.id :
    id => module.ec2_instances.public_ip[i]
  })
  dynamodb_instance_addrs = tomap({
    for i, id in module.ec2_instances_dynamodb.id :
    id => module.ec2_instances_dynamodb.public_ip[i]
  })
  instance_addrs = merge(
    local.main_instance_addrs,
    local.dynamodb_instance_addrs,
  )
}

```

I _think_ that gets the result you were looking for here, but please let me know if I misunderstood.

---

<div class="post-metadata">

**Author:** ![hadens](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/hadens/32/5949_2.png) [@hadens](https://discuss.hashicorp.com/u/hadens)\
**Post date:** [April 13, 2020, 9:25pm UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/3 "2020-04-13T21:25:28Z")

</div>

Hi @apparentlymart,

Thanks for helping out. Your approach did work! However, it only works when one instance is created per module.

I tested it with two instances for the “main instances” `(main_instance_addrs)` and one instance for `(dynamodb_instance_addr)` and obtained these results.

```
dynamodb_ec2_instance_info = {
  "i-087750beb58cb161b" = "54.149.XX.XX"
}
ec2_instance_info = {
  "i-0249412693e02700a" = "34.221.XX.XX"
  "i-0d17abc6ac2a1a002" = "34.221.XX.XX"
}
ec2_instances = {
  "i-0249412693e02700a" = "34.221.XX.XX"
  "i-0d17abc6ac2a1a002" = "34.221.XX.XX"
} 

```

It appears that the merged result totally skips over the dynamo\_db map. Any ideas?

Thanks again!

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 14, 2020, 5:54pm UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/4 "2020-04-14T17:54:07Z")

</div>

Hi @hadens,

I’m not sure what happened there… it’s weird that you’d only see the elements from one of the maps in the result of `merge`. Would you mind sharing your full implementation that produced this so I can make sure I’m following the chain of operations properly? Thanks.

---

<div class="post-metadata">

**Author:** ![hadens](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/hadens/32/5949_2.png) [@hadens](https://discuss.hashicorp.com/u/hadens)\
**Post date:** [April 15, 2020, 4:44pm UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/5 "2020-04-15T16:44:09Z")

</div>

Hi @apparentlymart,

This is what I am using to create the EC2 instances:

```
module "ec2_instances" {
  source = "terraform-aws-modules/ec2-instance/aws"
  version = "2.12.0"

  name = "s3_rw_access"
  instance_count = 1

  ami = data.aws_ami.amazon_linux.id
  instance_type = "t2.micro"

  key_name = aws_key_pair.deployer.key_name
  iam_instance_profile = module.s3_read_write.profile_name

  vpc_security_group_ids = [module.vpc.default_security_group_id, module.nsg.this_security_group_id]
  subnet_id = module.vpc.public_subnets[0]

  tags = local.common_tags
}

module "ec2_instances_dynamodb" {
  source = "terraform-aws-modules/ec2-instance/aws"
  version = "2.12.0"

  name = "dynamodb_rw_access"
  instance_count = 1

  ami = data.aws_ami.amazon_linux.id
  instance_type = "t2.micro"

  key_name = aws_key_pair.deployer.key_name
  iam_instance_profile = module.DynamoDB_read_write.profile_name

  vpc_security_group_ids = [module.vpc.default_security_group_id, module.nsg.this_security_group_id]
  subnet_id = module.vpc.public_subnets[0]

  tags = local.common_tags
} 

```

`merge` only displays one map when `instance_count = X` is greater than 1.

Please let me know if you need anything else. Thanks!

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 15, 2020, 5:19pm UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/6 "2020-04-15T17:19:53Z")

</div>

Hi @hadens,

I’d also like to see the `locals` block you’re using. The result you shared in your earlier message suggested that you’d modified it a little from what I suggested (the names were different) so I want to make sure I understand how the new names you are using relate to one another and to my earlier suggestion. Thanks!

---

<div class="post-metadata">

**Author:** ![hadens](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/hadens/32/5949_2.png) [@hadens](https://discuss.hashicorp.com/u/hadens)\
**Post date:** [April 16, 2020, 12:39am UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/7 "2020-04-16T00:39:17Z")

</div>

Hi @apparentlymart,

Sorry I misunderstood. Here is my `locals` block:

```
locals {
  # ec2_ids = flatten(tolist([module.ec2_instances.id, module.ec2_instances_dynamodb.id]))
  # ec2_ips = flatten(tolist([module.ec2_instances.public_ip, module.ec2_instances_dynamodb.public_ip]))

  # pair = concat([local.ec2_ids, local.ec2_ips])

  # ec2_comb = flatten([
  # for ID, IPs in local.pair : [
  # for IP in IPs : {
  # ID = ID
  # IP = IP
  # }
  # ]
  # ])

  main_instance_addrs = tomap({
    for i, id in module.ec2_instances.id :
    id => module.ec2_instances.public_ip[i]
  })
  dynamodb_instance_addrs = tomap({
    for i, id in module.ec2_instances_dynamodb.id :
    id => module.ec2_instances_dynamodb.public_ip[i]
  })
  instance_addrs = merge(
    local.main_instance_addrs,
    local.dynamodb_instance_addrs,
  )
}

```

And the output I’m using:

```
 output "ec2_instances" {
   value = local.main_instance_addrs
}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 16, 2020, 10:10pm UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/8 "2020-04-16T22:10:43Z")

</div>

Hi @hadens,

It looks like your output is referring to `local.main_instance_addrs`, rather than to the merged result `local.instance_addrs`. Hopefully this will get the result you were looking for:

```auto
output "ec2_instances" {
  value = local.instance_addrs
}

```

---

<div class="post-metadata">

**Author:** ![hadens](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/hadens/32/5949_2.png) [@hadens](https://discuss.hashicorp.com/u/hadens)\
**Post date:** [April 17, 2020, 4:10am UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/9 "2020-04-17T04:10:55Z")

</div>

@apparentlymart,

Boy do I feel dumb… that was definitely the issue. The output is now displaying the merged maps correctly.

Thank you for all your help! 👍

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [April 17, 2020, 4:43pm UTC](https://discuss.hashicorp.com/t/create-a-map-from-two-lists/7580/10 "2020-04-17T16:43:42Z")

</div>

No problem! Easy to get the names mixed up when they all look so similar. I’m glad it worked out!
