You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/aws-cdk-lib/aws-ec2/README.md
+63
Original file line number
Diff line number
Diff line change
@@ -2603,3 +2603,66 @@ new ec2.Instance(this, 'Instance', {
2603
2603
instanceProfile,
2604
2604
});
2605
2605
```
2606
+
2607
+
### Using IPAM for Subnet CIDR Allocation
2608
+
2609
+
Instead of specifying a CIDR block directly, you can allocate a CIDR from an [IPAM pool](https://docs.aws.amazon.com/vpc/latest/ipam/what-it-is-ipam.html) for your subnet. This allows for more flexible and managed IP address allocation.
2610
+
2611
+
For IPv4, use the `ipv4IpamAllocation` property:
2612
+
2613
+
```ts
2614
+
import*asec2from'aws-cdk-lib/aws-ec2';
2615
+
2616
+
const vpc =newec2.Vpc(this, 'Vpc');
2617
+
2618
+
// Create a subnet with IPv4 CIDR allocated from an IPAM pool
2619
+
newec2.Subnet(this, 'Subnet', {
2620
+
vpcId: vpc.vpcId,
2621
+
availabilityZone: 'us-east-1a',
2622
+
ipv4IpamAllocation: {
2623
+
ipamPoolId: 'ipam-pool-12345',
2624
+
netmaskLength: 24, // Optional: if not specified, uses the default netmask length from the IPAM pool
2625
+
},
2626
+
});
2627
+
```
2628
+
2629
+
For IPv6, use the `ipv6IpamAllocation` property:
2630
+
2631
+
```ts
2632
+
// Create a subnet with IPv6 CIDR allocated from an IPAM pool
2633
+
newec2.Subnet(this, 'Subnet', {
2634
+
vpcId: vpc.vpcId,
2635
+
availabilityZone: 'us-east-1a',
2636
+
cidrBlock: '10.0.0.0/24', // IPv4 CIDR is required
2637
+
ipv6IpamAllocation: {
2638
+
ipamPoolId: 'ipam-pool-67890',
2639
+
netmaskLength: 64, // Optional: if not specified, uses the default netmask length from the IPAM pool
2640
+
},
2641
+
assignIpv6AddressOnCreation: true,
2642
+
});
2643
+
```
2644
+
2645
+
You can also use both IPv4 and IPv6 IPAM allocations together:
2646
+
2647
+
```ts
2648
+
// Create a subnet with both IPv4 and IPv6 CIDR allocated from IPAM pools
2649
+
newec2.Subnet(this, 'Subnet', {
2650
+
vpcId: vpc.vpcId,
2651
+
availabilityZone: 'us-east-1a',
2652
+
ipv4IpamAllocation: {
2653
+
ipamPoolId: 'ipam-pool-12345',
2654
+
// netmaskLength is optional and will use the IPAM pool's default if not specified
2655
+
},
2656
+
ipv6IpamAllocation: {
2657
+
ipamPoolId: 'ipam-pool-67890',
2658
+
// netmaskLength is optional and will use the IPAM pool's default if not specified
2659
+
},
2660
+
assignIpv6AddressOnCreation: true,
2661
+
});
2662
+
```
2663
+
2664
+
Note that:
2665
+
- You cannot specify both `cidrBlock` and `ipv4IpamAllocation` at the same time
2666
+
- You cannot specify both `ipv6CidrBlock` and `ipv6IpamAllocation` at the same time
2667
+
- If you specify `assignIpv6AddressOnCreation: true`, you must also specify either `ipv6CidrBlock` or `ipv6IpamAllocation`
2668
+
- The `netmaskLength` property in both `ipv4IpamAllocation` and `ipv6IpamAllocation` is optional. If not specified, the default netmask length configured in the IPAM pool will be used.
0 commit comments