-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(aws-ec2) Configure NAT to Subnet Placement #874
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { cloudformation } from './ec2.generated'; | ||
import { NetworkBuilder } from './network-util'; | ||
import { DEFAULT_SUBNET_NAME, subnetId } from './util'; | ||
import { DEFAULT_SUBNET_NAME, subnetId, subnetName } from './util'; | ||
import { SubnetType, VpcNetworkRef, VpcSubnetRef } from './vpc-ref'; | ||
|
||
/** | ||
|
@@ -61,16 +61,9 @@ export interface VpcNetworkProps { | |
maxAZs?: number; | ||
|
||
/** | ||
* Define the maximum number of NAT Gateways for this VPC | ||
* | ||
* Setting this number enables a VPC to trade availability for the cost of | ||
* running a NAT Gateway. For example, if set this to 1 and your subnet | ||
* configuration is for 3 Public subnets with natGateway = `true` then only | ||
* one of the Public subnets will have a gateway and all Private subnets | ||
* will route to this NAT Gateway. | ||
* @default maxAZs | ||
* Define the NAT Gateway Configuration for this VPC | ||
*/ | ||
natGateways?: number; | ||
natGateways?: NatGatewayConfiguration; | ||
|
||
/** | ||
* Configure the subnets to build for each AZ | ||
|
@@ -123,6 +116,27 @@ export enum DefaultInstanceTenancy { | |
Dedicated = 'dedicated' | ||
} | ||
|
||
export interface NatGatewayConfiguration { | ||
/** | ||
* The number of NAT Gateways to create. | ||
* | ||
* For example, if set this to 1 and your subnet configuration is for 3 Public subnets then only | ||
* one of the Public subnets will have a gateway and all Private subnets will route to this NAT Gateway. | ||
* @default maxAZs | ||
*/ | ||
gatewayCount?: number; | ||
|
||
/** | ||
* The names of the subnets that will have NAT Gateways | ||
* | ||
* The names of the corresponding subnets in `SubnetConfiguration` that will | ||
* have a NAT Gateway. If a corresponding subnet name is not found this will | ||
* throw an error. By default the first public subnets will receive NAT | ||
* Gateways until the `gatewayCount` is reached. | ||
*/ | ||
subnetName?: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not a |
||
} | ||
|
||
/** | ||
* Specify configuration parameters for a VPC to be built | ||
*/ | ||
|
@@ -231,13 +245,6 @@ export class VpcNetwork extends VpcNetworkRef implements cdk.ITaggable { | |
*/ | ||
public readonly tags: cdk.TagManager; | ||
|
||
/** | ||
* Maximum Number of NAT Gateways used to control cost | ||
* | ||
* @default {VpcNetworkProps.maxAZs} | ||
*/ | ||
private readonly natGateways: number; | ||
|
||
/** | ||
* The VPC resource | ||
*/ | ||
|
@@ -303,8 +310,6 @@ export class VpcNetwork extends VpcNetworkRef implements cdk.ITaggable { | |
this.subnetConfiguration = ifUndefined(props.subnetConfiguration, VpcNetwork.DEFAULT_SUBNETS); | ||
const useNatGateway = this.subnetConfiguration.filter( | ||
subnet => (subnet.subnetType === SubnetType.Private)).length > 0; | ||
this.natGateways = ifUndefined(props.natGateways, | ||
useNatGateway ? this.availabilityZones.length : 0); | ||
|
||
// subnetConfiguration and natGateways must be set before calling createSubnets | ||
this.createSubnets(); | ||
|
@@ -321,12 +326,33 @@ export class VpcNetwork extends VpcNetworkRef implements cdk.ITaggable { | |
internetGatewayId: igw.ref, | ||
vpcId: this.resource.ref | ||
}); | ||
this.dependencyElements.push(igw, att); | ||
|
||
const natConfig = ifUndefined(props.natGateways, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A shorter way to get the same effect would be:
(Which works in this case because |
||
gatewayCount: undefined, | ||
subnetName: undefined, | ||
}); | ||
const natCount = ifUndefined(natConfig.gatewayCount, | ||
useNatGateway ? this.availabilityZones.length : 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, what if you set both How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment needs one more review with the latest commit |
||
const natSubnet = natConfig.subnetName; | ||
|
||
if (natSubnet !== undefined) { | ||
const subnetNames = (this.publicSubnets as VpcPublicSubnet[]).map( subnet => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest taking a Then you can |
||
return subnetName(subnet); | ||
}); | ||
if (!subnetNames.includes(natSubnet)) { | ||
throw new Error(`NatGatewayConfiguration contains subnet name ${natSubnet} which is not a Public Subnet in SubnetConfiguration`); | ||
} | ||
} | ||
|
||
(this.publicSubnets as VpcPublicSubnet[]).forEach(publicSubnet => { | ||
publicSubnet.addDefaultIGWRouteEntry(igw.ref); | ||
const currentNatCount = Object.values(this.natGatewayByAZ).length; | ||
if (addNatGatewy(publicSubnet, natSubnet, natCount, currentNatCount)) { | ||
this.natGatewayByAZ[publicSubnet.availabilityZone] = publicSubnet.addNatGateway(); | ||
} | ||
}); | ||
|
||
this.dependencyElements.push(igw, att); | ||
|
||
(this.privateSubnets as VpcPrivateSubnet[]).forEach((privateSubnet, i) => { | ||
let ngwId = this.natGatewayByAZ[privateSubnet.availabilityZone]; | ||
if (ngwId === undefined) { | ||
|
@@ -386,12 +412,6 @@ export class VpcNetwork extends VpcNetworkRef implements cdk.ITaggable { | |
switch (subnetConfig.subnetType) { | ||
case SubnetType.Public: | ||
const publicSubnet = new VpcPublicSubnet(this, name, subnetProps); | ||
if (this.natGateways > 0) { | ||
const ngwArray = Array.from(Object.values(this.natGatewayByAZ)); | ||
if (ngwArray.length < this.natGateways) { | ||
this.natGatewayByAZ[zone] = publicSubnet.addNatGateway(); | ||
} | ||
} | ||
this.publicSubnets.push(publicSubnet); | ||
break; | ||
case SubnetType.Private: | ||
|
@@ -561,5 +581,16 @@ export class VpcPrivateSubnet extends VpcSubnet { | |
} | ||
|
||
function ifUndefined<T>(value: T | undefined, defaultValue: T): T { | ||
return value !== undefined ? value : defaultValue; | ||
return value !== undefined ? value : defaultValue; | ||
} | ||
|
||
function addNatGatewy(subnet: VpcPublicSubnet, natSubnet: string | undefined, maxNats: number, natsCreated: number): boolean { | ||
const name = subnetName(subnet); | ||
if (natSubnet !== undefined && natSubnet !== name) { | ||
return false; | ||
} | ||
if (natsCreated >= maxNats) { | ||
return false; | ||
} | ||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we put both those arguments on the top-level object this doesn't even have to be a breaking change.