Skip to content

Instantly share code, notes, and snippets.

@rcollette
Created October 4, 2020 00:50
Show Gist options
  • Save rcollette/1cdc518c69d9f03a1474ddf1d96e38b3 to your computer and use it in GitHub Desktop.
Save rcollette/1cdc518c69d9f03a1474ddf1d96e38b3 to your computer and use it in GitHub Desktop.
AWS CDK VPC Peering and Routing
private _createVpcPeering() {
// Currently, the console shows a name attribute for the peering connection but no
// name is available in the peering connection properties.
this.vpcPeeringConnection = new CfnVPCPeeringConnection(this, 'PeerToLegacyVpc', {
vpcId: this.vpc.vpcId,
peerVpcId: this.legacyVpc.vpcId,
}
);
}
private _createPeeringRoutes() {
// Create routes from legacy subnets to new VPC CIDR
const legacySubnets = this.legacyVpc.privateSubnets.concat(this.legacyVpc.publicSubnets);
let processed = [] as string[];
let i=1;
legacySubnets.forEach((subnetRef) => {
const routeTableId = subnetRef.routeTable.routeTableId;
if (!processed.includes(routeTableId)) {
processed.push(routeTableId);
this.legacyVpcToNewVpcRoutes.push(new CfnRoute(this, `RouteToNewVpc${i++}`, {
routeTableId,
vpcPeeringConnectionId: this.vpcPeeringConnection.ref,
destinationCidrBlock: this.vpc.vpcCidrBlock,
}));
}
});
// Create routes from new VPC to legacy VPC CIDR
const newSubnets = this.vpc.publicSubnets.concat(this.vpc.privateSubnets);
processed = [];
i=1;
newSubnets.forEach((subnetRef) => {
const routeTableId = subnetRef.routeTable.routeTableId;
if (!processed.includes(routeTableId)) {
processed.push(routeTableId);
this.vpcToLegacyVpcRoutes.push(new CfnRoute(this, `RouteToLegacyVpc${i++}`, {
routeTableId,
vpcPeeringConnectionId: this.vpcPeeringConnection.ref,
destinationCidrBlock: this.legacyVpc.vpcCidrBlock,
}));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment