# Using for/maps in outputs

**URL:** <https://discuss.hashicorp.com/t/using-for-maps-in-outputs/24466>\
**Category:** Terraform\
**Created:** [May 18, 2021, 1:22pm UTC](https://discuss.hashicorp.com/t/using-for-maps-in-outputs/24466 "2021-05-18T13:22:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rba1-source](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rba1-source/32/9412_2.png) [@rba1-source](https://discuss.hashicorp.com/u/rba1-source)\
**Post date:** [May 18, 2021, 1:22pm UTC](https://discuss.hashicorp.com/t/using-for-maps-in-outputs/24466/1 "2021-05-18T13:22:37Z")

</div>

I have the following in terraform v12 which works:

main.tf

```
locals {
  fileserver_modules = {
    fs_001 = module.fs_001
    fs_002 = module.fs_002
}

module "fs_001" {
  source = "./fileserver"
  uid = "team_001"
}
module "fs_002" {
  source = "./fileserver"
  uid = "team_002"
}

output "map_uid_dns__fileserver" {
  # <instance_uid> = [ipv4_1,ipv4_2...]
  value = {
    for storage_module in local.fileserver_modules :
    storage_module.uid => storage_module.private_ip
  }
}

```

and in my “fileserver” directory I have:

```
resource "aws_network_interface" "eni" {
  subnet_id = data.aws_subnet.this.id
  security_groups = local.vpc_security_group_ids
}

output "private_ip" {
  value = aws_network_interface.eni.private_ip
}
output "uid" {
  value = var.uid
}

```

This all works, and the output “map\_uid\_dns\_\_fileserver” outputs a map correctly:

```
map_uid_dns__fileserver = {
  "team001" = "1.2.3.4"
  "team002" = "4.2.3.4"
}

```

I’m upgrading to terraform v13 and will be making use of the new “count” attribute for modules. Now I want to deploy fs\_001 and fs\_002 in my Prod environment, but only fs\_001 in Preprod.

In my main.tf, if I change to the following:

```
locals {
  fileserver_modules = {
	prod = {
	  fs_001 = module.fs_001
	  fs_002 = module.fs_002
	}
	preprod = {
	  fs_001 = module.fs_001
	}
  }
}

output "map_uid_dns__fileserver" {
  # <instance_uid> = [ipv4_1,ipv4_2...]
  value = {
    for storage_module in local.fileserver_modules[var.stack_env] :
    storage_module.uid => storage_module.private_ip
  }
}

```

where “var.stack\_env” evaluates to either “preprod” or “prod”, I get the following error in the terraform plan:

```
Error: Unsupported attribute

  on main.tf line 1012, in output "map_uid_dns__fileserver":
1012: storage_module.uid => storage_module.private_ip

This value does not have any attributes.

```

If I run for Prod, I get this error twice (2 modules) and if I run for PreProd then I get it once (1 module).

I’ve tried simplifying by doing the following:

```
locals {
  map_uid_dns__fileserver = local.fileserver_modules[var.stack_env]
}
output "map_uid_dns__fileserver" {
  # <instance_uid> = [ipv4_1,ipv4_2...]
  value = {
    for storage_module in local.map_uid_dns__fileserver :
    storage_module.uid => storage_module.private_ip
  }
}

```

but I get the same error, so I’m guessing that I can use “for storage\_module in local.fileserver\_modules[var.stack\_env] :” (i.e. I can index into the ‘fileserver\_modules’ map) but something is still not valid.

How do I successfully get a working output?

---

<div class="post-metadata">

**Author:** ![rba1-source](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rba1-source/32/9412_2.png) [@rba1-source](https://discuss.hashicorp.com/u/rba1-source)\
**Post date:** [May 18, 2021, 3:07pm UTC](https://discuss.hashicorp.com/t/using-for-maps-in-outputs/24466/2 "2021-05-18T15:07:09Z")

</div>

I got this working by changing from this (terraform v12):

```
output "map_uid_dns__fileserver" {
  # <instance_uid> = [ipv4_1,ipv4_2...]
  value = {
    for storage_module in local.fileserver_modules[var.stack_env] :
    storage_module.uid => storage_module.private_ip
  }
}

```

to this (terraform v13):

```
output "map_uid_dns__fileserver" {
  # <instance_uid> = [ipv4_1,ipv4_2...]
  value = {
    for storage_module in local.fileserver_modules[var.stack_env] :
    storage_module[0].uid => storage_module[0].private_ip
  }
}

```

I found the difference after outputting local.fileserver\_modules[var.stack\_env] and finding that it was outputting a _list of map_ instead of just _map_:

```
Changes to Outputs:
  + stuff = {
      + fs_001 = [
          + {
              + instance_id = (known after apply)
              + private_ip = (known after apply)
              + uid = "team001"
            },
        ]
      + fs_002 = [
          + {
              + instance_id = (known after apply)
              + private_ip = (known after apply)
              + uid = "team002"
            },
        ]
    }

```

Another formalising/tidying of the syntax/outputs between v12 & 13?
