Laptop to Production

Subnets and IP ranges

Slicing the network, and planning addresses for growth.

Time
35 minutes
Level
Beginner
Read first
What is a VPC?

Part 1: The problem

QuickCart, month four

When Priya built QuickCart’s first network, she gave the VPC 10.0.0.0/24 — 256 addresses. It seemed like plenty for three servers.

Then QuickCart moved to containers. Every running container needs its own address, and the app subnets filled up in an afternoon. New containers failed to start with an error about no free addresses.

A subnet can’t be resized, and a VPC’s first range can’t be changed. The fix was a new, bigger VPC, and a weekend moving everything across.

Both problems come from choosing address ranges without a plan. This lesson is that plan: how to read a range, how big to make each one, and how to keep them apart.

Part 2: The idea

The VPC lesson showed that every range is written like 10.0.0.0/16, and that a smaller number after the slash means a bigger range. Here’s why.

An address is 32 bits

An IPv4 address like 10.0.11.24 is four numbers, each from 0 to 255. Each number is one octet: 8 bits. Four octets make 32 bits.

Written1001124
In bits00001010000000000000101100011000

A computer only sees the 32 bits. The dots are for people.

The slash says how many bits are fixed

In 10.0.0.0/16, the /16 means the first 16 bits are fixed. Every address in the range starts with the same 16 bits: 10.0. The other 16 bits are free: they can be anything.

Each free bit doubles the number of addresses. So the size of a range is 2 to the power of the free bits:

RangeFixed bitsFree bitsAddressesCovers
10.0.0.0/1616162¹⁶ = 65,53610.0.0.0 to 10.0.255.255
10.0.0.0/2020122¹² = 4,09610.0.0.0 to 10.0.15.255
10.0.1.0/242482⁸ = 25610.0.1.0 to 10.0.1.255
10.0.1.0/262662⁶ = 6410.0.1.0 to 10.0.1.63
10.0.1.0/282842⁴ = 1610.0.1.0 to 10.0.1.15

That’s the whole trick: free bits = 32 − the number after the slash; addresses = 2 to that power. One step down the slash doubles the size. One step up halves it.

Key point: The number after the slash is how many bits are fixed. The rest are free, and 2 to the power of the free bits is the number of addresses.

Every block starts on its own boundary

A range is written as its first address. And a block of a given size can only start at a multiple of that size.

A /20 holds 4,096 addresses — 16 blocks of 256. So a /20 always starts where the third number is a multiple of 16:

Valid /20 blocks in 10.0.0.0/16Not a valid /20
10.0.0.0/20, 10.0.16.0/20, 10.0.32.0/20, 10.0.48.0/2010.0.1.0/20, 10.0.8.0/20, 10.0.20.0/20

The same rule at every size: a /24 starts at x.x.x.0, a /26 at .0, .64, .128 or .192, a /28 at a multiple of 16 in the last number. If a range doesn’t start on its boundary, the free bits wouldn’t all be free — some would already be set. AWS rejects it.

AWS keeps five addresses in every subnet

In every subnet, AWS reserves the first four addresses and the last one. For 10.0.11.0/24:

AddressReserved for
10.0.11.0The network address — the name of the range itself
10.0.11.1The VPC router, where traffic leaving the subnet goes first
10.0.11.2The Amazon DNS server
10.0.11.3Kept by AWS for future use
10.0.11.255The last address. Networks use it for broadcast; AWS doesn’t support broadcast, so it simply keeps it

So a /24 gives you 251 usable addresses, and the smallest subnet AWS allows, a /28, gives only 11. The five matter most in small subnets.

Which ranges to use at all

Three ranges are set aside for private networks. Nothing on the internet uses them, so they’re safe inside a VPC:

Private rangeSizeNotes
10.0.0.0/816.7 million addressesThe usual choice. Room for 256 VPCs of /16 each
172.16.0.0/12About 1 millionDefault VPCs use 172.31.0.0/16 from here
192.168.0.0/1665,536Home routers use it. Avoid it, or your VPN users will clash with their own Wi-Fi

A VPC can be anything from a /16 (65,536 addresses) down to a /28. Subnets have the same limits.

Planning for growth

A plan is cheap on paper and very expensive to change later. Three rules:

RuleWhy
Give each VPC a /16Addresses inside a VPC are free. Running out is not
Make subnets bigger than today’s need, and leave most of the VPC unusedContainers and Kubernetes can use one address per running copy. You can add subnets later, but never resize one
Give every VPC its own range, even in another region or accountOverlapping VPCs can never be connected. 10.0.0.0/16, 10.1.0.0/16, 10.2.0.0/16…

QuickCart’s plan for its first VPC, 10.0.0.0/16. The third number tells you the tier and the zone at a glance:

TierZone aZone bRoom to grow
Public10.0.1.0/2410.0.2.0/2410.0.3.0/24 for a third zone
App10.0.11.0/2410.0.12.0/2410.0.13.0/24
Data10.0.21.0/2410.0.22.0/2410.0.23.0/24
Containers, later10.0.32.0/2010.0.48.0/204,091 usable addresses each

Six subnets use 1,536 of 65,536 addresses. More than 95% of the VPC is left for things nobody has thought of yet. The second region gets 10.1.0.0/16 with the same layout, so the two can be joined whenever they need to be.

Part 3: See it

