Skip to content
v2.273.0

NatInstanceProviderV2 Class ​

Modern NAT provider which uses NAT Instances.

The instance uses Amazon Linux 2023 as the operating system.

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);
}
python
nat_gateway_provider = ec2.NatProvider.instance_v2(
    instance_type=ec2.InstanceType("t3.small"),
    default_allowed_traffic=ec2.NatTrafficDirection.NONE
)
vpc = ec2.Vpc(self, "Vpc", nat_gateway_provider=nat_gateway_provider)

security_group = ec2.SecurityGroup(self, "SecurityGroup",
    vpc=vpc,
    allow_all_outbound=False
)
security_group.add_egress_rule(ec2.Peer.any_ipv4(), ec2.Port.tcp(443))
for gateway_instance in nat_gateway_provider.gateway_instances:
    gateway_instance.add_security_group(security_group)
java
NatInstanceProviderV2 natGatewayProvider = NatProvider.instanceV2(NatInstanceProps.builder()
        .instanceType(new InstanceType("t3.small"))
        .defaultAllowedTraffic(NatTrafficDirection.NONE)
        .build());
Vpc vpc = Vpc.Builder.create(this, "Vpc").natGatewayProvider(natGatewayProvider).build();

SecurityGroup securityGroup = SecurityGroup.Builder.create(this, "SecurityGroup")
        .vpc(vpc)
        .allowAllOutbound(false)
        .build();
securityGroup.addEgressRule(Peer.anyIpv4(), Port.tcp(443));
for (Object gatewayInstance : natGatewayProvider.getGatewayInstances()) {
    gatewayInstance.addSecurityGroup(securityGroup);
}
csharp
// Install: dotnet add package Amazon.CDK.Lib
var natGatewayProvider = NatProvider.InstanceV2(new NatInstanceProps {
    InstanceType = new InstanceType("t3.small"),
    DefaultAllowedTraffic = NatTrafficDirection.NONE
});
var vpc = new Vpc(this, "Vpc", new VpcProps { NatGatewayProvider = natGatewayProvider });

var securityGroup = new SecurityGroup(this, "SecurityGroup", new SecurityGroupProps {
    Vpc = vpc,
    AllowAllOutbound = false
});
securityGroup.AddEgressRule(Peer.AnyIpv4(), Port.Tcp(443));
for (var gatewayInstance in natGatewayProvider.GatewayInstances)
{
    gatewayInstance.AddSecurityGroup(securityGroup);
}
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
natGatewayProvider := ec2.NatProvider_InstanceV2(&NatInstanceProps{
	InstanceType: ec2.NewInstanceType(jsii.String("t3.small")),
	DefaultAllowedTraffic: ec2.NatTrafficDirection_NONE,
})
vpc := ec2.NewVpc(this, jsii.String("Vpc"), &VpcProps{
	NatGatewayProvider: NatGatewayProvider,
})

securityGroup := ec2.NewSecurityGroup(this, jsii.String("SecurityGroup"), &SecurityGroupProps{
	Vpc: Vpc,
	AllowAllOutbound: jsii.Boolean(false),
})
securityGroup.AddEgressRule(ec2.Peer_AnyIpv4(), ec2.Port_Tcp(jsii.Number(443)))
for _, gatewayInstance := range natGatewayProvider.gatewayInstances {
	gatewayInstance.AddSecurityGroup(securityGroup)
}

Initializer ​

ts
new NatInstanceProviderV2(props: NatInstanceProps)
python
NatInstanceProviderV2(props: NatInstanceProps)
java
new NatInstanceProviderV2(NatInstanceProps props)
csharp
new NatInstanceProviderV2(NatInstanceProps props)
go
NewNatInstanceProviderV2(Props NatInstanceProps)

Properties ​

configuredGatewaysGatewayConfig[]configured_gatewaystyping.List[GatewayConfig]configuredGatewaysList<GatewayConfig>ConfiguredGatewaysGatewayConfig[]ConfiguredGateways*[]GatewayConfigrequired#
Return list of gateways spawned by the provider.
connectionsConnectionsconnectionsConnectionsconnectionsConnectionsConnectionsConnectionsConnectionsConnectionsrequired#
Manage the Security Groups associated with the NAT instances.
DEFAULT_USER_DATA_COMMANDSstring[]default_user_data_commandstyping.List[str]DEFAULT_USER_DATA_COMMANDSList<String>DEFAULT_USER_DATA_COMMANDSstring[]DEFAULT_USER_DATA_COMMANDS*[]*stringrequiredstatic#
Amazon Linux 2023 NAT instance user data commands Enable iptables on the instance, enable persistent IP forwarding, configure NAT on instance.
gatewayInstancesInstance[]gateway_instancestyping.List[Instance]gatewayInstancesList<Instance>GatewayInstancesInstance[]GatewayInstances*[]Instancerequired#
Array of gateway instances spawned by the provider after internal configuration.
securityGroupISecurityGroupsecurity_groupISecurityGroupsecurityGroupISecurityGroupSecurityGroupISecurityGroupSecurityGroupISecurityGrouprequired#
The Security Group associated with the NAT instances.

Methods ​

configureNat(options: ConfigureNatOptions): voiddef configure_nat(self, options: ConfigureNatOptions) -> Nonepublic void configureNat(ConfigureNatOptions options)public void ConfigureNat(ConfigureNatOptions options)func ConfigureNat(Options ConfigureNatOptions)#
Called by the VPC to configure NAT.

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

configureSubnet(subnet: PrivateSubnet): voiddef configure_subnet(self, subnet: PrivateSubnet) -> Nonepublic void configureSubnet(PrivateSubnet subnet)public void ConfigureSubnet(PrivateSubnet subnet)func ConfigureSubnet(Subnet PrivateSubnet)#
Configures subnet with the gateway.

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

gateway(props?: NatGatewayProps): NatProvider@staticmethod def gateway(props: typing.Optional[NatGatewayProps] = None) -> NatProviderpublic static NatProvider gateway(NatGatewayProps props)public static NatProvider Gateway(NatGatewayProps? props = null)func Gateway(Props NatGatewayProps) NatProviderstatic#
Use NAT Gateways to provide NAT services for your VPC.

NAT gateways are managed by AWS.

instance(props: NatInstanceProps): NatInstanceProvider@staticmethod def instance(props: NatInstanceProps) -> NatInstanceProviderpublic static NatInstanceProvider instance(NatInstanceProps props)public static NatInstanceProvider Instance(NatInstanceProps props)func Instance(Props NatInstanceProps) NatInstanceProviderstaticdeprecated#
use instanceV2. 'instance' is deprecated since NatInstanceProvider uses a instance image that has reached EOL on Dec 31 2023
notes

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@staticmethod def instance_v2(props: NatInstanceProps) -> NatInstanceProviderV2public static NatInstanceProviderV2 instanceV2(NatInstanceProps props)public static NatInstanceProviderV2 InstanceV2(NatInstanceProps props)func InstanceV2(Props NatInstanceProps) NatInstanceProviderV2static#
Use NAT instances to provide NAT services for your VPC.
notes

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@staticmethod def regional_gateway(props: typing.Optional[RegionalNatGatewayProviderProps] = None) -> RegionalNatGatewayProviderpublic static RegionalNatGatewayProvider regionalGateway(RegionalNatGatewayProviderProps props)public static RegionalNatGatewayProvider RegionalGateway(RegionalNatGatewayProviderProps? props = null)func RegionalGateway(Props RegionalNatGatewayProviderProps) RegionalNatGatewayProviderstatic#
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.