Skip to content

feat(ec2): support IPAM CIDR allocation for Subnet - #38810

Merged
mergify[bot] merged 12 commits into
aws:mainfrom
gauranshahuja:34296/subnet-ipam
Nov 16, 2026
Merged

mergify[bot] merged 12 commits into
aws:mainfrom
gauranshahuja:34296/subnet-ipam

Conversation

@gauranshahuja

@gauranshahuja gauranshahuja commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Issue # (if applicable)

Closes #34296.

Reason for this change

Subnet (and PublicSubnet/PrivateSubnet) require a concrete cidrBlock, while AWS::EC2::Subnet can allocate its IPv4 CIDR from an Amazon VPC IP Address Manager pool (Ipv4IpamPoolId + Ipv4NetmaskLength). Vpc already supports IPAM for the VPC CIDR through IpAddresses.awsIpamAllocation(), but a standalone L2 subnet cannot use IPAM without dropping to the L1, or overriding the L1 properties with CfnSubnetPropsMixin and a placeholder cidrBlock.

Description of changes

  • SubnetProps.cidrBlock becomes optional and a new optional ipv4IpamAllocation: SubnetIpv4IpamAllocation prop is added, where SubnetIpv4IpamAllocation is { ipamPool: IIPAMPoolRef, netmaskLength: number }.
  • Subnet validates that exactly one of cidrBlock/ipv4IpamAllocation is set and that netmaskLength is within /16 to /28 (the IPv4 subnet sizes the package already enforces in NetworkBuilder; skipped for tokens), using ValidationError with lit codes.
  • Ipv4IpamPoolId/Ipv4NetmaskLength are passed to CfnSubnet. When IPAM is used, subnet.ipv4CidrBlock is CfnSubnet.attrCidrBlock (a deploy-time token) instead of a concrete string, the same way Vpc.vpcCidrBlock is attrCidrBlock for an IPAM-allocated VPC.
  • README: new "Allocating a subnet CIDR from AWS IPAM" section.

Design decisions, flagged for review:

  1. Nested struct rather than flat ipv4IpamPoolId/ipv4NetmaskLength: the two values are only valid together (co-dependent props rule in docs/AGENTS_CONSTRUCT_DESIGN.md). Exclusivity with the pre-existing cidrBlock cannot be made structural without adding a second, factory-style way to express the CIDR, so it is validated at construction time as proposed in the issue.
  2. ipamPool: IIPAMPoolRef rather than an ID string, following the preference for I*Ref interfaces in props (as in placementGroup?: IPlacementGroupRef, networkAcl: INetworkAclRef). CfnIPAMPool implements it directly and CfnIPAMPool.fromIpamPoolId() covers pools created elsewhere. AwsIpamProps.ipv4IpamPoolId is still a string (with a "todo: should be a type" comment); happy to switch to a string if consistency with it is preferred.
  3. netmaskLength is required, mirroring AwsIpamProps.ipv4NetmaskLength; relaxing it to optional later is non-breaking.
  4. Not implemented as a mixin: the blocker is the L2 props contract (cidrBlock required, ipv4CidrBlock derived from it), which a mixin must not change. CfnSubnetPropsMixin remains the escape hatch, e.g. for IPv6 IPAM allocation, which is left for a follow-up.
  5. Consumers of ipv4CidrBlock that parse the CIDR (SubnetFilter.byCidrMask(), byCidrRanges(), containsIpAddresses()) cannot work with a deploy-time value; this is documented on the prop and in the README rather than changed. Subnets created by Vpc from subnetConfiguration are untouched.
  6. Making cidrBlock optional on an input struct is source-compatible for callers; PublicSubnetProps/PrivateSubnetProps inherit the new prop.

Alternatives considered and rejected: flat props (co-dependent rule); a SubnetIpAddresses.cidr()/.ipam() factory prop mirroring Vpc.ipAddresses (cidrBlock already exists and cannot be removed); optional netmaskLength using the pool's default netmask as in #34349 (deferred).

Describe any new or updated permissions being added

None. No IAM policies are created; the allocation uses the deploying principal's existing EC2/IPAM permissions.

Description of how you validated changes

  • Unit tests in packages/aws-cdk-lib/aws-ec2/test/vpc.test.ts (describe('Subnet')): the concrete-CIDR path renders exactly as before (CidrBlock, no IPAM properties); the IPAM path renders Ipv4IpamPoolId/Ipv4NetmaskLength and no CidrBlock, both with CfnIPAMPool.fromIpamPoolId() and with a pool defined in the same stack; ipv4CidrBlock resolves to Fn::GetAtt [<subnet>, CidrBlock]; PublicSubnet/PrivateSubnet accept the prop (including addNatGateway()); a token netmaskLength is passed through; the three validation errors.
  • New integration test packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.subnet-ipam.ts: an Advanced Tier IPAM, a VPC with 10.0.0.0/16, a custom resource that waits until IPAM has discovered the VPC, a resource planning pool for the VPC, and a Subnet with ipv4IpamAllocation. An awsApiCall('EC2', 'describeSubnets') assertion checks that the allocated CIDR is 10.0.0.0/24. A maintainer deployed the test and committed the snapshot.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions Bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 labels Sep 11, 2026
@aws-cdk-automation
aws-cdk-automation requested a review from a team September 11, 2026 09:04
@github-actions github-actions Bot added the beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK label Sep 11, 2026

