title: Unable to create a nested dynamic block
body: <p>I have a set of 9 security group rules that I need to apply to 4 different sources. I wanted to build it as a module so instead of copy/pasting the same block multiple times, I just need to pass the ports and source as variables.</p>
  <p>I have tried to create a module that gets the ports as <code>for_each</code> in a dynamic block and also passes the sources with <code>count</code> since I failed to provide an additional dynamic block with <code>for_each</code> also for the sources.</p>
  <h2>modules/sg/main.tf</h2>
  <pre><code>resource &quot;aws_security_group&quot; &quot;test&quot; {
    name = &quot;test2&quot;
    count = length(var.groups)
    vpc_id = var.vpc_id
  
  
    dynamic &quot;ingress_tcp&quot; {
      for_each = var.tcp_ports
      content {
        from_port = ingress_tcp.value
        to_port = ingress_tcp.value
        protocol = &quot;tcp&quot;
        security_groups = [var.groups[*].id]
      }
    }
    dynamic &quot;ingress_udp&quot; {
      for_each = var.udp_ports
      content {
        from_port = ingress_udp.value
        to_port = ingress_udp.value
        protocol = &quot;udp&quot;
        security_groups = [var.groups[*].id]
      }
    }
  }
  </code></pre>
  <h2>main.tf</h2>
  <pre><code>module &quot;rules&quot; {
    source = &quot;./module/sg&quot;
    vpc_id = var.vpc_id
    name = &quot;tomer-test&quot;
    tcp_ports = var.tcp_ports
    udp_ports = var.udp_ports
    groups = [var.groups[*].id]
  }
  </code></pre>
  <h2>variables.tf</h2>
  <pre><code>variable &quot;groups&quot; {
    description = &quot;source groups&quot;
    type = &quot;list&quot;
    default  = [{
      name = &quot;Enforcement-STG&quot;,
      id = &quot;sg-c9db2183abcd&quot;
    },
      {
        name = &quot;Managment-STG&quot;,
        id = &quot;sg-b0e71dfa123&quot;
    }]
  }
  
  variable &quot;name&quot; {
    type = string
  }
  variable &quot;vpc_id&quot; {
    type = string
    default = &quot;&quot;
  }
  variable &quot;tcp_ports&quot; {
    description = &quot;tcp ports to open&quot;
    default = [514,1514, 11514, 12514, 6514]
  }
  variable &quot;udp_ports&quot; {
    description = &quot;tcp ports to open&quot;
    default = [514,1514, 11514, 12514]
  }
  </code></pre>
  <p>I accept the output to build a set of rules per source groups, but the root module fails to invoke the module.
  The error that I'm currently getting is</p>
  <pre><code>terraform plan
  
  Error: Unsupported block type
  
    on module/sg/main.tf line 7, in resource &quot;aws_security_group&quot; &quot;test&quot;:
     7:   dynamic &quot;ingress_tcp&quot; {
  
  Blocks of type &quot;ingress_tcp&quot; are not expected here.
  
  
  Error: Unsupported block type
  
    on module/sg/main.tf line 16, in resource &quot;aws_security_group&quot; &quot;test&quot;:
    16:   dynamic &quot;ingress_udp&quot; {
  
  Blocks of type &quot;ingress_udp&quot; are not expected here.
  </code></pre>