# Extract objects without for\_each

**URL:** <https://discuss.hashicorp.com/t/extract-objects-without-for-each/47026>\
**Category:** Terraform\
**Created:** [November 17, 2022, 3:42pm UTC](https://discuss.hashicorp.com/t/extract-objects-without-for-each/47026 "2022-11-17T15:42:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![menvol3](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/menvol3/32/23258_2.png) [@menvol3](https://discuss.hashicorp.com/u/menvol3)\
**Post date:** [November 17, 2022, 3:42pm UTC](https://discuss.hashicorp.com/t/extract-objects-without-for-each/47026/1 "2022-11-17T15:42:30Z")

</div>

Hello  
I need community help)

I have a var with output like below

```auto
private_subnets = {
      + eu-west-1a = [
          + "subnet-0b53fe5230ebe5c78",
        ]
      + eu-west-1b = [
          + "subnet-0eab295109e89e12a",
        ]
      + eu-west-1c = [
          + "subnet-02124519fb318a211",
        ]

```

How can I extract values (subnet id) by providing a key (availability zone)?  
Also my part of code

```auto
module "ec2_instance" {
  source = "terraform-aws-modules/ec2-instance/aws"
  version = "~> 3.0"

  count = length(local.azs)
  name = "${local.server_name}-${count.index +1}"
  subnet_id = element(local.az_subnet_ids[local.azs], count.index)

```

```auto
 Error: Invalid index
│ 
│ on runner.tf line 104, in module "ec2_instance":
│ 104: subnet_id = element(local.az_subnet_ids[local.azs], count.index)
│ ├────────────────
│ │ local.az_subnet_ids is object with 3 attributes
│ │ local.azs is tuple with 3 elements
│ 
│ The given key does not identify an element in this collection value: string required.

```

---

<div class="post-metadata">

**Author:** ![stuart-c](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/stuart-c/32/10789_2.png) [@stuart-c](https://discuss.hashicorp.com/u/stuart-c)\
**Post date:** [November 17, 2022, 10:47pm UTC](https://discuss.hashicorp.com/t/extract-objects-without-for-each/47026/2 "2022-11-17T22:47:18Z")

</div>

You can’t use a tuple (your local.azs) to do a lookup in an object (your local.az\_subnet\_ids).

I’m not sure what are in both those local variables, as the code doesn’t match the above bit and nothing shows local.azs, but assuming az\_subnet\_ids is the map you showed above (called private\_subnets) and azs is a list of the different availability zones you’d just want

```auto
subnet_id = local.az_subnet_ids[count.index]

```
