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' }
]
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.