DEV Community

Cover image for Dividing IPs for Multiple Subnets in a VPC
Keme Kenneth
Keme Kenneth

Posted on

Dividing IPs for Multiple Subnets in a VPC

When creating resources for your application in a cloud infrastructure like AWS you would categorize resources into public or private subnets, often spread across multiple Availability Zones (AZs) for high availability, all in a single virtual private cloud (VPC).

VPC defines the total IP range using CDIR (Classless Inter-Domain Routing) notation.
Eg. 10.0.0.0/22 - 1024 IPs

The number after the slash "/" - 22 (prefix length) is used to calculate the total IPs following a formula - 2^(32 - PREFIX_LEN). It could go from /0 to /32.

In the above VPC the CIDR is 10.0.0.0/22 - 1024 IPs
This means the total number of subnets will share 1024 IPs

But how do you now further divide these IPs for each subnet?

Octets referencing:
[10].[0].[0].[0] => [1st].[2nd].[3rd].[4th]

Subnet-1 => 10.0.0.0/24 - 256 IPs
Range 10.0.0.0, 10.0.0.1, 10.0.0.2, ... 10.0.0.255
Meaning the 4th or last octet is full it has exactly 256 IPs.

But anything below /24 would have eaten into the 3rd octet, say /23 - 512 IPs, because the last octet certainly don't have up to 1024 IPs

Subnet-2
Knowing that the last octet is full, 2nd subnet has the 3rd octet to play with.
Common practice when creating additional subnets is to increase the octet by 1 - 10.0.1.0/24 - 256 IPs
Range 10.0.1.0, 10.0.1.1 ... 10.0.1.255

I could choose any PREFIX_LEN (0-32) but I also have to bear in mind that the VPC's total range is 1024
If I did 10.0.1.0/22, this won't work because 1024 + Subnet-1's 256 is more than the VPC's total.

Subnet-3 => 10.0.2.0/24 - 256 IPs
Range 10.0.2.0, 10.0.2.1, 10.0.2.2 ... 10.0.2.255

I hope this brief explanation helps you understand how to allocate IP ranges to a VPC's subnets better.

Thanks for reading ❤️

Top comments (0)