From 2752ef0751e9126693a3a80bc81b337dfd9c5826 Mon Sep 17 00:00:00 2001 From: Bharat Parmar Date: Mon, 27 Mar 2023 19:28:28 +0530 Subject: [PATCH] feat: import eip for natgateway (#8) * feat: import eip for natgateway * feat: import eip for natgateway --- API.md | 11 +++++++++++ src/constructs/network.ts | 19 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/API.md b/API.md index b633aa2..d8248bc 100644 --- a/API.md +++ b/API.md @@ -418,6 +418,7 @@ const vPCProps: VPCProps = { ... } | --- | --- | --- | | subnets | ISubnetsProps[] | *No description.* | | vpc | aws-cdk-lib.aws_ec2.VpcProps | *No description.* | +| natEipAllocationIds | string[] | *No description.* | | peeringConfigs | {[ key: string ]: PeeringConfig} | *No description.* | --- @@ -442,6 +443,16 @@ public readonly vpc: VpcProps; --- +##### `natEipAllocationIds`Optional + +```typescript +public readonly natEipAllocationIds: string[]; +``` + +- *Type:* string[] + +--- + ##### `peeringConfigs`Optional ```typescript diff --git a/src/constructs/network.ts b/src/constructs/network.ts index c4fecb7..a7ee28e 100644 --- a/src/constructs/network.ts +++ b/src/constructs/network.ts @@ -60,6 +60,7 @@ export interface ISubnetsProps { export interface VPCProps { readonly vpc: ec2.VpcProps; readonly peeringConfigs?: Record; + readonly natEipAllocationIds?: string[]; readonly subnets: ISubnetsProps[]; } @@ -85,7 +86,7 @@ export class Network extends Construct { this.vpc = new ec2.Vpc(this, 'VPC', props.vpc); if (props.peeringConfigs) { const convertPeeringConfig: Map = ObjToStrMap(props.peeringConfigs); - convertPeeringConfig.forEach((createVpcPeering, key)=>{ + convertPeeringConfig.forEach((createVpcPeering, key) => { let peeringConnectionIdByKey: ec2.CfnVPCPeeringConnection = new ec2.CfnVPCPeeringConnection(this, `PeerDestination-${key}`, { vpcId: this.vpc.vpcId, peerVpcId: createVpcPeering.peeringVpcId, @@ -130,16 +131,25 @@ export class Network extends Construct { pb.addDefaultInternetRoute(internetGateway.ref, att); }); if (this.natSubnets.length > 0) { - this.natProvider = ec2.NatProvider.gateway(); + if (props.natEipAllocationIds?.length != 0 && this.natSubnets.length != props.natEipAllocationIds?.length) { + // eslint-disable-next-line max-len + throw new Error( + 'natEipAllocationIds and natSubnets length should be equal', + ); + } + this.natProvider = ec2.NatProvider.gateway({ + eipAllocationIds: props.natEipAllocationIds, + }); this.natProvider.configureNat({ vpc: this.vpc, natSubnets: this.natSubnets, privateSubnets: this.pvSubnets, + }); } } - createSubnet(option: ISubnetsProps, vpc: ec2.Vpc, peeringConnectionId?: PeeringConnectionInternalType ) { + createSubnet(option: ISubnetsProps, vpc: ec2.Vpc, peeringConnectionId?: PeeringConnectionInternalType) { const subnets: ec2.Subnet[] = []; const SUBNETTYPE_TAG = 'aws-cdk:subnet-type'; const SUBNETNAME_TAG = 'aws-cdk:subnet-name'; @@ -162,6 +172,7 @@ export class Network extends Construct { cidrBlock: option.cidrBlock[index], vpcId: vpc.vpcId, mapPublicIpOnLaunch: true, + }, ) : new ec2.PrivateSubnet( @@ -175,7 +186,7 @@ export class Network extends Construct { }, ); option.routes?.forEach((route, routeIndex) => { - if (peeringConnectionId != undefined && route.existingVpcPeeringRouteKey != undefined ) { + if (peeringConnectionId != undefined && route.existingVpcPeeringRouteKey != undefined) { console.log(`peeringConnectionid ${peeringConnectionId}`); console.log(`existingVpcPeeringRouteKey ${route.existingVpcPeeringRouteKey}`); console.log(`object ${Object.keys(peeringConnectionId)}`);