title: How to Generate a Diagram Function in Python
body: <p>So I have a task, whereby which I am trying to generate a diagram for all of the VPC(s) in an AWS account and include their cidr_block as pertaining information. I have this one block in Python that returns the diagram for the account as well as two functions that return the <code>vpc_id</code> and the <code>cidr_blocks</code>. The block and two functions are as followed:</p>
  <pre class="lang-py prettyprint-override"><code>def parse_vpc_id_payload():
      with open('vpc_payload.json', 'r') as f:
          data = json.load(f)
          for i in data:
              print(i['VpcId'])
  
  def parse_vpc_cidr_block_payload():
      with open('vpc_payload.json', 'r') as f:
          data = json.load(f)
          for i in data:
              print(i['CidrBlock'])
  
  def companynetcluster():
      with Diagram(filename=&quot;images/net&quot;, show=False, direction=&quot;LR&quot;, graph_attr=graph_attr):
          with Cluster(&quot;impinjnet&quot;, graph_attr={&quot;margin&quot;: &quot;40&quot;, &quot;fontsize&quot;: &quot;18&quot;}):
              companynet = OrganizationsAccount(&quot;Company-Net&quot;)                
              with Cluster(&quot;vpc-id1&quot;, graph_attr={&quot;margin&quot;: &quot;40&quot;, &quot;fontsize&quot;: &quot;18&quot;}):
                  net_subnet1 = PrivateSubnet(&quot;10.XXX.Y.Z/16&quot;)
              with Cluster(&quot;vpc-id2&quot;, graph_attr={&quot;margin&quot;: &quot;40&quot;, &quot;fontsize&quot;: &quot;18&quot;}):
                  net_subnet2 = PrivateSubnet(&quot;192.XXX.YYY.Z/24&quot;)
              companynet
  </code></pre>
  <p>The functions work, and so does the diagram block, the problem is I want to dynamically create the diagram block using some kind of a for loop and I do not know where to begin. Basically I need to create this block from the outputs I get from the two functions:</p>
  <pre class="lang-py prettyprint-override"><code>with Cluster(&quot;vpc-id1&quot;, graph_attr={&quot;margin&quot;: &quot;40&quot;, &quot;fontsize&quot;: &quot;18&quot;}):
      net_subnet1 = PrivateSubnet(&quot;10.XXX.Y.Z/16&quot;)
  with Cluster(&quot;vpc-id2&quot;, graph_attr={&quot;margin&quot;: &quot;40&quot;, &quot;fontsize&quot;: &quot;18&quot;}):
      net_subnet2 = PrivateSubnet(&quot;192.XXX.YYY.Z/24&quot;)
  </code></pre>
  <p>So I would basically iterate over all VPCs and for each VPC make a cluster dynamically set the ID of the VPC and then for each additional VPC append the subnet value to the next net_subnet1 or net_subnet2 and I need the <code>net_subnet</code> to also be generated. I need a little help on how to get started on this...any advice would be greatly appreciated.</p>