Skip to content

aws-ec2: Export NatGatewayProvider #28372

@crh23

Description

@crh23

Describe the feature

(originally posted as a discussion)

There are two implementations of NatProvider: NatGatewayProvider and NatInstanceProvider.

Of the two, only NatInstanceProvider is exported. Both are usable through aws_ec2.NatProvider.instance() and aws_ec2.NatProvider.gateway() respectively, but since only NatInstanceProvider is exported the docs indicate that NatProvider is only implemented by NatInstanceProvider.

I suggest that NatGatewayProvider also be exported, so that both providers are available through both interfaces and so the docs correctly reflect the two available implementations of NatProvider.

Use Case

No real new functionality is exposed, except the ability to instantiate NatGatewayProvider directly instead of using the static method of NatProvider. The main advantage is the implied update to the docs.

Proposed Solution

I think this is as simple as inserting an export at the start of L217

/**
* Provider for NAT Gateways
*/
class NatGatewayProvider extends NatProvider {
private gateways: PrefSet<string> = new PrefSet<string>();
constructor(private readonly props: NatGatewayProps = {}) {
super();
}
public configureNat(options: ConfigureNatOptions) {
if (
this.props.eipAllocationIds != null
&& !Token.isUnresolved(this.props.eipAllocationIds)
&& this.props.eipAllocationIds.length < options.natSubnets.length
) {
throw new Error(`Not enough NAT gateway EIP allocation IDs (${this.props.eipAllocationIds.length} provided) for the requested subnet count (${options.natSubnets.length} needed).`);
}
// Create the NAT gateways
let i = 0;
for (const sub of options.natSubnets) {
const eipAllocationId = this.props.eipAllocationIds ? pickN(i, this.props.eipAllocationIds) : undefined;
const gateway = sub.addNatGateway(eipAllocationId);
this.gateways.add(sub.availabilityZone, gateway.ref);
i++;
}
// Add routes to them in the private subnets
for (const sub of options.privateSubnets) {
this.configureSubnet(sub);
}
}
public configureSubnet(subnet: PrivateSubnet) {
const az = subnet.availabilityZone;
const gatewayId = this.gateways.pick(az);
subnet.addRoute('DefaultRoute', {
routerType: RouterType.NAT_GATEWAY,
routerId: gatewayId,
enablesInternetConnectivity: true,
});
}
public get configuredGateways(): GatewayConfig[] {
return this.gateways.values().map(x => ({ az: x[0], gatewayId: x[1] }));
}
}

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.115.0

Environment details (OS name and version, etc.)

Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ec2Related to Amazon Elastic Compute Cloudeffort/mediumMedium work item – several days of effortfeature-requestA feature should be added or improved.p2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions