AWS-CDK: cannot consume a cross reference from stack (creating a utility class)

Viewed 22

I'm trying to create a utility class in a separate file that would supply things like vpcid, subnetids, availability zones etc after reading these from AWS Parameter store. I would like to keep it portable and separate i.e. it can utilized by multiple stacks across different repos.

I know I could have used boto3 as well but just curious if I can make the below code work without initializing it as a CDK stack in the first place and consequently running into 'cross reference' issue. And if something like this can be done then how would I call or create its object from another CDK stack?

 from aws_cdk import aws_ec2 as ec2
 from aws_cdk import aws_ssm as ssm
 from aws_cdk import core as cdk


class VPC(cdk.Stack):

    def __init__(self, scope: cdk.Construct, construct_id: str, my_environment: dict, **kwargs) -> None:
    super().__init__(scope, construct_id, **kwargs)

    self.targetlist = list()
    self.targetport = 443
    self.ssl_policyvalue = "ELBSecurityPolicy-TLS-1-2-2017-01"
    self.ipaddreslist = []
    self.execute_api_vpce = ''
    self.routetableidlist = []
    self.availabiltyzonelist = []
    self.vpc_api_gw_securitygroup_id = ''

    self.vpc = ec2.Vpc.from_vpc_attributes(self, id="xyz-vpc",
                                           availability_zones=self.availabiltyzonelist,
                                           vpc_id=self.vpcid,
                                           vpc_cidr_block=self.vpccidr,
                                           private_subnet_ids=self.subnetlist,
                                           private_subnet_route_table_ids=self.routetableidlist
                                           )

TIA

0 Answers
Related