title: how to add adding multiple aws target groups to multiple autoscaling groups using terraform
body: <p>i am creating 4 target groups and 4 nlb , count is based on number of screenrecorders (01-04). Also 3 ASG one for each AZ. I want to add 4 target groups to each ASG.
  for example : fw_asg_az1 should have 4 target groups attached, same way for fw_asg_az2 and fw_asg_az3 same target groups needs to be attached.</p>
  <p>in future if i add server05 in my variable and re run terraform, newly created target group should be added to all three ASG's</p>
  <p>variables in my main.tf</p>
  <pre><code>screenrecorders = [
        &quot;server01&quot;,
        &quot;server02&quot;,
        &quot;server03&quot;,
        &quot;server04&quot;  
    ]
  </code></pre>
  <p>variable.tf</p>
  <pre><code>variable &quot;screenrecorders&quot; {
    description = &quot;List of screen recorders&quot;
    type = list(string)
  }
  </code></pre>
  <p>main.tf</p>
  <pre><code>resource &quot;aws_autoscaling_group&quot; &quot;fw_asg_az1&quot; {
    name = &quot;${var.deployment_prefix}-asg-${var.az1}&quot;
    availability_zones = [var.az1]
    health_check_grace_period = 300
    health_check_type         = &quot;ELB&quot;
    default_cooldown          = 600
    launch_template {
      id      = aws_launch_template.fw_launch_template_az1.id
      version = &quot;$Latest&quot;
    }
    min_size = 1
    max_size = 5
    tag {
      key                 = &quot;Name&quot;
      value               = &quot;${var.deployment_prefix}-${var.az1}&quot;
      propagate_at_launch = true
    }
    target_group_arns = [&quot;${aws_lb_target_group.tg_scr_vr.arn}&quot;]
    depends_on = [aws_lb_target_group.tg_scr_vr]   
  }
  
  resource &quot;aws_autoscaling_group&quot; &quot;fw_asg_az2&quot; {
    name = &quot;${var.deployment_prefix}-asg-${var.az2}&quot;
    availability_zones = [var.az2]
    health_check_grace_period = 300
    health_check_type         = &quot;ELB&quot;
    default_cooldown          = 600
    launch_template {
      id      = aws_launch_template.fw_launch_template_az2.id
      version = &quot;$Latest&quot;
    }
    min_size = 1
    max_size = 5
    tag {
      key                 = &quot;Name&quot;
      value               = &quot;${var.deployment_prefix}-${var.az2}&quot;
      propagate_at_launch = true
    } 
    target_group_arns = [&quot;${aws_lb_target_group.tg_scr_vr.arn}&quot;]
    depends_on = [aws_lb_target_group.tg_scr_vr]   
  }
  
  resource &quot;aws_autoscaling_group&quot; &quot;fw_asg_az3&quot; {
    name = &quot;${var.deployment_prefix}-asg-${var.az3}&quot;
    availability_zones = [var.az3]
    #availability_zones = [data.aws_availability_zones.all.names]
    health_check_grace_period = 300
    health_check_type         = &quot;ELB&quot;
    default_cooldown          = 600
    launch_template {
      id      = aws_launch_template.fw_launch_template_az3.id
      version = &quot;$Latest&quot;
    }
    min_size = 1
    max_size = 5
    tag {
      key                 = &quot;Name&quot;
      value               = &quot;${var.deployment_prefix}-${var.az3}&quot;
      propagate_at_launch = true
    }
    target_group_arns = [&quot;${aws_lb_target_group.tg_scr_vr.arn}&quot;]
    depends_on = [aws_lb_target_group.tg_scr_vr]
    
  }
  
  ###############################################################################
  #                              ELB Target Groups
  ###############################################################################
  resource &quot;aws_lb_target_group&quot; &quot;tg_scr_vr&quot; {
    count       = length(var.screenrecorders)
    name        = &quot;tg-${var.screenrecorders[count.index]}&quot;
    target_type = &quot;instance&quot;
    protocol    = &quot;TCP&quot;
    port        = &quot;80&quot;
    vpc_id      = var.vpc_id
    health_check {
      protocol = &quot;TCP&quot;
      port     = &quot;traffic-port&quot;
      
  
    }
  }
  
  ###############################################################################
  #                              ELB
  ###############################################################################
  resource &quot;aws_lb&quot; &quot;lb_scr_vr&quot; {
    count              = length(var.screenrecorders)
    name               = &quot;lb-${var.screenrecorders[count.index]}&quot;
    load_balancer_type = &quot;network&quot;
    internal           = false
    ip_address_type    = &quot;ipv4&quot;
    subnet_mapping {
      subnet_id = var.az1_fw_public_subnet_id
    }
    subnet_mapping {
      subnet_id = var.az2_fw_public_subnet_id
    }
    subnet_mapping {
      subnet_id = var.az3_fw_public_subnet_id
    }
  }
  </code></pre>
  <p>error i am getting</p>
  <pre><code>Error: Missing resource instance key
  
    on .terraform/modules/fw/main.tf line 147, in resource &quot;aws_autoscaling_group&quot; &quot;fw_asg_az1&quot;:
   147:   target_group_arns = [&quot;${aws_lb_target_group.tg_scr_vr.arn}&quot;]
  
  Because aws_lb_target_group.tg_scr_vr has &quot;count&quot; set, its attributes must
  be accessed on specific instances.
  
  For example, to correlate with indices of a referring resource, use:
      aws_lb_target_group.tg_scr_vr[count.index]
  </code></pre>
  <p>i tried below as suggested in the error, still didn't work</p>
  <pre><code>aws_lb_target_group.tg_scr_vr[count.index]
  </code></pre>
  <p>i am new to terraform, still learning, any help from stackoverflow community is greatly appreciated.</p>