title: Terraform for-each with list of objects
body: <p>I have the following variable in variables.tf file:</p>
  <pre><code>variable tenants {
    description = &quot;Map of project names to configuration.&quot;
    type = list(object({
      name  = string
      dname = string
      desc  = string
      site  = list(string)
    }))
    default = [{
        name  = &quot;Tenant-1&quot;,
        dname = &quot;Tenant-1&quot;,
        desc  = &quot;Test Tenant 1&quot;,
        site  = [&quot;site1&quot;, &quot;site2&quot;]
      },
      {
        name  = &quot;Tenant-2&quot;,
        dname = &quot;Tenant-2&quot;,
        desc  = &quot;Test Tenant 2&quot;,
        site  = [&quot;site1&quot;]
      }]
  }
  </code></pre>
  <p>In my main.tf file, I would like to loop over this list. I have the following code in main.tf file:</p>
  <pre><code>resource &quot;mso_tenant&quot; &quot;restenant&quot; {
    for_each = {for i, v in var.tenants:  i =&gt; v}
      name         = each.value.name
      display_name = each.value.dname
      description  = each.value.desc
      site_associations {
        site_id =  each.value.site
    }
  }
  </code></pre>
  <p>So the end result should be that 2 tenants get created with the attributes as specified in the variable file. So tenant1 will have 2 site_associations and tenant2 will have 1 association once created.</p>
  <p>Result should be:</p>
  <pre><code>name         = &quot;Tenant-1&quot;
  display_name = &quot;Tenant-1&quot;
  description  = &quot;Test Tenant 1&quot;
  site_associations {
    site_id = site1
    site_id = site2
  }
  </code></pre>
  <p>and</p>
  <pre><code>name         = &quot;Tenant-2&quot;
  display_name = &quot;Tenant-2&quot;
  description  = &quot;Test Tenant 2&quot;
  site_associations {
     site_id = site1
  }
  </code></pre>
  <p>I tried the following:</p>
  <pre><code>resource &quot;mso_tenant&quot; &quot;restenant&quot; {
    for_each = {for i, v in var.tenants:  i =&gt; v}
      name         = each.value.name
      display_name = each.value.dname
      description  = each.value.desc
      site_associations {
        site_id =  each.value.site
    }
  }
  </code></pre>
  <p>This works for the <code>name</code>, <code>dname</code> and <code>desc</code> but it does not iterate over the site variable (which is a list). This results in the error message:</p>
  <blockquote>
  <p>each.value.site is list of string with 1 element Inappropriate value
  for attribute &quot;site_id&quot;: string required.</p>
  </blockquote>
  <p>Tried to solve as follows:</p>
  <pre><code>resource &quot;mso_tenant&quot; &quot;restenant&quot; {
    for_each = {for i, v in var.tenants:  i =&gt; v}
      
      name         = each.value.name
      display_name = each.value.dname
      description  = each.value.desc
      site_associations {
        site_id = [for site in each.value.site: site]
    }
  }
  </code></pre>
  <p>but this also gives:</p>
  <blockquote>
  <p>each.value.site is list of string with 2 elements
  Inappropriate value for attribute &quot;site_id&quot;: string required.</p>
  </blockquote>