constructor()

in source/cdk-vpc-peering/src/vpc-peering-construct.ts [26:73]


    constructor(scope: Construct, id: string, props: VpcPeeringConstructProps) {
        super(scope, id);

        const { name, vpc1, vpc2 } = props;

        console.log(`Setting up peering: ${vpc1.id} / ${vpc2.id}`);
        let peeringConnectionId = props.peeringConnectionId;

        if (peeringConnectionId === undefined) {
            const peeringConnection = new CfnVPCPeeringConnection(this,
                `peering-${vpc1.id}-${vpc2.id}`,
                {
                    vpcId: vpc1.id,
                    peerVpcId: vpc2.id,
                    peerRegion: vpc2.region,
                    tags: [{ key: "Name", value: name }]
                }
            );

            peeringConnectionId = peeringConnection.ref;
        }

        const sourceVpc = Vpc.fromLookup(this, vpc1.id, {
            vpcId: vpc1.id
        });

        let routeTableIds = [];
        for (const subnet of [...sourceVpc.publicSubnets, ...sourceVpc.privateSubnets]) {
            routeTableIds.push(subnet.routeTable.routeTableId);
        }

        let i = 0;
        routeTableIds = [...new Set(routeTableIds)];
        for (const routeTableId of routeTableIds) {
            new CfnRoute(this, `${name}-${i++}`, {
                routeTableId,
                destinationCidrBlock: vpc2.cidr,
                vpcPeeringConnectionId: peeringConnectionId
            });
        }

        const exportName = `${vpc1.id}-${vpc2.id}-PeeringConnectionId`
        new CfnOutput(this, exportName, {
            exportName,
            description: "PeeringConnectionId",
            value: peeringConnectionId || "None"
        });
    }