-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(ec2): regional NAT Gateway #36538
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
473ab77
6c3dd67
e93aa99
a24a26c
0a044a3
412860b
25bf4fd
26b5d84
85dbfbb
f502f19
1431032
2f595bb
0ac01a3
8017f0d
9d42d2a
9007845
42522c7
0d1b9db
9ca245e
b720b2e
f0d57fe
8d1c766
2af3db8
0a0956d
306acaa
773ffbb
c12feef
86032d6
10f416d
b001201
243a35f
e239513
30dfb7e
46168d1
02bff45
43e535c
ed2029d
bf06cd9
b1628a8
184729c
368f92a
df895f1
7715eb1
58a2f65
30bf7e5
2b28003
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,4 +1,5 @@ | ||
| import { Connections, IConnectable } from './connections'; | ||
| import { CfnNatGateway } from './ec2.generated'; | ||
| import { Instance } from './instance'; | ||
| import { InstanceArchitecture, InstanceType } from './instance-types'; | ||
| import { IKeyPair } from './key-pair'; | ||
|
|
@@ -9,7 +10,8 @@ import { ISecurityGroup, SecurityGroup } from './security-group'; | |
| import { UserData } from './user-data'; | ||
| import { PrivateSubnet, PublicSubnet, RouterType, Vpc } from './vpc'; | ||
| import * as iam from '../../aws-iam'; | ||
| import { Fn, Token, UnscopedValidationError } from '../../core'; | ||
| import { Duration, Fn, Token, UnscopedValidationError } from '../../core'; | ||
| import { IEIPRef } from '../../interfaces/generated/aws-ec2-interfaces.generated'; | ||
|
|
||
| /** | ||
| * Direction of traffic to allow all by default. | ||
|
|
@@ -52,8 +54,6 @@ export interface GatewayConfig { | |
| * | ||
| * Determines what type of NAT provider to create, either NAT gateways or NAT | ||
| * instance. | ||
| * | ||
| * | ||
| */ | ||
| export abstract class NatProvider { | ||
| /** | ||
|
|
@@ -100,6 +100,19 @@ export abstract class NatProvider { | |
| return new NatInstanceProviderV2(props); | ||
| } | ||
|
|
||
| /** | ||
| * Use a Regional NAT Gateway to provide NAT services for your VPC | ||
| * | ||
| * Regional NAT Gateways provide automatic multi-AZ redundancy with a single | ||
| * gateway that scales across availability zones. AWS automatically manages | ||
| * AZ coverage and EIP allocation. | ||
| * | ||
| * @see https://docs.aws.amazon.com/vpc/latest/userguide/nat-gateways-regional.html | ||
| */ | ||
| public static regionalGateway(props: RegionalNatGatewayProviderProps = {}): NatProvider { | ||
| return new RegionalNatGatewayProvider(props); | ||
| } | ||
|
|
||
| /** | ||
| * Return list of gateways spawned by the provider | ||
| */ | ||
|
|
@@ -158,9 +171,41 @@ export interface NatGatewayProps { | |
| } | ||
|
|
||
| /** | ||
| * Properties for a NAT instance | ||
| * | ||
| * Properties for a Regional NAT Gateway Provider | ||
| * | ||
| * Regional NAT Gateways provide automatic multi-AZ redundancy with a single | ||
| * gateway that scales across availability zones. | ||
| */ | ||
| export interface RegionalNatGatewayProviderProps { | ||
| /** | ||
| * Maximum amount of time to wait before forcibly releasing IP addresses | ||
| * if connections are still in progress. | ||
| * | ||
| * @default Duration.seconds(350) | ||
| */ | ||
| readonly maxDrainDuration?: Duration; | ||
|
Contributor
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.
Contributor
Author
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've added a validation and |
||
|
|
||
| /** | ||
| * The allocation ID of the Elastic IP address to use for this NAT gateway. | ||
| * | ||
| * Cannot be specified together with `eip`. | ||
| * | ||
| * @default - A new EIP is automatically allocated | ||
| */ | ||
| readonly allocationId?: string; | ||
|
|
||
| /** | ||
| * Reference to an existing EIP to use for this NAT gateway. | ||
| * | ||
| * Cannot be specified together with `allocationId`. | ||
| * | ||
| * @default - A new EIP is automatically allocated | ||
| */ | ||
| readonly eip?: IEIPRef; | ||
|
Comment on lines
+237
to
+255
Contributor
Author
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. The As implementation approaches, there are two options: (i) define the L2 argument as a union, or (ii) provide separate arguments for (i) union typeredaonly eip?: string | IETPRef;(ii) separate argumentreadonly allocationId?: string;
redaonly eip?: IETPRef;
Contributor
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.
Contributor
Author
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. Due to a recent L1 fix,
Contributor
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. Agree. Thanks for clarifying! |
||
| } | ||
|
|
||
| /** | ||
| * Properties for a NAT instance | ||
| */ | ||
| export interface NatInstanceProps { | ||
| /** | ||
|
|
@@ -333,6 +378,62 @@ export class NatGatewayProvider extends NatProvider { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Provider for Regional NAT Gateways | ||
| * | ||
| * Regional NAT Gateways provide automatic multi-AZ redundancy with a single gateway that scales across availability zones. | ||
| * Unlike zonal NAT gateways, a regional NAT gateway does not require a public subnet and is created at the VPC level. | ||
| * | ||
| * @see https://docs.aws.amazon.com/vpc/latest/userguide/nat-gateways-regional.html | ||
| */ | ||
| export class RegionalNatGatewayProvider extends NatProvider { | ||
| private natGateway?: CfnNatGateway; | ||
|
|
||
| constructor(private readonly props: RegionalNatGatewayProviderProps = {}) { | ||
| super(); | ||
|
|
||
| if (this.props.allocationId && this.props.eip) { | ||
| throw new UnscopedValidationError( | ||
| 'Cannot specify both allocationId and eip. Use one or the other.', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public configureNat(options: ConfigureNatOptions) { | ||
| this.natGateway = new CfnNatGateway(options.vpc, 'RegionalNatGateway', { | ||
| vpcId: options.vpc.vpcId, | ||
| availabilityMode: 'regional', | ||
| connectivityType: 'public', | ||
| allocationId: this.props.allocationId ?? this.props.eip, | ||
| maxDrainDurationSeconds: this.props.maxDrainDuration?.toSeconds(), | ||
| }); | ||
|
|
||
| // Add routes to the regional NAT gateway in all private subnets | ||
| for (const sub of options.privateSubnets) { | ||
| this.configureSubnet(sub); | ||
| } | ||
| } | ||
|
|
||
| public configureSubnet(subnet: PrivateSubnet) { | ||
| if (!this.natGateway) { | ||
| throw new UnscopedValidationError('Cannot configure subnet before configuring NAT gateway'); | ||
| } | ||
| // All private subnets use the same regional NAT gateway ID | ||
| subnet.addRoute('DefaultRoute', { | ||
| routerType: RouterType.NAT_GATEWAY, | ||
| routerId: this.natGateway.attrNatGatewayId, | ||
| enablesInternetConnectivity: true, | ||
| }); | ||
| } | ||
|
|
||
| public get configuredGateways(): GatewayConfig[] { | ||
| // Regional NAT gateway is a single gateway covering all AZs | ||
| return this.natGateway | ||
| ? [{ az: 'regional', gatewayId: this.natGateway.attrNatGatewayId }] | ||
|
Contributor
Author
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. Because |
||
| : []; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * NAT provider which uses NAT Instances | ||
| * | ||
|
|
||
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.
Default value is described in the docs.
https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-natgateway.html#cfn-ec2-natgateway-maxdraindurationseconds