# How to output values seperately when created using for\_each

**URL:** <https://discuss.hashicorp.com/t/how-to-output-values-seperately-when-created-using-for-each/34908>\
**Category:** Terraform\
**Created:** [January 28, 2022, 12:32pm UTC](https://discuss.hashicorp.com/t/how-to-output-values-seperately-when-created-using-for-each/34908 "2022-01-28T12:32:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeirdreRodgers](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/deirdrerodgers/32/3275_2.png) [@DeirdreRodgers](https://discuss.hashicorp.com/u/DeirdreRodgers)\
**Post date:** [January 28, 2022, 12:32pm UTC](https://discuss.hashicorp.com/t/how-to-output-values-seperately-when-created-using-for-each/34908/1 "2022-01-28T12:32:03Z")

</div>

I created 3 subnets using the for\_each function and I outputted some values. However the values are being outputted altogether as a map and I need them to be outputted seperately so I can use them as inputs for other resources

my root module main.tf

```auto

module "deploy_subnet" {
  source = "./modules/azure_subnet"
  for_each = var.prefix
  subnet_name = each.value["name"]
  address_prefixes = [each.value["cidr"]]
  subscription_id = var.subscription_id
  resource_group_name = var.resource_group_name
  region = var.region
  vnet_name = var.vnet_name
}

```

My variables.tf in my root module

```auto
variable "prefix" {
  type = map(object({
    name = string
    cidr = string
  }))
  default = {
    sub-1 = {
      name = "aks-sn"
      cidr = "10.0.1.0/24"
    }
    sub-2 = {
      name = "postgres-sn"
      cidr = "10.0.2.0/24"
    }
    sub-3 = {
      name = "keyvault-sn"
      cidr = "10.0.3.0/24"
    }
  }
}

```

My root module output.tf

```auto
output "subnet_name" {
  value = {
    for k, subnet in module.deploy_subnet : k => subnet.subnet_name
  }
}
output "subnet_id" {
  value = {
    for k, subnet in module.deploy_subnet : k => subnet.subnet_id
  }
}
output "subnet_cidr_block" {
  value = {
    for k, subnet in module.deploy_subnet : k => subnet.subnet_cidr_block
  }
}

```

My child module main.tf

```auto
resource "azurerm_subnet" "obc_subnet" {
  name = var.subnet_name
  address_prefixes = var.address_prefixes
  resource_group_name = var.resource_group_name
  virtual_network_name = var.vnet_name
}

```

Child module output.tf

```auto
output "subnet_name" {

value = azurerm_subnet.obc_subnet.name

}

output "subnet_id" {

value = azurerm_subnet.obc_subnet.id

}

output "subnet_cidr_block" {

value = azurerm_subnet.obc_subnet.address_prefixes

}

```

I am getting the output like this for example

```auto

subnet_name = {
  "sub-1" = "aks-sn"
  "sub-2" = "postgres-sn"
  "sub-3" = "keyvault-sn"
}

```

I want to get them to output their values individually so I can use them elsewhere to create resources

---

<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:** [January 28, 2022, 3:27pm UTC](https://discuss.hashicorp.com/t/how-to-output-values-seperately-when-created-using-for-each/34908/2 "2022-01-28T15:27:43Z")

</div>

That’s looking exactly as it should do. If you need to reference a specific value just do `module.deploy_subnet["sub-1"].subnet_name`

---

<div class="post-metadata">

**Author:** ![DeirdreRodgers](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/deirdrerodgers/32/3275_2.png) [@DeirdreRodgers](https://discuss.hashicorp.com/u/DeirdreRodgers)\
**Post date:** [January 28, 2022, 4:08pm UTC](https://discuss.hashicorp.com/t/how-to-output-values-seperately-when-created-using-for-each/34908/3 "2022-01-28T16:08:42Z")

</div>

thats great, thanks Stuart, it worked. i tried something similar but I was doing the below

module.deploy\_subnet.obc\_subnet["sub-1].name

I realise now that this doesnt make sense as the attribute of name was already outputted in the child module and I have to reference that output in my root module
