Why won't the CDK let me divide my network?

0

Problem

I am trying to use CDK for the first time and trying to divide a 10.0.0.0/24 VPC into 8 /27 subnets with 4 public and 4 private subnets spanning no more than 4 Availability Zones. When I run cdk deploy I am receiving the following error.

Error: 1 of /27 exceeds remaining space of 10.0.0.0/24                                                                                                                             

Multiple websites have displayed that I can split the network this way.

I know that AWS reserves 5 IP addresses from each subnet, but that should still leave 25 hosts per subnet, which is plenty for my exercise.


Code

new ec2.Vpc(this, 'SimpleVpc', {
  cidr: '10.0.0.0/24',
  maxAzs: 4,
  natGateways: 1,
  subnetConfiguration: 
    SimpleVpcStack.createSubnets(SubnetType.PUBLIC).concat(
    SimpleVpcStack.createSubnets(SubnetType.PRIVATE_WITH_NAT))
});

private static createSubnets(type: SubnetType): ec2.SubnetConfiguration[] {
  const label = SubnetType.PUBLIC === type ? 'pub' : 'pvt';
  const subnets: ec2.SubnetConfiguration[] = [];
  for(let i = 1; i < 5; i++){
    subnets.push({
      cidrMask: 27,
      name: `${label}-${i}`,
      subnetType: type
    });
  }
  return subnets;
}

Logs

subnets [                                                
  { cidrMask: 27, name: 'pub-1', subnetType: 'Public' }, 
  { cidrMask: 27, name: 'pub-2', subnetType: 'Public' }, 
  { cidrMask: 27, name: 'pub-3', subnetType: 'Public' }, 
  { cidrMask: 27, name: 'pub-4', subnetType: 'Public' }, 
  { cidrMask: 27, name: 'pvt-1', subnetType: 'Private' },
  { cidrMask: 27, name: 'pvt-2', subnetType: 'Private' },
  { cidrMask: 27, name: 'pvt-3', subnetType: 'Private' },
  { cidrMask: 27, name: 'pvt-4', subnetType: 'Private' } 
]         
asked 2 years ago1632 views
1 Answer
1
Accepted Answer

From https://github.com/aws/aws-cdk/issues/11082 it looks like spreading across the AZs (when maxAzs>1) is done automatically, so your code may actually be trying to create 8 subnets per AZ. I don't think the documentation is very clear on this point.

answered 2 years ago
  • Thanks! You're right, the subnetConfiguation is replicated for each AZ. I found a reference in the docs. I wish I had more control over this behavior.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions