# NatInstanceProviderV2

Modern NAT provider which uses NAT Instances.

The instance uses Amazon Linux 2023 as the operating system.

Extends: NatProvider › NatInstanceProviderV2
Implements: IConnectable

- Source: [`aws-ec2/lib/nat.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-ec2/lib/nat.ts#L738)
- Type last changed: v2.137.0 · 11 Apr 2024

## Example

```ts
const natGatewayProvider = ec2.NatProvider.instanceV2({
  instanceType: new ec2.InstanceType('t3.small'),
  defaultAllowedTraffic: ec2.NatTrafficDirection.NONE,
});
const vpc = new ec2.Vpc(this, 'Vpc', { natGatewayProvider });

const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
  vpc,
  allowAllOutbound: false,
});
securityGroup.addEgressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(443));
for (const gatewayInstance of natGatewayProvider.gatewayInstances) {
   gatewayInstance.addSecurityGroup(securityGroup);
}
```

## Initializer

```ts
new NatInstanceProviderV2(props: NatInstanceProps)
```

## Properties

- `configuredGateways`: [GatewayConfig](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.GatewayConfig.html)[] — Return list of gateways spawned by the provider.

- `connections`: [Connections](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Connections.html) — Manage the Security Groups associated with the NAT instances.

- `DEFAULT_USER_DATA_COMMANDS`: string[] (static) — Amazon Linux 2023 NAT instance user data commands Enable iptables on the instance, enable persistent IP forwarding, configure NAT on instance.

- `gatewayInstances`: [Instance](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Instance.html)[] — Array of gateway instances spawned by the provider after internal configuration.

- `securityGroup`: [ISecurityGroup](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.ISecurityGroup.html) — The Security Group associated with the NAT instances.

## Methods

- `configureNat(options: ConfigureNatOptions): void` — Called by the VPC to configure NAT.

  Don't call this directly, the VPC will call it automatically.

- `configureSubnet(subnet: PrivateSubnet): void` — Configures subnet with the gateway.

  Don't call this directly, the VPC will call it automatically.

- `gateway(props?: NatGatewayProps): NatProvider` (static) — Use NAT Gateways to provide NAT services for your VPC.

  NAT gateways are managed by AWS.

- `instance(props: NatInstanceProps): NatInstanceProvider` (static) — use instanceV2. 'instance' is deprecated since NatInstanceProvider uses a instance image that has reached EOL on Dec 31 2023

  NAT instances are managed by you, but in return allow more configuration.

  Be aware that instances created using this provider will not be
  automatically replaced if they are stopped for any reason. You should implement
  your own NatProvider based on AutoScaling groups if you need that.

- `instanceV2(props: NatInstanceProps): NatInstanceProviderV2 // chainable` (static) — Use NAT instances to provide NAT services for your VPC.

  NAT instances are managed by you, but in return allow more configuration.

  Be aware that instances created using this provider will not be
  automatically replaced if they are stopped for any reason. You should implement
  your own NatProvider based on AutoScaling groups if you need that.

- `regionalGateway(props?: RegionalNatGatewayProviderProps): RegionalNatGatewayProvider` (static) — 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.
