'How can I deploy cdk python to multiple environment to self.node.try_get_context('env') and some logic to specify where the stack gets deployed

I have created the following vpc class using python aws cdk, I need help understanding how to dynamicaly set the env variable through self.node.try_get_context('env') to represent the environment where the stack will be deployed, for example prod,dev,stg etc. since I'm reusing it in my logic to formulate naming convention for the stack.

I have assigned env variables in the cdk.json as "env_stg": "stg", "env_prd": "prd",

I can call them individually but lack understanding calling them dynamically to affect my environments on the fly.

I really appreciate any help

class VPC(Stack):

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

        env = self.node.try_get_context('env')

        self.vpc =ec2.Vpc(self, "Stg",
            cidr = '10.0.0.0/16',
            max_azs = 2,
            enable_dns_support = True,
            enable_dns_hostnames = True,
                subnet_configuration = [
                    ec2.SubnetConfiguration(
                        name = 'Public',
                        subnet_type = ec2.SubnetType.PUBLIC,
                        cidr_mask = 24
                    ),
                    ec2.SubnetConfiguration(
                        name = 'Isolated',
                        subnet_type = ec2.SubnetType.PRIVATE_ISOLATED,
                        cidr_mask = 24                      
                    )
                ]       
        )

        # Store all private subnets in Parameter store 

        private_subnets = [subnet.subnet_id for subnet in self.vpc.private_subnets]

        # public_subnets = [subnet.subnet_id for subnet in self.vpc.public_subnets]
        count = 1 
        for subnets in private_subnets:
            ssm.StringParameter(self, 'private-subnet-'+str(count),
            string_value = subnets,
            parameter_name = '/'+env+'/pivate-subnet-'+str(count)
            )
            count += 1


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source