@aws-cdk-automation aws-cdk-automation left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@gauranshahuja

Copy link
Copy Markdown
Contributor Author

Exemption Request: I could not deploy the integration test (no AWS account available), so the snapshot for integ.subnet-ipam.ts is not included. The test is written per INTEGRATION_TESTS.md; could a maintainer run it to generate the snapshot? Happy to adjust the test if the pool setup needs changing.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Sep 11, 2026
# Conflicts:
#	CHANGELOG.v2.alpha.md
#	CHANGELOG.v2.md
#	packages/@aws-cdk/metrics-facade-alpha/package.json
#	packages/@aws-cdk/mixins-preview/package.json
#	packages/aws-cdk-lib/aws-ec2/test/vpc.test.ts
#	packages/aws-cdk-lib/package.json
#	tools/@aws-cdk/spec2cdk/package.json
#	version.v2.json
#	yarn.lock
@gauranshahuja

Copy link
Copy Markdown
Contributor Author

@ozelalisen Done, thanks. I updated the second bullet of "Description of how you validated changes" to your wording and dropped the fork-run bullet, since CI on this PR covers the same checks.

@ozelalisen ozelalisen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the description. Two inline comments are still open: the struct name on vpc.ts (SubnetIpv4IpamAllocation), which can't change after release, and the README note on IPAM discovery timing. Could you take a look at those?

Leaves room for an IPv6 struct in a follow-up. Also clarifies in the README that the planning pool belongs in a later deployment, once IPAM has discovered the VPC.
@gauranshahuja

Copy link
Copy Markdown
Contributor Author

@ozelalisen Thanks, both done:

  • Renamed the struct to SubnetIpv4IpamAllocation, so an IPv6 follow-up can add its own struct next to it. Nothing else referenced the old name; the PR description is updated to match.
  • The README now says to create the planning pool in a later deployment, once IPAM has discovered the VPC, and notes that a stack deployed right after the VPC's stack (for example by the same cdk deploy --all) can still start before discovery finishes.

@mergify
mergify Bot dismissed ozelalisen’s stale review November 13, 2026 06:32

Pull request has been modified.

@ozelalisen ozelalisen removed the pr/needs-integration-tests-deployment Requires the PR to deploy the integration test snapshots. label Nov 13, 2026

@ozelalisen ozelalisen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the rename and the README wording both look good.

@ozelalisen

Copy link
Copy Markdown
Member

@Mergifyio update

@mergify

mergify Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

update

❌ GitHub refused to update workflow files

Details

GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/README.md without workflows permission
GitHub only lets Mergify write to .github/workflows/ with the workflows permission. An organization owner can accept Mergify's pending permissions at https://dashboard.mergify.com/orgs/aws/repositories.

ozelalisen
ozelalisen previously approved these changes Nov 13, 2026
@mergify
mergify Bot dismissed ozelalisen’s stale review November 13, 2026 14:24

Pull request has been modified.

@mergify

mergify Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify

mergify Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

❌ Left the queue · checks timed out

2 h 22 min in the queue, 2 h 21 min of it CI.

So far

  • ✅ Entered the queue Sat 14 Nov, 12:44 · rule default-squash · triggered by rule automatic merge
  • ❌ Checks timed out · in place
  • 🚫 Left the queue 15:06 · d1281db

Reason

The merge conditions cannot be satisfied: the checks did not pass within the automatic checks timeout of 2 hours 20 minutes 40 seconds, derived from this queue's recent CI runtime

These checks were still running:

  • build

To get back in the queue

  • Push a fix: the pull request is queued again once its queue conditions match.
  • This timeout was not configured: set checks_timeout on the queue to use your own value, or null to disable it.
  • If the timeout is a flake, tick the box below to queue it again as it is.

Merge conditions not met when it left the queue

  • any of · required by GitHub branch protection:
    • check-neutral = build
    • check-skipped = build
    • check-success = build

Requeue requested — the merge queue status continues in this comment ↓.

2 merge conditions met
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = validate-pr
    • check-neutral = validate-pr
    • check-skipped = validate-pr

Times in UTC · dashboard · how the merge queue works

@gauranshahuja

Copy link
Copy Markdown
Contributor Author

@ozelalisen Thanks for the approval. The merge queue dropped this PR because build timed out: the workflow runs on Mergify's update commit d1281db are waiting for maintainer approval ("action_required"), so build never started. Could you approve the workflow runs and requeue it?

@ozelalisen

Copy link
Copy Markdown
Member

@Mergifyio queue

@mergify

mergify Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

✅ Merged into main as 16c42cb at 08:53 UTC

Less than a minute in the queue, with no time running CI.

So far

  • ✅ Entered the queue Mon 16 Nov, 08:53 · rule default-squash · triggered by @ozelalisen with the @mergifyio queue command
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged 08:53 · 16c42cb · squash
3 merge conditions met
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = validate-pr
    • check-neutral = validate-pr
    • check-skipped = validate-pr
  • any of · required by GitHub branch protection:
    • check-success = build
    • check-neutral = build
    • check-skipped = build

Times in UTC · dashboard · how the merge queue works

@mergify

mergify Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify
mergify Bot merged commit 16c42cb into aws:main Nov 16, 2026
26 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 16, 2026

This branch was successfully deployed

1 inactive deployment
automation — d1281dba Deployed Nov 14, 2026 by mergify[bot] via validate-pr #378970
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 pr/request-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants