title: Terraform loop over subnet ids
body: <p>I have a VPC module which creates private subnets</p>
  <pre><code>resource &quot;aws_subnet&quot; &quot;private&quot; {
    for_each          = { for index, az_name in local.az_names : index =&gt; az_name }
    vpc_id            = aws_vpc.network.id
    cidr_block        = cidrsubnet(aws_vpc.network.cidr_block, 8, each.key + 11)
    availability_zone = local.az_names[each.key]
    tags = {
      Name        = &quot;${var.env}-private-${local.az_names[each.key]}&quot;
      Description = local.az_names[each.key]
      Type = &quot;private&quot;
    }
  }
  </code></pre>
  <p>And an output</p>
  <pre><code>output &quot;private_subnet&quot; {
    value = aws_subnet.private
  }
  </code></pre>
  <p>Now I am trying to create an EFS module which will create a mount target for each private subnet</p>
  <pre><code>resource &quot;aws_efs_mount_target&quot; &quot;efs-mt&quot; {
    count = length(var.private_subnet)
    file_system_id  = aws_efs_file_system.efs.id
    subnet_id = var.private_subnet[count.index].id
    security_groups = var.security_groups
  }
  </code></pre>
  <p>main.tf</p>
  <pre><code>module &quot;efs&quot; {
    source              = &quot;./modules/efs&quot;
    env                 = var.env
    vpc_id           = module.vpc.vpc_id
    security_groups = [module.security.efs_sg_ids]
    private_subnet = module.vpc.private_subnet
  }
  </code></pre>
  <p>But I am getting these errors</p>
  <pre><code>│ Error: Invalid index
  │ 
  │   on modules/efs/main.tf line 14, in resource &quot;aws_efs_mount_target&quot; &quot;efs-mt&quot;:
  │   14:   subnet_id = var.private_subnet[count.index].id
  │     ├────────────────
  │     │ count.index is a number, known only after apply
  │     │ var.private_subnet is a string, known only after apply
  │ 
  │ This value does not have any indices.
  </code></pre>