DEV Community

Cover image for Understanding Subnetting in Simple Terms
BAKRE JAMIU
BAKRE JAMIU

Posted on

Understanding Subnetting in Simple Terms

Subnetting might sound complicated, but it’s like slicing a pizza. Imagine you have one big pizza (your VPC network), and you need to divide it into smaller slices (subnets). Each slice is meant for specific groups of people (applications, teams, or services). However, there are some rules to follow to make sure no slice overlaps with another, and everyone gets the right amount.


What is a VPC?

A VPC (Virtual Private Cloud) is like a private network on the cloud where you can create and manage resources like servers and databases. When you set up a VPC, you define how big it is using a CIDR block (this decides how many IPs you can use).

For example, the VPC CIDR block 172.16.0.0/24 gives you 256 total IPs to work with.


Why Divide a VPC into Subnets?

Imagine you’re running an office with three departments:

  1. HR (needs 80 desks),
  2. IT (needs 50 desks),
  3. Finance (needs 20 desks).

Your office has 256 desks, and you need to allocate them so:

  1. Each department gets enough space.
  2. Nobody accidentally sits in someone else’s area.

This is where subnetting comes in. It divides your network into smaller, isolated sections, just like dividing the office into separate rooms for each department.


Subnetting Rules Made Simple

  1. Subnets must not overlap: Each subnet needs its own space; otherwise, the system won’t know where a desk belongs.
  2. Use powers of 2: Subnet sizes must follow specific rules, like 128, 64, or 32 IPs. This ensures neat divisions.
  3. Reserve 5 IPs: AWS automatically takes 5 IPs from every subnet for internal use (e.g., for the gateway).

Real-World Example

Let’s say your VPC CIDR block is 172.16.0.0/24 (256 IPs). You need three subnets:

  1. Subnet 1: 80 IPs for HR,
  2. Subnet 2: 50 IPs for IT,
  3. Subnet 3: 20 IPs for Finance.

Here’s how you divide it step by step:

  1. Subnet 1 (80 IPs):

    • Use the closest power of 2: 128 IPs (AWS reserves 5, so 123 are usable).
    • CIDR: 172.16.0.0/25 (Range: 172.16.0.0 - 172.16.0.127).
  2. Subnet 2 (50 IPs):

    • Use the closest power of 2: 64 IPs (AWS reserves 5, so 59 are usable).
    • CIDR: 172.16.0.128/26 (Range: 172.16.0.128 - 172.16.0.191).
  3. Subnet 3 (20 IPs):

    • Use the closest power of 2: 32 IPs (AWS reserves 5, so 27 are usable).
    • CIDR: 172.16.0.192/27 (Range: 172.16.0.192 - 172.16.0.223).

How CIDR Works

CIDR stands for Classless Inter-Domain Routing. The /25, /26, and /27 at the end of the addresses indicate how many IPs are in each subnet:

  • /25 = 128 IPs,
  • /26 = 64 IPs,
  • /27 = 32 IPs.

These blocks ensure every subnet has just the right amount of space.


Why Errors Happen in Subnetting

If you try to create overlapping subnets, AWS will throw an error. For example:

  • Using 172.16.0.6/25 instead of 172.16.0.0/25 causes problems because subnets don’t start at the right boundaries.

How to Avoid Overlap

Follow these tips:

  1. Start subnets at exact boundaries: /25 must start at .0, /26 must start at .128, and /27 must start at .192.
  2. Plan your network carefully: Use tools like subnet calculators to check your ranges.
  3. Use the right subnet sizes: Don’t allocate more IPs than necessary.

Final Thoughts

Subnetting is like organizing a party. You need to know how many guests (IPs) are coming and ensure there’s enough space for everyone. By understanding the rules and planning ahead, you can divide your network (pizza) into neat, non-overlapping slices (subnets) that fit perfectly within your VPC.

Got stuck? Drop your questions in the comments below, and I’ll help you troubleshoot!

Top comments (0)