Virtual Networks: CIDR Blocks, Subnets and Security Groups
Design a private cloud network: split an address block into subnets across zones, understand public and private subnets, and write security-group rules that allow only what is needed. Check address maths with two implementations and test rules against a table of packets.
In this lesson you will
- Read CIDR notation
- Split a block into non-overlapping subnets
- Write least-open security group rules
- Verify address maths twice
About 28 minutes
Which door is open to the world?
Wren’s database was reachable from the whole internet for two weeks, simply because it was placed in the default network with a rule that allowed everything. A virtual private network, VPC or virtual network depending on the provider, is your own isolated slice of the provider’s network, and designing it is the main security measure after access policies. The first tool is CIDR notation, which names a block of addresses by a prefix: 10.20.0.0/16 means that the first 16 bits are fixed, and the remaining 16 bits give 65,536 addresses. A /24 fixes 24 bits and gives 256 addresses, and each extra bit of prefix halves the block. The private ranges 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are reserved for private use and are not routed on the public internet, so every company can use them, and the blocks of networks that must talk to each other must not overlap.
Inside the network you create subnets, one per availability zone and role. A public subnet has a route to the internet, and holds only what must be reached from outside, such as the load balancer. A private subnet has no inbound route from the internet and holds the application servers and the database, which reach out, for updates, through a controlled gateway. A security group is a firewall attached to a resource: a list of allow rules by protocol, port and source, with everything else denied by default, the same default-deny logic as in lesson 4. The good pattern is layered: the load balancer accepts web traffic from anywhere, the application servers accept traffic only from the load balancer, and the database accepts connections only from the application servers, on its own port. Providers reserve a few addresses in each subnet for their own use, often five, so the usable count is smaller than the arithmetic count; the program below uses that figure as an example and you should check the number in your provider’s documentation.
| Prefix | Addresses | Typical use |
|---|---|---|
| /16 | 65,536 | Whole network |
| /20 | 4,096 | Large tier |
| /24 | 256 | One subnet |
| /28 | 16 | Small subnet |
import ipaddress, json
vpc = ipaddress.ip_network("10.20.0.0/16")
subnets = {}
for i, zone in enumerate(["zone-a", "zone-b", "zone-c"]):
subnets["public-" + zone] = ipaddress.ip_network("10.20.%d.0/24" % i)
subnets["private-" + zone] = ipaddress.ip_network("10.20.%d.0/24" % (4 + i))
subnets["data-" + zone] = ipaddress.ip_network("10.20.%d.0/24" % (8 + i))
RESERVED = 5 # example figure: addresses a provider keeps in each subnet
for n, s in subnets.items(): print(n.ljust(16), str(s).ljust(16), "addresses", s.num_addresses, "usable", s.num_addresses - RESERVED)
nets = list(subnets.values())
overlaps = [(a, b) for i, a in enumerate(nets) for b in nets[i + 1:] if a.overlaps(b)]
print("subnets", len(nets), "| all inside the VPC:", all(s.subnet_of(vpc) for s in nets), "| overlaps:", overlaps, "| VPC addresses", vpc.num_addresses, "| used by subnets", sum(s.num_addresses for s in nets))
rules = {"lb": [("tcp", 443, "0.0.0.0/0"), ("tcp", 80, "0.0.0.0/0")], "app": [("tcp", 8080, "10.20.0.0/22")], "db": [("tcp", 5432, "10.20.4.0/22")]}
def allowed(group, proto, port, src):
ip = ipaddress.ip_address(src)
return any(proto == p and port == q and ip in ipaddress.ip_network(c) for p, q, c in rules[group])
tests = [("lb", "tcp", 443, "203.0.113.7"), ("lb", "tcp", 22, "203.0.113.7"), ("app", "tcp", 8080, "10.20.1.5"), ("app", "tcp", 8080, "10.20.4.9"), ("app", "tcp", 8080, "203.0.113.7"),
("db", "tcp", 5432, "10.20.5.30"), ("db", "tcp", 5432, "10.20.1.5"), ("db", "tcp", 5432, "203.0.113.7"), ("db", "udp", 5432, "10.20.5.30"), ("db", "tcp", 3306, "10.20.5.30")]
res = [[g, p, q, s, allowed(g, p, q, s)] for g, p, q, s in tests]
for g, p, q, s, a in res: print(g.ljust(4), p, str(q).ljust(5), s.ljust(14), "ALLOW" if a else "deny")
print("DATA " + json.dumps({"subnets": {k: str(v) for k, v in subnets.items()}, "res": res, "rules": rules, "overlaps": len(overlaps), "used": sum(s.num_addresses for s in nets)}))
public-zone-a 10.20.0.0/24 addresses 256 usable 251
private-zone-a 10.20.4.0/24 addresses 256 usable 251
data-zone-a 10.20.8.0/24 addresses 256 usable 251
public-zone-b 10.20.1.0/24 addresses 256 usable 251
private-zone-b 10.20.5.0/24 addresses 256 usable 251
data-zone-b 10.20.9.0/24 addresses 256 usable 251
public-zone-c 10.20.2.0/24 addresses 256 usable 251
private-zone-c 10.20.6.0/24 addresses 256 usable 251
data-zone-c 10.20.10.0/24 addresses 256 usable 251
subnets 9 | all inside the VPC: True | overlaps: [] | VPC addresses 65536 | used by subnets 2304
lb tcp 443 203.0.113.7 ALLOW
lb tcp 22 203.0.113.7 deny
app tcp 8080 10.20.1.5 ALLOW
app tcp 8080 10.20.4.9 deny
app tcp 8080 203.0.113.7 deny
db tcp 5432 10.20.5.30 ALLOW
db tcp 5432 10.20.1.5 deny
db tcp 5432 203.0.113.7 deny
db udp 5432 10.20.5.30 deny
db tcp 3306 10.20.5.30 deny
The program lays out a network of 10.20.0.0/16 with nine subnets of 256 addresses, three roles in three zones. The public subnets are 10.20.0.0/24 to 10.20.2.0/24, the private application subnets 10.20.4.0/24 to 10.20.6.0/24 and the data subnets 10.20.8.0/24 to 10.20.10.0/24, so that each tier starts on a /22 boundary and has room for a fourth zone without renumbering. Each subnet has 256 addresses and, using the example of five reserved per subnet, 251 usable ones. The checks print that all nine are inside the VPC and that none overlaps another: together they use 2,304 of the 65,536 addresses, about 3.5 percent, which leaves plenty of room to grow. Planning the block size early matters, because a network built too small, or with a range that clashes with a partner’s or an office network, is painful to change, and it blocks private connections between the two.
The second half tests the security groups with ten packets. The load balancer accepts web ports from anywhere, so a request to port 443 from a public address is allowed, and a connection to port 22, the remote login port, is denied. The application servers accept port 8080 only from 10.20.0.0/22, which is the range of the four /24 blocks starting at the public subnets, so the load balancer’s address 10.20.1.5 is allowed and an address from the private range, 10.20.4.9, is not, nor is an address from the internet. The database accepts port 5432 only from 10.20.4.0/22, the application subnets, so a request from 10.20.5.30 is allowed, and the same port from the public subnet, from the internet, over UDP, or to a different port, is denied. The result is a path where an attacker from the internet can reach only the load balancer’s web ports and not the database, whatever its password.
Independent check: the address arithmetic of the plan was verified with the standard ipaddress module, which found no overlaps and counted 2,304 addresses, which is 9 x 256. The ten test packets were evaluated by a separate JavaScript implementation that does not use the module: it turns each address into a number and compares the block index, the address divided by the block size, which is a way of applying the prefix mask. Its ten decisions are identical to the Python results and to the list that was written down before the run, from reading the rules. A small table of packets that must be allowed and must be refused is the cheapest regression test for a network.
Check by hand: a /24 leaves 32 – 24 = 8 bits for hosts, which is 2 to the 8 = 256 addresses, and a /22 leaves 10 bits, which is 1,024 addresses, the same as four /24 blocks. So 10.20.4.0/22 covers 10.20.4.0 to 10.20.7.255, and the address 10.20.5.30 is inside it, while 10.20.1.5 is not. A /22 block must start at a multiple of four in the third number, which is why the tiers start at 0, 4 and 8.
The block 0.0.0.0/0 means every address on the internet. Even with a strong password, an exposed database is found by scanners within hours. Allow only the application subnets.
Choose ranges that do not overlap with offices, partners or other networks you may connect to, and leave gaps between tiers for later growth.
No cloud account is needed for this course. The cloud services in these lessons are small simulations that run on your own computer, in Python 3.13 or Node.js 22, because the ideas, such as access policies, queues, autoscaling and templates, are the same at every provider. Prices and names are invented or generic, and each provider’s services and prices change often, so check the current documentation of the provider you use. Every result was checked a second way. Providers differ in how many addresses they reserve, in whether security groups are stateful, and in the extra network firewalls they offer. Check the documentation.
Practice
Add a /28 subnet for a bastion host and check that it fits. Write a rule that lets the application servers reach the database and a test that proves a web server cannot. Compute how many /24 subnets fit in the VPC.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
How many addresses are in a /24?
- 24
- 65,536
- 256
- 1,024
32 – 24 = 8 host bits.
Which subnet should hold the database?
- A public subnet
- A private subnet without inbound internet access
- Any subnet
- The load balancer’s subnet
Only the application tier should reach it.
What is the default of a security group?
- Allow everything
- Deny anything not allowed
- Allow ports below 1024
- Allow local traffic only
Allow-lists.
What does 0.0.0.0/0 mean in a rule?
- Every address on the internet
- No address
- The local network
- One address
Avoid it for anything but public web ports.
Why test rules with a table of packets?
- It catches rules that open or close too much
- It adds addresses
- It is required
- It speeds the network
Two implementations agree.
Summary
- CIDR blocks name address ranges by prefix; each bit halves the block.
- Use public subnets only for what must be reached, and private ones for the rest.
- Security groups are default-deny allow lists, layered by tier.
- Verify address maths and rules with two implementations.
Questions
What is a NAT gateway?
A gateway that lets private servers start connections to the internet, for example for updates, without letting the internet start connections to them.
What is the difference between a security group and a network ACL?
A security group is attached to a resource, usually remembers connections and only allows. A network ACL applies to a subnet and is often stateless. Check your provider.
Do I need IPv6?
It is increasingly supported and gives far more addresses. Check which services in your plan support it, and plan the rules for both versions.
