title: How to reference count index in another module?
body: <p>I have deployed two subnets by using count.index then I need to referece the id of subnet in subnet_route_table_association module. Can any one advise me what is the correct way to do that?</p>
  <p>here is my code</p>
  <p>/application/demo/main.tf</p>
  <pre><code>module “subnet_association” {
  source = “…/…/Modules/subnet_association”
  subid = var.subid
  subnet_id = module.subnet.subnet_id
  route_table_id = module.route_table.route_table_id
  }
  </code></pre>
  <p>Modules/subnet/main.tf</p>
  <pre><code>resource &quot;azurerm_subnet&quot; &quot;module-spoke-subnet&quot; {
    count                                          = var.subnet_count
    name                                           = element(var.subnet_name, count.index)
    resource_group_name                            = var.resource_group_name
    virtual_network_name                           = var.virtual_network_name
    address_prefixes                               = [var.subnet_address[count.index]]
    enforce_private_link_endpoint_network_policies = true
    enforce_private_link_service_network_policies  = true
  }
  variable &quot;resource_group_name&quot; {
  }
  variable &quot;virtual_network_name&quot; {
  }
  </code></pre>
  <p>Modules/subnet/output.tf</p>
  <pre><code>output &quot;subnet_id&quot; {
    value = azurerm_subnet.module-spoke-subnet.*.id
  }
  </code></pre>
  <p>Modules/subnet_association/main.tf</p>
  <pre><code>resource &quot;azurerm_subnet_route_table_association&quot; &quot;module-subnet-association&quot; {
    subnet_id      = var.subnet_id
    route_table_id = var.route_table_id
  }
  variable &quot;subnet_id&quot; {
  
  }
  variable &quot;route_table_id&quot; {
  }
  </code></pre>
  <p>and I`m getting the error</p>
  <pre><code> Error: Incorrect attribute value type
  │
  │   on ..\..\modules\Subnet_association\main.tf line 20, in resource &quot;azurerm_subnet_route_table_association&quot; &quot;module-subnet-association&quot;:
  │   20:   subnet_id      = var.subnet_id
  │     ├────────────────
  │     │ var.subnet_id is tuple with 1 element
  │
  │ Inappropriate value for attribute &quot;subnet_id&quot;: string required.
  </code></pre>