Below is QuickCart’s VPC, 10.0.0.0/16, drawn as a map: one square for every /24. Pick a size and a starting address, and add a subnet. Watch the bits, the address count, and the map. Then try to break it: start a /20 at 10.0.1.0, and add a /24 that lands on top of one that’s already there.

Subnet size

The VPC is 10.0.0.0/16. Type where a subnet starts, pick a size, and add it. Try 10.0.1.0 at /20 to see what goes wrong.

Subnets in the plan

    ReadyQuickCart's plan starts with three /24 subnets. Choose Show on one to see its bits, or add your own.

    What to notice: each step down in the slash doubles the addresses and halves the number of blocks that fit. A block only fits where its fixed bits line up, and nothing may overlap — the two rules AWS enforces every time you create a subnet.

    Part 4: Do it

    Free — VPCs and subnets cost nothing.

    You’ll plan QuickCart’s addresses on paper, build part of the plan, and deliberately hit the two errors from this lesson so you recognise them. About 20 minutes.

    Check the region selector at the top right of the console says N. Virginia (us-east-1).

    1. Plan on paper first

      Before opening the console, write down the plan from the idea: one VPC, and a public, app and data subnet in each of two zones. For each subnet, work out:

      SubnetRangeAddressesUsableFirst usableLast usable
      quickcart-public-a10.0.1.0/2425625110.0.1.410.0.1.254
      quickcart-app-a10.0.11.0/2425625110.0.11.410.0.11.254
      quickcart-data-a10.0.21.0/2425625110.0.21.410.0.21.254

      Fill in the zone b rows yourself — 10.0.2.0/24, 10.0.12.0/24 and 10.0.22.0/24 — and check your answers with the planner above.

      You should see

      A table where no two ranges share an address, every range is inside 10.0.0.0/16, and most of the VPC is still unused.

    2. Create the VPC

      In the AWS console: VPCYour VPCsCreate VPC

      FieldEnter
      Resources to createVPC only
      Name tagquickcart-vpc
      IPv4 CIDR blockIPv4 CIDR manual input
      IPv4 CIDR10.0.0.0/16
      IPv6 CIDR blockNo IPv6 CIDR block
      TenancyDefault

      Choose Create VPC.

      You should see

      The VPC’s details page, with State: Available and IPv4 CIDR: 10.0.0.0/16.

    3. Create the zone a subnets

      In the AWS console: VPCSubnetsCreate subnet

      For VPC ID, choose quickcart-vpc. Fill in the first subnet, choose Add new subnet for each of the others, then choose Create subnet:

      Subnet nameAvailability ZoneIPv4 subnet CIDR block
      quickcart-public-aus-east-1a10.0.1.0/24
      quickcart-app-aus-east-1a10.0.11.0/24
      quickcart-data-aus-east-1a10.0.21.0/24
      You should see

      Three subnets in the list. The Available IPv4 addresses column says 251 for each: 256 minus the five AWS keeps.

    4. Create the zone b subnets

      Same page, same VPC:

      Subnet nameAvailability ZoneIPv4 subnet CIDR block
      quickcart-public-bus-east-1b10.0.2.0/24
      quickcart-app-bus-east-1b10.0.12.0/24
      quickcart-data-bus-east-1b10.0.22.0/24
      You should see

      Six subnets, three in each zone, each with 251 available addresses.

    5. Try a block that isn't on its boundary

      Create one more subnet in quickcart-vpc:

      FieldEnter
      Subnet namequickcart-containers-a
      Availability Zoneus-east-1a
      IPv4 subnet CIDR block10.0.33.0/20

      Choose Create subnet.

      You should see

      quickcart-containers-a in the list with 4091 available addresses: 4,096 minus five.

    6. Try a range that overlaps

      Create one more subnet:

      FieldEnter
      Subnet namequickcart-extra-a
      Availability Zoneus-east-1a
      IPv4 subnet CIDR block10.0.40.0/24

      Choose Create subnet.

    7. Clean-up step: Clean up

      In the AWS console: VPCYour VPCsquickcart-vpcActionsDelete VPC

      AWS lists the subnets that go with it. Type delete in the box and choose Delete.

      You should see

      The VPC and all seven subnets are gone. Nothing was charged; keep your paper plan for the routing lesson.

    Part 5: Check yourself

    How many addresses are in 10.0.8.0/22?

    A subnet is 10.0.11.0/24. How many addresses can you actually give to servers?

    Why does AWS reject 10.0.1.0/20?

    QuickCart is adding a VPC in a second region. Which range should it use, if the first VPC is 10.0.0.0/16?

    You made the app subnets /24 and the service now needs far more addresses. What can you do?

    Part 6: Cheat sheet

    On one card
    TermIn one line
    IPv4 address32 bits, written as four numbers from 0 to 255
    /nThe first n bits are fixed. 32 − n bits are free
    Addresses in a range2 to the power of the free bits
    /16 · /20 · /24 · /2865,536 · 4,096 · 256 · 16
    Boundary ruleA block starts on a multiple of its own size
    Reserved in every subnetFirst four and the last: 5 addresses. A /24 has 251 usable
    Subnet size limitsFrom /16 to /28
    Private ranges10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
    Growth/16 per VPC, leave most of it unused. You can’t resize a subnet
    Several VPCsNever overlap: 10.0.0.0/16, 10.1.0.0/16, 10.2.0.0/16

    Finished the build and the quiz? Mark it done to fill in its stop on the route map.