[ec2] VPC creation fails when VLSM used with max azs set to value more than 1
Hi,
While using the VPC construct to create vpc and subnets, the cdk synth commands produces errors saying "Error: 1 of /25 exceeds remaining space of 10XXXX " this only occurs when max_azs attribute is set to more than 1.
Reproduction Steps
` subnet_config = [ ec2.SubnetConfiguration(name='private-sub-1', subnet_type=ec2.SubnetType.PRIVATE, cidr_mask=25), ec2.SubnetConfiguration(name='private-sub-2, subnet_type=ec2.SubnetType.PRIVATE, cidr_mask=25), ec2.SubnetConfiguration(name='private-sub-3', subnet_type=ec2.SubnetType.PRIVATE, cidr_mask=26), ec2.SubnetConfiguration(name='public-sub-1', subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=26), ec2.SubnetConfiguration(name='public-sub-2', subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=27), ec2.SubnetConfiguration(name='public-sub-3, subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=27), ] vpc = ec2.Vpc(self,"vpc", cidr='10.243.202.0/23', enable_dns_hostnames=True, enable_dns_support=True, subnet_configuration= subnet_config, max_azs=3, vpn_gateway=True, )
`
What did you expect to happen?
I expected to create a cloud formation with a vpc with 3 private subnets and 3 public subnets spanning across 3 availability zones.
What actually happened?
Got the following error,
jsii.errors.JavaScriptError:
Error: 1 of /26 exceeds remaining space of 10.243.202.0/23
at NetworkBuilder.addSubnets (/tmp/jsii-kernel-01ziJE/node_modules/@aws-cdk/aws-ec2/lib/network-util.js:121:19)
at NetworkBuilder.addSubnet (/tmp/jsii-kernel-01ziJE/node_modules/@aws-cdk/aws-ec2/lib/network-util.js:110:21)
at availabilityZones.forEach (/tmp/jsii-kernel-01ziJE/node_modules/@aws-cdk/aws-ec2/lib/vpc.js:512:48)
at Array.forEach (<anonymous>)
at Vpc.createSubnetResources (/tmp/jsii-kernel-01ziJE/node_modules/@aws-cdk/aws-ec2/lib/vpc.js:502:32)
at Vpc.createSubnets (/tmp/jsii-kernel-01ziJE/node_modules/@aws-cdk/aws-ec2/lib/vpc.js:493:18)
at new Vpc (/tmp/jsii-kernel-01ziJE/node_modules/@aws-cdk/aws-ec2/lib/vpc.js:330:14)
at obj._wrapSandboxCode (/root/.local/share/virtualenvs/vpcdeploy-U7ggNUho/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7934:49)
at Kernel._wrapSandboxCode (/root/.local/share/virtualenvs/vpcdeploy-U7ggNUho/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8422:19)
at Kernel._create (/root/.local/share/virtualenvs/vpcdeploy-U7ggNUho/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7934:26)
at Kernel.create (/root/.local/share/virtualenvs/vpcdeploy-U7ggNUho/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7678:21)
at KernelHost.processRequest (/root/.local/share/virtualenvs/vpcdeploy-U7ggNUho/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7458:28)
at KernelHost.run (/root/.local/share/virtualenvs/vpcdeploy-U7ggNUho/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7396:14)
at Immediate.setImmediate [as _onImmediate] (/root/.local/share/virtualenvs/vpcdeploy-U7ggNUho/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7399:37)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
Environment
- **CLI Version :**1.70.0
- Framework Version: 1.70.0
- Node.js Version: 10.21.0
- **OS :**ubuntu 18
- Language (Version): python 3.6
This is :bug: Bug Report
SubnetConfiguration will be repeated per AZ.
ec2.SubnetConfiguration(name='private-sub-1', subnet_type=ec2.SubnetType.PRIVATE, cidr_mask=25),
ec2.SubnetConfiguration(name='private-sub-2, subnet_type=ec2.SubnetType.PRIVATE, cidr_mask=25),
ec2.SubnetConfiguration(name='private-sub-3', subnet_type=ec2.SubnetType.PRIVATE, cidr_mask=26),
ec2.SubnetConfiguration(name='public-sub-1', subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=26),
ec2.SubnetConfiguration(name='public-sub-2', subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=27),
ec2.SubnetConfiguration(name='public-sub-3, subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=27),
This will create 6 subnets per AZ. Is that what you intended, or did you intend to create 2 per AZ?
I require 2 Subnets per AZ one is private and one is public
That is what the Vpc construct will create automatically if you don't configure anything.
You're probably looking to do something like this:
ec2.SubnetConfiguration(name='Private', subnet_type=ec2.SubnetType.PRIVATE, cidr_mask=26),
ec2.SubnetConfiguration(name='Public', subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=26),
Spreading across AZs is automatic.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.