title: Dynamic block with for_each inside a resource created with for_each syntax assitance
body: <p>I am trying to find a way to conditionally populate the pages block of the github_repository resource, when the resource itself is created using for_each. The dynamic block seems to be the appropriate way to achieve this, but I am struggling with the syntax.</p>
  <p>I've tried with the code below and it fails.</p>
  <p>variables.tf:</p>
  <pre><code>variable &quot;repositories&quot; {
    description = &quot;The repositories to create using terraform&quot;
    type = list(object({
      name                 = string,
      description          = string,
      vulnerability_alerts = bool,
      pages_cname          = string
     }))
    }
  </code></pre>
  <p>terraform.tfvars.json:</p>
  <pre><code>{
    &quot;repositories&quot;: [
      {
        &quot;name&quot;: &quot;repo-with-pages&quot;,
        &quot;description&quot;: &quot;Repository with pages enabled.&quot;,
        &quot;vulnerability_alerts&quot;: true,
        &quot;pages_cname&quot;: &quot;www.example.com&quot;
      },
      {
        &quot;name&quot;: &quot;repo-without-pages&quot;,
        &quot;description&quot;: &quot;Repository without pages enabled.&quot;,
        &quot;vulnerability_alerts&quot;: true
      }
    ]
  }
  </code></pre>
  <p>main.tf:</p>
  <pre><code>resource &quot;github_repository&quot; &quot;this&quot; {
    for_each               = { for repo in var.repositories : repo.name =&gt; repo }
    name                   = each.value.name
    description            = each.value.description
    vulnerability_alerts   = each.value.vulnerability_alerts
  
    dynamic &quot;pages&quot; {
      for_each = { for repo in var.repositories : repo.name =&gt; repo }
      content {
        source {
          branch = &quot;main&quot;
          path   = &quot;/docs&quot;
        }
        cname = each.value.pages_cname
        }
      }
    }
   
  </code></pre>
  <p>Result:</p>
  <pre><code>Error: Too many pages blocks
  
  on main.tf line 43, in resource &quot;github_repository&quot; &quot;this&quot;:
  43:     content {
  
  No more than 1 &quot;pages&quot; blocks are allowed
  </code></pre>
  <p>This makes perfect sense because the for expression within the dynamic block for_each returns two values (repo-with-pages and repo-without-pages) and only 1 page block is allowed. So, what needs to happen is the for_each / for expression combination needs to return 1 value - the name of the repository created - <strong>IF</strong> pages are enabled.</p>
  <p>Been looking at this for a while and beginning to wonder if what I want to do is even possible or if I am over complicating things. Any help most welcome. Thanks.</p>