Skip to content

Network-Utils: IpAddresses.cidr() silently adjusts misaligned base addresses to the next valid CIDR block #34784

@DaltJM

Description

@DaltJM

Describe the bug

When using aws_ec2.IpAddresses.cidr() in the AWS CDK to define a VPC or subnet CIDR block, if the provided base IP address is not properly aligned for the specified prefix length, the CDK silently "rounds up" the address to the next valid CIDR block without issuing a warning or error. This can result in unexpected address space being allocated, which may go unnoticed during deployment and lead to future routing or peering issues.

Last Known Working CDK Library Version

No response

Expected Behavior

The CDK should validate the base address against the specified prefix length and throw an error if the base address is not aligned correctly for that prefix. At minimum, it should issue a warning or explicitly document that rounding will occur.

Current Behavior

CDK accepts misaligned CIDR specifications without validation. It internally adjusts the base address to the next aligned block for the given prefix length and proceeds with deployment. This adjustment is not surfaced to the user, leading to potential confusion or misconfiguration of network boundaries.

Reproduction Steps

  1. Create a new CDK stack using TypeScript.

  2. Define a VPC or subnet using an invalid CIDR block such as 10.0.40.0/19:

ec2.Vpc.fromVpcAttributes(this, 'MyVpc', {
  vpcId: 'vpc-abc123',
  ipAddresses: ec2.IpAddresses.cidr('10.0.40.0/19')
});
  1. Synthesize or deploy the stack.

  2. Observe that the resulting CIDR block is 10.0.64.0/19 rather than the originally specified 10.0.40.0/19.

Possible Solution

Implement validation in IpAddresses.cidr() to check whether the base IP address is aligned for the given prefix length.

If misaligned, throw an error to prevent silent correction.

Alternatively, provide a clear warning or log message during synthesis to inform the user that the input CIDR block has been modified.

Document this behavior explicitly in the method's API documentation to prevent confusion for users relying on deterministic network design.

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

2.1019.1

AWS CDK CLI version

2.27.40

Node.js Version

22.16.0lts

OS

Windows 10

Language

TypeScript

Language Version

5.8.3

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ec2Related to Amazon Elastic Compute CloudbugThis issue is a bug.effort/mediumMedium work item – several days of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions