If you’re interested in firing up some AWS EC2 instances, and some point you’re going to need to know about VPCs (Virtual Private Clouds) and CIDR blocks. If you’re studying for the AWS Solution Architect certification this is also an important topic covered in the exam. There’s a whole section in the AWS docs here that covers the topic of CIDR blocks.
Unless you’re an experienced network engineer or someone who works with configuring network topologies, this might be the first time you’ve come across this concept (I had seen the CIDR notation before but didn’t know what it meant). At first it might be easy to memorize a few of the common examples and remember which represents a smaller network and which is larger, but to understand why a /24 network has less available addresses than a /8 network you’ll need to dig a little deeper.
First, let’s look at typical IPv4 IP addresses, networks and subnets:
IPv4 IP addresses have 4 digits separated by ‘.’s. Each digit is 8 bits, and are referred to as ‘octets’.
A typical address on a home network like 192.168.1.16 has a subnet mask of 255.255.255.0. This means the first 3 octets are used to represent the network, in this case 192.168.1.0. Computers with this same IP prefix are therefore part of the same network, so
- up to
… are all on the same network and assuming your router and each individual computer is setup ok, then each of the computers with these addresses on this same network can also see each other (without any additional routing between networks).
In this range there are also some reserved IPs for special purposes:
- 192.168.1.0 is referred to as the network itself
- 192.168.1.255 is a broadcast address for this network
- this leaves 192.168.1.1 through 192.168.1.254 as usable addresses in the network
Originally networks were divided and categorized by 8 bit boundaries and were referred to as ‘class’ based networks:
Class C network:
- subnet mask 255.255.255.0
- first 3 octets are the network
- the smallest network Class, with 256 available addresses
- only the 4th octet is available for your host addresses, 0-255, 256 available addresses, (or 1-254 ignoring 0 and 255)
- 24 bits used for network, 8 for hosts
Class B network:
- subnet mask 255.255.0.0
- first 2 octets are the network
- 3rd and 4th octets available for host addresses
- 16 bits used for network, 16 bits for hosts
Class A network:
- subnet mask 255.0.0.0
- first octet is the network
- the largest IPv4 network Class
- 2nd, 3rd and 4th octets available for hosts
- 8 bits used for network, 24 bits for hosts
Ok, so this summarizes Class based networks which are divided by 8 bit boundaries, of which we only have 3 options, A (largest), B and C (smallest). Now let’s look at Classless networks.
Instead of restricting to 8 bit boundaries, Classless networks can use any of the bits to represent the network and the remaining bits for the hosts. Now let’s first look at the Classless InterDomain Routing (CIDR) notation for the same Class based networks as the first examples:
- /24 CIDR block is the same as Class C, with subnet mask 255.255.255.0
- /16 CIDR block is the same as Class B, with 255.255.0.0
- /8 CIDR block is the same as Class A, with subnet mask 255.0.0.0
You might have noticed that the number in the /xx CIDR block notation is referring to is the number of bits used for the network, and therefore implies the number of bits remaining for host addresses (from the 4 total octets, or 32 bits). This approach is not restricted to the 8 bit boundaries though, any number of bits can be used for combination of network and hosts, so /24, /23, /22 and any value from /32 to /1 are all valid (although /32 with all bits for the network is of little practical use, similarly for /31 as there’s only 1 bit remaining for a host address, but these are used for special purposes, e.g. for a single host route, or point to point network links).
Ok, so now let’s apply this to look at AWS VPCs and CIDR blocks.
For a /24 block, we already looked at x.x.x.0 address to refer to the network and x.x.x.255 for the broadcast address. AWS VPC subnets reserve a further 3 IP addresses (described here) for AWS usage, x.x.x.1, x.x.x.2, and x.x.x.3, so for each subnet there are 5 IP addressed unavailable for your own hosts.
Now we’ve looked at how the address blocks are comprised, it’s easy to calculate how many addresses are available in a VPC for any CIDR block. Taking /24 as an example:
- 32 – 24 = 8 bits for the host addresses
- 2^8 = 256
- 256 – 5 = 251
Knowing how IP addresses are structured, how CIDR blocks define the range of possible IPs for your hosts, the purpose of the .0 and .255 and with the additional 3 AWS reserved IPs, you can now calculate how many IP addresses are available to your VPS for any CDR block.