aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(aws-ec2): Support IPv6 IPAM allocation in L2 Subnet construct

Open gauranshahuja opened this issue 2 weeks ago • 1 comments

Describe the feature

AWS::EC2::Subnet can allocate its IPv6 CIDR block from an Amazon VPC IP Address Manager pool (Ipv6IpamPoolId + Ipv6NetmaskLength), but the L2 Subnet construct only accepts a concrete ipv6CidrBlock. #34296 (PR #38810) adds IPv4 IPAM allocation through ipv4IpamAllocation: SubnetIpv4IpamAllocation; this is the IPv6 counterpart.

Use Case

Teams that manage IPv6 address space with IPAM pools have to drop to CfnSubnet, or override the L1 properties, to give an L2 subnet an IPAM-allocated IPv6 block.

Proposed Solution

new ec2.Subnet(stack, 'Subnet', {
  vpcId: vpc.vpcId,
  availabilityZone: vpc.availabilityZones[0],
  cidrBlock: '10.0.0.0/24',
  ipv6IpamAllocation: { ipamPool: ipv6Pool, netmaskLength: 64 },
  assignIpv6AddressOnCreation: true,
});
  • New optional ipv6IpamAllocation?: SubnetIpv6IpamAllocation ({ ipamPool: IIPAMPoolRef, netmaskLength: number }), next to SubnetIpv4IpamAllocation.
  • Mutually exclusive with ipv6CidrBlock, validated at synth time.
  • assignIpv6AddressOnCreation works with either source of the IPv6 block.

Other Information

Follow-up to #34296. During review of #38810 the IPv4 struct was named SubnetIpv4IpamAllocation so that an IPv6 struct could sit next to it. Workaround today: CfnSubnet or a property override.

Acknowledgements

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

AWS CDK Library version (aws-cdk-lib)

2.277.0

AWS CDK CLI version

N/A (construct library feature)

Environment details (OS name and version, etc.)

Windows 10 / macOS

gauranshahuja avatar Nov 16 '26 08:11 gauranshahuja

Hi @gauranshahuja,

Thanks for opening this and for the PR. Here is how it looks at a glance:

┌──────────────────────────┐     ┌──────────────────────────┐     ┌──────────────────────────┐
│ Subnet L2 (2.277.0)      │ ──> │ CfnSubnet                │ ──> │ AWS::EC2::Subnet         │
│ no ipv6IpamAllocation    │     │ no Ipv6IpamPoolId /      │     │ cannot set IPv6 IPAM     │
│ prop                     │     │ Ipv6NetmaskLength passed │     │ pool via the L2          │
└──────────────────────────┘     └──────────────────────────┘     └──────────────────────────┘

I could reproduce this with cdk synth on 2.277.0, which is both the reported and the latest release. ipv6IpamAllocation fails to compile (TS2353), and an L2 Subnet never emits Ipv6IpamPoolId or Ipv6NetmaskLength. From my view this is a feature gap rather than a regression. SubnetProps only has ipv6CidrBlock and assignIpv6AddressOnCreation, and the constructor passes just those to the L1:

https://github.com/aws/aws-cdk/blob/1c78b1b74386cb0e2b9c8bb736487bb99fccb98c/packages/aws-cdk-lib/aws-ec2/lib/vpc.ts#L2244-L2251

The IPv4 half (ipv4IpamAllocation, #38810) was merged after 2.277.0, so it is on main but not yet in a release:

https://github.com/aws/aws-cdk/blob/16c42cbd230f0a7d4d87566cbddfd3ea0181ff58/packages/aws-cdk-lib/aws-ec2/lib/vpc.ts#L2299-L2308

The linked PR is #39752. It is open with changes requested: the PR linter requires an integ test with a deployed snapshot (integ.subnet-ipv6-ipam.js.snapshot/), and an exemption request is pending. A maintainer or another contributor may be able to help with the snapshot.

In the meantime, a workaround that I verified with cdk synth on 2.277.0 is the Mixin from @aws-cdk/cfn-property-mixins (same version as aws-cdk-lib, and it needs moduleResolution node16 or bundler):

import { CfnSubnetPropsMixin } from '@aws-cdk/cfn-property-mixins/aws-ec2';

const subnet = new ec2.Subnet(this, 'Subnet', { vpcId, availabilityZone, cidrBlock: '10.0.0.0/24',
  assignIpv6AddressOnCreation: true });
subnet.with(new CfnSubnetPropsMixin({ ipv6IpamPoolId: pool.ref, ipv6NetmaskLength: 64 }));

Please apply .with() after the Subnet is created, on the Subnet rather than the Stack. Do not also set ipv6CidrBlock, since the Mixin does not validate that. The equivalent fallback, which synthesizes the same template, is:

const cfn = subnet.node.defaultChild as ec2.CfnSubnet;
cfn.addPropertyOverride('Ipv6IpamPoolId', pool.ref);
cfn.addPropertyOverride('Ipv6NetmaskLength', 64);

pahud avatar Nov 16 '26 19:11 pahud