title: Terraform use count.index in nested dynamic block
body: <p>I have the following code for a 'google_compute_url_map'</p>
  <pre><code>dynamic &quot;path_matcher&quot; {
    for_each = var.url_map_path_matcher
    content {
      name = path_matcher.key
      default_service = module.lb_http_serverless.backend_services[path_matcher.value.default_service].self_link
      dynamic &quot;path_rule&quot; {
        for_each = var.url_map_path_rules
        content {
          paths = path_rule.value.paths
          service = module.lb_http_serverless.backend_services[path_rule.value.service].self_link
        }
      }
    }
  }
  </code></pre>
  <p>Due to the complexity of the variables I split the URL map and the path rules in two variables. How can I reference the index position of the first dynamic block variable in the for_each loop to be used across the two variables to make sure that the correct paths apply? Like in below example...</p>
  <pre><code>dynamic &quot;path_matcher&quot; {
    for_each = var.url_map_path_matcher
    content {
      name = path_matcher[count.index].key
      default_service = module.lb_http_serverless.backend_services[path_matcher[count.index].value.default_service].self_link
      dynamic &quot;path_rule&quot; {
        for_each = var.url_map_path_rules
        content {
          paths = path_rule[count.index].value.paths
          service = module.lb_http_serverless.backend_services[path_rule[count.index].value.service].self_link
        }
      }
    }
  }
  </code></pre>
  <p>Any help or pointer in the right direction is greatly appreciated, been breaking my head over this the entire weekend.</p>
  <p>Adding variable structure and example content as per request:</p>
  <pre><code>variable &quot;url_map_path_matcher&quot; {
    type = map(object({
      default_service = string
    }))
    description = &quot;Variables to set the URL Map Path matcher&quot;
  }
  
  variable &quot;url_map_path_rules&quot; {
    type = map(object({
      paths           = list(string)
      service         = string
    }))
    description = &quot;Variables to set the URL Map Path rules&quot;
  }
  </code></pre>
  <p>Example content:</p>
  <pre><code>    &quot;url_map_path_matcher&quot;: {
          &quot;allpaths&quot;: {
              &quot;default_service&quot;: &quot;echo&quot;
          }
      },
      &quot;url_map_path_rules&quot;: {
          &quot;path_rule_1&quot;: {
              &quot;paths&quot;: [
                  &quot;/echo&quot;,
                  &quot;/echo/*&quot;
              ],
              &quot;service&quot;: &quot;echo&quot;
          },
          &quot;path_rule_2&quot;: {
              &quot;paths&quot;: [
                  &quot;/ping&quot;
              ],
              &quot;service&quot;: &quot;ping&quot;
          },
          &quot;path_rule_3&quot;: {
              &quot;paths&quot;: [
                  &quot;/my-new-svc&quot;
              ],
              &quot;service&quot;: &quot;my-new-svc&quot;
          }
      }
  </code></pre>
  <p>Expected to work with more complex setup like below variables:</p>
  <pre><code>    &quot;url_map_path_matcher&quot;: {
          &quot;allpaths&quot;: {
              &quot;default_service&quot;: &quot;echo&quot;
          },
          &quot;service1&quot;: {
              &quot;default_service&quot;: &quot;service1&quot;
          },
      },
      &quot;url_map_path_rules&quot;: {
        &quot;allpaths&quot;: { 
          &quot;path_rule_1&quot;: {
              &quot;paths&quot;: [
                  &quot;/echo&quot;,
                  &quot;/echo/*&quot;
              ],
              &quot;service&quot;: &quot;echo&quot;
          },
          &quot;path_rule_2&quot;: {
              &quot;paths&quot;: [
                  &quot;/ping&quot;
              ],
              &quot;service&quot;: &quot;ping&quot;
          },
          &quot;path_rule_3&quot;: {
              &quot;paths&quot;: [
                  &quot;/my-new-svc&quot;
              ],
              &quot;service&quot;: &quot;my-new-svc&quot;
          },
        },
        &quot;service1&quot;: {
          &quot;path_rule_1&quot;: {
              &quot;paths&quot;: [
                  &quot;/pingservice&quot;
              ],
              &quot;service&quot;: &quot;pingservice&quot;
          },
          &quot;path_rule_2&quot;: {
              &quot;paths&quot;: [
                  &quot;/service1&quot;
              ],
              &quot;service&quot;: &quot;service1&quot;
          },
        }
      }
  </code></pre>
  <p>Kind regards,</p>
  <p>Eric V.</p>