Skip to content
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(ec2): create NAT Gateways with fixed IPs #14250

Merged
merged 5 commits into from Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 38 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/nat.ts
@@ -1,4 +1,5 @@
import * as iam from '@aws-cdk/aws-iam';
import { Fn, Token } from '@aws-cdk/core';
import { Connections, IConnectable } from './connections';
import { Instance } from './instance';
import { InstanceType } from './instance-types';
Expand Down Expand Up @@ -59,8 +60,8 @@ export abstract class NatProvider {
*
* @see https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html
*/
public static gateway(): NatProvider {
return new NatGatewayProvider();
public static gateway(props: NatGatewayProps = {}): NatProvider {
return new NatGatewayProvider(props);
}

/**
Expand Down Expand Up @@ -122,6 +123,19 @@ export interface ConfigureNatOptions {
readonly privateSubnets: PrivateSubnet[];
}

/**
* Properties for a NAT gateway
*
*/
export interface NatGatewayProps {
/**
* EIP allocation IDs for the NAT gateways
nija-at marked this conversation as resolved.
Show resolved Hide resolved
*
* @default - No fixed EIPs allocated for the NAT gateways
*/
readonly eipAllocationIds?: string[];
}

/**
* Properties for a NAT instance
*
Expand Down Expand Up @@ -203,11 +217,20 @@ export interface NatInstanceProps {
class NatGatewayProvider extends NatProvider {
private gateways: PrefSet<string> = new PrefSet<string>();

constructor(private readonly props: NatGatewayProps = {}) {
super();
}

public configureNat(options: ConfigureNatOptions) {
// Create the NAT gateways
let i = 0;
for (const sub of options.natSubnets) {
const gateway = sub.addNatGateway();
if (this.props.eipAllocationIds) {
gateway.allocationId = pickN(i, this.props.eipAllocationIds);
}
this.gateways.add(sub.availabilityZone, gateway.ref);
i++;
}

// Add routes to them in the private subnets
Expand Down Expand Up @@ -377,4 +400,17 @@ function isOutboundAllowed(direction: NatTrafficDirection) {

function isInboundAllowed(direction: NatTrafficDirection) {
return direction === NatTrafficDirection.INBOUND_AND_OUTBOUND;
}

/**
* Token-aware pick index function
*/
function pickN(i: number, xs: string[]) {
if (Token.isUnresolved(xs)) { return Fn.select(i, xs); }

if (i >= xs.length) {
throw new Error(`Cannot get element ${i} from ${xs}`);
}

return xs[i];
}
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/vpc.test.ts
Expand Up @@ -855,6 +855,24 @@ nodeunitShim({

test.done();
},

'NAT gateway provider with EIP allocations'(test: Test) {
const stack = new Stack();
const natGatewayProvider = NatProvider.gateway({
eipAllocationIds: ['a', 'b', 'c', 'd'],
});
new Vpc(stack, 'VpcNetwork', { natGatewayProvider });

cdkExpect(stack).to(haveResource('AWS::EC2::NatGateway', {
AllocationId: 'a',
}));
cdkExpect(stack).to(haveResource('AWS::EC2::NatGateway', {
AllocationId: 'b',
}));

test.done();
},

'Can add an IPv6 route'(test: Test) {
// GIVEN
const stack = getTestStack();
Expand Down