title: Outputs from for_each loop for each resource
body: <p>I am having a hard time figuring out how to make an output for each target group resource that this code creates.  I'd like to be able to reference each one individually in other modules.  It sounds like for_each stores it as a map, so my question is how would I get the arn for targetgroup1 and targetgroup2?  Terraform normally refers to outputs by resource name, so I am struggling with that in this scenario and also how to refer to these individual arns.  Would I also need to work the outputs into the for_each or could I drop it into the output.tf file?</p>
  <pre><code>locals {
    target_groups_beta = {
      targetgroup1 = {
        name = &quot;example&quot;,
        path = &quot;/&quot;,
        environment = &quot;Beta&quot;
      }
      targetgroup2 = {
        name = &quot;example2&quot;,
        path = &quot;/&quot;,
        environment = &quot;Beta&quot;
      }
      }
    }
  
  resource &quot;aws_lb_target_group&quot; &quot;target-group&quot; {
    for_each = local.target_groups_beta
    name     = &quot;example-${each.value.name}-&quot;
    port     = 80
    protocol = &quot;HTTP&quot;
    vpc_id   = var.vpc-id
    deregistration_delay = 5
  
    tags = {
      Environment = &quot;${each.value.environment}&quot;
    }
  
    health_check{
    healthy_threshold = 2
    unhealthy_threshold = 2
    timeout = 10
    interval = 15
    path = each.value.path
    }
  }
  </code></pre>
  <p>I receive the following error when trying to do it in the output.tf file without a key value, but when I input one such as value = &quot;${aws_lb_target_group.target-group[0].arn}&quot; it says it's invalid.  Error without key value below:</p>
  <p>Error: Missing resource instance key</p>
  <p>on modules\targetgroups\output.tf line 2, in output &quot;tg_example_beta&quot;:
  2:     value = &quot;${aws_lb_target_group.target-group.arn}&quot;</p>
  <p>Because aws_lb_target_group.target-group has &quot;for_each&quot; set, its attributes
  must be accessed on specific instances.</p>
  <p>For example, to correlate with indices of a referring resource, use:
  aws_lb_target_group.target-group[each.key]</p>