title: Iterate over nested data with for / for_each at resource level
body: <p>I am trying to work out how to iterate over nested variables from a complex object given in the following tfvars file using Terraform 0.12.10:</p>
  
  <h3>example.tfvars</h3>
  
  <pre><code>virtual_network_data = {
    1 = {
      product_instance_id              = 1
      location                         = "somewhere"
      address_space                    = ["192.168.0.0/23"]
      dns_servers                      = []
      custom_tags                      = {"test":"test value"}
      subnets                          = [
        {
          purpose = "mgmt"
          newbits = 4
          item = 0
        },
        {
          purpose = "transit"
          newbits = 4
          item = 1
        }
      ]
    }
  }
  </code></pre>
  
  <h3>example.tf</h3>
  
  <pre><code>variable "virtual_network_data" {} #Data comes from example.tfvars
  
  variable "resource_group_name" {
      default = "my_resource_group"
  }
  
  variable "virtual_network_name" {
      default = "my_virtual_network"
  }
  
  ####
  
  resource "azurerm_subnet" "pool" {
      for_each             = var.virtual_network_data
  
      name                 = format("%s%s%02d", "subnet_", s.purpose, s.item)
      resource_group_name  = var.resource_group_name
      virtual_network_name = var.virtual_network_name
      address_prefix       = cidrsubnet(each.value["address_space"], s.newbits, s.item)
  
  }
  </code></pre>
  
  <p>In <code>example.tf</code> I can use <code>each.value["address_space"]</code> to get to the top level variables, but I can't work out how to get to the items in <code>subnets</code> (<code>s.purpose</code>, <code>s.item</code> &amp; <code>s.newbits</code>).</p>
  
  <p>I have used dynamic blocks, as part of a parent resource (below), which works but in this case, I need to move the subnet into its own resource. Simply put, how do I get the first <code>for_each</code> to behave like the second <code>for_each</code> in the dynamic block?</p>
  
  <pre><code>resource "azurerm_virtual_network" "pool" {
    for_each                  = var.virtual_network_data
  
    name                      = format("%s%02d", local.resource_name, each.key)
    resource_group_name       = var.resource_group_name
    location                  = each.value["location"]
    address_space             = each.value["address_space"]
    dns_servers               = each.value["dns_servers"]
    tags                      = merge(local.tags, each.value["custom_tags"])
  
    dynamic "subnet" {
      for_each = [for s in each.value["subnets"]: {
        name   = format("%s%s%02d", "subnet_", s.purpose, s.item)
        prefix = cidrsubnet(element(each.value["address_space"],0), s.newbits, s.item)
      }]
  
      content {
        name            = subnet.value.name
        address_prefix  = subnet.value.prefix
      }
    }
  }
  </code></pre>
  
  <p>Cheeky bonus, is there a way to replace <code>s.item</code> with something like <code>each.key</code> or <code>count.index</code>? </p>
  
  <p>TIA</p>