# SubnetMapping

Specifies a subnet for a load balancer.

- Source: [`aws-elasticloadbalancingv2/lib/shared/base-load-balancer.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-elasticloadbalancingv2/lib/shared/base-load-balancer.ts#L57)
- Type last changed: v2.190.0 · 17 Apr 2025

## Example

TypeScript:

```ts
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import { aws_ec2 as ec2 } from 'aws-cdk-lib';
import { aws_elasticloadbalancingv2 as elbv2 } from 'aws-cdk-lib';

declare const sourceNatIpv6Prefix: elbv2.SourceNatIpv6Prefix;
declare const subnet: ec2.Subnet;
const subnetMapping: elbv2.SubnetMapping = {
  subnet: subnet,

  // the properties below are optional
  allocationId: 'allocationId',
  ipv6Address: 'ipv6Address',
  privateIpv4Address: 'privateIpv4Address',
  sourceNatIpv6Prefix: sourceNatIpv6Prefix,
};
```

Python:

```python
# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
from aws_cdk import aws_ec2 as ec2
from aws_cdk import aws_elasticloadbalancingv2 as elbv2

# source_nat_ipv6_prefix: elbv2.SourceNatIpv6Prefix
# subnet: ec2.Subnet

subnet_mapping = elbv2.SubnetMapping(
    subnet=subnet,

    # the properties below are optional
    allocation_id="allocationId",
    ipv6_address="ipv6Address",
    private_ipv4_address="privateIpv4Address",
    source_nat_ipv6_prefix=source_nat_ipv6_prefix
)
```

Java:

```java
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import software.amazon.awscdk.services.ec2.*;
import software.amazon.awscdk.services.elasticloadbalancingv2.*;

SourceNatIpv6Prefix sourceNatIpv6Prefix;
Subnet subnet;

SubnetMapping subnetMapping = SubnetMapping.builder()
        .subnet(subnet)

        // the properties below are optional
        .allocationId("allocationId")
        .ipv6Address("ipv6Address")
        .privateIpv4Address("privateIpv4Address")
        .sourceNatIpv6Prefix(sourceNatIpv6Prefix)
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
using Amazon.CDK.AWS.EC2;
using Amazon.CDK.AWS.ElasticLoadBalancingV2;

SourceNatIpv6Prefix sourceNatIpv6Prefix;
Subnet subnet;

var subnetMapping = new SubnetMapping {
    Subnet = subnet,

    // the properties below are optional
    AllocationId = "allocationId",
    Ipv6Address = "ipv6Address",
    PrivateIpv4Address = "privateIpv4Address",
    SourceNatIpv6Prefix = sourceNatIpv6Prefix
};
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

var sourceNatIpv6Prefix SourceNatIpv6Prefix
var subnet Subnet

subnetMapping := &SubnetMapping{
	Subnet: subnet,

	// the properties below are optional
	AllocationId: jsii.String("allocationId"),
	Ipv6Address: jsii.String("ipv6Address"),
	PrivateIpv4Address: jsii.String("privateIpv4Address"),
	SourceNatIpv6Prefix: sourceNatIpv6Prefix,
}
```

## Properties

- `allocationId`: string — The allocation ID of the Elastic IP address for an internet-facing load balancer.

  **Default:** undefined - AWS default is to allocate a new IP address for internet-facing load balancers

- `ipv6Address`: string — The IPv6 address.

  **Default:** undefined - AWS default is to allocate an IPv6 address from the subnet's pool

- `privateIpv4Address`: string — The private IPv4 address for an internal load balancer.

  **Default:** undefined - AWS default is to allocate a private IPv4 address from the subnet's pool

- `sourceNatIpv6Prefix`: [SourceNatIpv6Prefix](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_elasticloadbalancingv2.SourceNatIpv6Prefix.html) — The IPv6 prefix to use for source NAT for a dual-stack network load balancer with UDP listeners.

  **Default:** undefined - AWS default is `SourceNatIpv6Prefix.autoAssigned()` for IPv6 load balancers

  Specify an IPv6 prefix (/80 netmask) from the subnet CIDR block
  or `SourceNatIpv6Prefix.autoAssigned()` to use an IPv6 prefix selected at random from the subnet CIDR block.

- `subnet`: [ISubnet](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.ISubnet.html) (required) — The subnet.
