Skip to content

Commit

Permalink
feat: import eip for natgateway (#8)
Browse files Browse the repository at this point in the history
* feat: import eip for natgateway

* feat: import eip for natgateway
  • Loading branch information
mr-robot-in authored Mar 27, 2023
1 parent daa6719 commit 2752ef0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
11 changes: 11 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions src/constructs/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface ISubnetsProps {
export interface VPCProps {
readonly vpc: ec2.VpcProps;
readonly peeringConfigs?: Record<string, PeeringConfig>;
readonly natEipAllocationIds?: string[];
readonly subnets: ISubnetsProps[];
}

Expand All @@ -85,7 +86,7 @@ export class Network extends Construct {
this.vpc = new ec2.Vpc(this, 'VPC', props.vpc);
if (props.peeringConfigs) {
const convertPeeringConfig: Map<string, PeeringConfig> = 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,
Expand Down Expand Up @@ -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';
Expand All @@ -162,6 +172,7 @@ export class Network extends Construct {
cidrBlock: option.cidrBlock[index],
vpcId: vpc.vpcId,
mapPublicIpOnLaunch: true,

},
)
: new ec2.PrivateSubnet(
Expand All @@ -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)}`);
Expand Down

0 comments on commit 2752ef0

Please sign in to comment.