title: Getting Outputs of Resources from Reused Modules
body: <p>I have the following code that will call a module and make target groups for me based off of the information I pass it in the locals variables.  This works just fine, my issue being trying to get the arn of each target group it creates in an output.</p>
  <pre><code>locals {
    targetgroups_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;
      }
      }
    }
  
  module &quot;target-groups&quot;{
    for_each = local.targetgroups_beta
    source = &quot;.//modules/targetgroups&quot;
    name-beta = each.value.name
    path-beta = each.value.path
    environment-beta = each.value.environment
    vpc-id = &quot;${module.vpc.vpc_id}&quot;
  }
  </code></pre>
  <p>The resource name in the module it is calling is target-group, so based off of what I read I should be able to refer to it by something like this:</p>
  <pre><code>output{
       value = &quot;${aws_lb_target_group.target-group[0].arn}&quot;
  }
  </code></pre>
  <p>When I try this I receive the following when running a terraform plan:</p>
  <p>&quot;Because aws_lb_target_group.target-group does not have &quot;count&quot; or &quot;for_each&quot; set, references to it must not include an index key. Remove the bracketed index to refer to the single instance of this resource.&quot;</p>
  <p>My understanding of this is the module that the for_each is calling isn't running a for_each, so I cannot reference the resources in this way.  If I do &quot;&quot;${aws_lb_target_group.target-group.arn}&quot; that reference works technically, but includes the arn for every target group and I plan on adding a lot more.  Is there a way to take each of these arns out of this list as its own output?</p>
  <p>Code in the module that it is calling for reference:</p>
  <pre><code>resource &quot;aws_lb_target_group&quot; &quot;target-group&quot; {
    name     = &quot;example-${var.name-beta}&quot;
    port     = 80
    protocol = &quot;HTTP&quot;
    vpc_id   = var.vpc-id
    deregistration_delay = 5
  
    tags = {
      Environment = &quot;${var.environment-beta}&quot;
    }
  
    health_check{
    healthy_threshold = 2
    unhealthy_threshold = 2
    timeout = 10
    interval = 15
    path = var.path-beta
    }
  }
  </code></pre>