title: AWS CDK Python - Passing multiple variables between Stacks
body: <h2>Background</h2>
  <p>I have two Stacks within my CDK App. One is a Stack that defines Network Constructs (such as a VPC, Security Groups, etc), and one is a Stack responsible for creating an EKS cluster. The EKS Cluster needs to be able to consume variables and output from the Networking Stack as part of the cluster provisioning process. Examples below:</p>
  <p>NetworkStack:</p>
  <pre><code>class NetworkingStack(Stack):
     def __init__(self, scope: Construct, id: str, MyCidr,**kwargs) -&gt; None:
         super().__init__(scope, id)
  
         subnet_count = range(1,3)
         public_subnets = []
         worker_subnets = []
         control_subnets = []
  
         for x in subnet_count:
             x = str(x)
             control_subnets.append(ec2.SubnetConfiguration(
                 name = 'Control-0{}'.format(x),
                 cidr_mask=28,
                 subnet_type = ec2.SubnetType.PRIVATE_WITH_NAT,
                 reserved = False
             ))
             worker_subnets.append(ec2.SubnetConfiguration(
                 name = 'Worker-0{}'.format(x),
                 cidr_mask=24,
                 subnet_type = ec2.SubnetType.PRIVATE_WITH_NAT,
                 reserved = False
             ))
             public_subnets.append(ec2.SubnetConfiguration(
                 name = 'Public-0{}'.format(x),
                 cidr_mask=27,
                 map_public_ip_on_launch=True,
                 subnet_type = ec2.SubnetType.PUBLIC,
                 reserved = False
             ))
  
         kubernetes_vpc = ec2.Vpc(self, 
         &quot;Kubernetes&quot;, 
         cidr=MyCidr,
         default_instance_tenancy=ec2.DefaultInstanceTenancy.DEFAULT,
         enable_dns_hostnames=True,
         enable_dns_support=True,
         flow_logs=None,
         gateway_endpoints=None,
         max_azs=2,
         nat_gateway_provider=ec2.NatProvider.gateway(),
         nat_gateways=1, # this is 1 PER AZ
         subnet_configuration=[*public_subnets,*control_subnets,*worker_subnets],
         vpc_name=&quot;Kubernetes&quot;,
         vpn_connections=None
         )
         
  
         controlplane_security_group = ec2.SecurityGroup(self, &quot;ControlPlaneSecurityGroup&quot;, vpc=kubernetes_vpc)
         workerplane_security_group = ec2.SecurityGroup(self, &quot;WorkerPlaneSecurityGroup&quot;, vpc=kubernetes_vpc) 
         public_security_group = ec2.SecurityGroup(self, &quot;PublicPlaneSecurityGroup&quot;, vpc=kubernetes_vpc)
  </code></pre>
  <p>Now, the Cluster Stack needs to consume several of the variables created here. Specifically, it needs to be able to access the <code>kubernetes_vpc</code>, <code>public/control/worker subnets</code>, and the <code>public/control/workerplane security groups</code></p>
  <p>In the same region, same account, same CDK <code>environment</code> I have another Stack, the Cluster Stack:</p>
  <p>ClusterStack:</p>
  <pre><code>        cluster = eks.Cluster(
              self,
              &quot;InfrastructureCluster&quot;,
              default_capacity_type=eks.DefaultCapacityType.NODEGROUP,
              alb_controller=eks.AlbControllerOptions(version=&quot;latest&quot;),
              endpoint_access=eks.EndpointAccess.PUBLIC_AND_PRIVATE,
              version=eks.KubernetesVersion.V1_21,
              cluster_name=&quot;InfrastructureCluster&quot;,
              security_group=MyNetworkStack.controlplane_security_group,
              vpc=MyNetworkStack.kubernetes_vpc,
              vpc_subnets=ec2.SubnetSelection(
                  subnets=[*MyNetworkStack.control_subnets, *MyNetworkStack.worker_subnets, *MyNetworkStack.public_subnets],
              )
          )
  </code></pre>
  <p>The official <a href="https://docs.aws.amazon.com/cdk/api/v1/python/aws_cdk.aws_s3/README.html" rel="nofollow noreferrer">documentation</a> uses the example of sharing a bucket between Stacks in order to access the StackA bucket from StackB. They do this by passing the object between Stacks. This would mean I need to create an argument for each variable that I'm looking for a pass it between Stacks. Is there an easier way?</p>
  <p>I tried passing the actual NetworkingStack to the ClusterStack as such:</p>
  <pre><code>class ClusterStack(Stack):
      def __init__(self, scope: Construct, id: str, MyNetworkStack, **kwargs) -&gt; None:
          super().__init__(scope, id, **kwargs)
  </code></pre>
  <p>and then in app.py:</p>
  <pre><code>NetworkingStack(app, &quot;NetworkingStack&quot;, MyCidr=&quot;10.40.0.0/16&quot;)
  ClusterStack(app, &quot;ClusterStack&quot;, MyNetworkStack=NetworkingStack)
  </code></pre>
  <p>but that doesn't seem to work when I reference the variables in NetworkingStack as attributes e.g. <code>security_group=MyNetworkStack.controlplane_security_group</code>. The error I get usually relates to this variables specifically <code>AttributeError: type object 'NetworkingStack' has no attribute 'controlplane_security_group'</code></p>
  <h2>Question:</h2>
  <p>What is the best way to pass in multiple variables between Stacks, without having to define each variable as an argument?</p>