Virtual networks, subnets and security groups

Intermediate · 18 min

Plan a cloud network: a CIDR that will not collide, public and private subnets per zone, routes through gateways, and security groups as the firewall.

Before thisRegions, availability zones and failure domainsFirewalls, ports and network troubleshootingCloud identity and least privilege

KodeKloud · 18 min

Virtual networks, subnets and security groups

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by KodeKloudReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A virtual network is your private address space in the cloud. Choose its CIDR range carefully, because overlapping ranges cannot be peered or joined by VPN later.
  • A subnet is public or private only because of its route table. Public subnets route 0.0.0.0/0 to an internet gateway, and private ones route outbound traffic through a NAT gateway or not at all.
  • Put one subnet per tier per zone, keep databases and app servers in private subnets, and expose only the load balancer publicly.
  • Security groups are stateful allow-lists attached to each instance. Return traffic is allowed automatically, and a rule can name another security group instead of an IP range.
  • AWS and Azure reserve five addresses in every subnet and Google Cloud reserves four, so a /28 gives you 11 or 12 hosts, not 16.

The problem: a server on the internet by default

The quickest way to launch a database in the cloud is to give it a public IP. Within minutes, scanners from around the world will be trying passwords on it. On your own servers you dealt with this using firewalls and ports. In the cloud the same job is split across a few building blocks: a private network, subnets, route tables, gateways and per-instance firewalls.

Think of an office building. The building is the network. Floors are subnets. The lobby faces the street and the server room does not. The route table is the signage saying which doors lead outside, and the security group is the badge reader on each room.

The pieces

Virtual network. A private IP range that belongs to you, in one region: a VPC on AWS and Google Cloud, a VNet on Azure. One difference: a Google Cloud VPC is global, with regional subnets, while AWS and Azure networks live in one region.

Subnet. A slice of the network’s range. On AWS a subnet lives in exactly one availability zone, so you create one per zone per tier. Azure and Google Cloud subnets span zones within a region.

Route table. Rules saying where traffic for each destination goes. Every subnet gets one.

Internet gateway. The door to the internet. A subnet is public only because its route table sends 0.0.0.0/0 to it. Nothing else makes a subnet public.

NAT gateway. Lets instances in private subnets open connections out, for updates or API calls, while nothing can connect in. It sits in a public subnet, and private route tables point 0.0.0.0/0 at it. NAT gateways are billed per hour and per gigabyte, and they show up on cost reports more often than people expect.

Security group. A stateful allow-list attached to each network interface. “Stateful” means if you allow a request in, the reply is allowed out automatically. Rules can name another security group as the source, which is the most useful feature here. Azure’s equivalent is a network security group, and Google Cloud uses VPC firewall rules that target instances by network tag or service account.

AWS also has network ACLs, stateless rules on a whole subnet. Most teams leave them at the default and do their filtering in security groups.

A worked example: a subnet plan that won’t collide

The decision that is hardest to undo is the network’s address range. If it overlaps your office network or another network you need to connect to, you can’t route between them. Changing it later means rebuilding everything inside.

I planned a network in Python with the standard ipaddress module. A 10.20.0.0/16, cut into /20 blocks: three tiers (public, private app, private data) across three zones, with spare blocks left over. Then I checked overlaps against three ranges we might need to connect to:

$ python3 subnets.py
public-a         10.20.0.0/20      4091 usable  first host 10.20.0.4
public-b         10.20.16.0/20     4091 usable  first host 10.20.16.4
public-c         10.20.32.0/20     4091 usable  first host 10.20.32.4
private-app-a    10.20.48.0/20     4091 usable  first host 10.20.48.4
private-app-b    10.20.64.0/20     4091 usable  first host 10.20.64.4
private-app-c    10.20.80.0/20     4091 usable  first host 10.20.80.4
private-data-a   10.20.96.0/20     4091 usable  first host 10.20.96.4
private-data-b   10.20.112.0/20    4091 usable  first host 10.20.112.4
private-data-c   10.20.128.0/20    4091 usable  first host 10.20.128.4
spare /20 blocks left for later: 7
overlap with 10.20.128.0/17 : ['private-data-c']
overlap with 10.21.0.0/16   : none
overlap with 192.168.0.0/16 : none

“Usable” subtracts the five addresses AWS and Azure reserve in every subnet: the network address, the router, DNS, one held for future use, and the broadcast address. That is why the first host is .4. Google Cloud reserves four. The overlap check caught a real problem: if another team already owns 10.20.128.0/17, our third data subnet collides with it. The fix costs nothing now and would mean a rebuild later. Keep a shared register of who owns which range.

Generous /20 subnets are deliberate. Container platforms often give every pod its own IP address from the subnet, and a /24 runs out fast.

The security groups

Here is the tiered setup written as rules. I did not apply these, because I have no cloud account. They are the pattern, not a tested template:

sg-alb   inbound 443/tcp from 0.0.0.0/0
sg-app   inbound 8080/tcp from sg-alb
sg-db    inbound 5432/tcp from sg-app

Only the load balancer accepts traffic from the internet. App servers accept traffic only from the load balancer’s group, and the database only from the app servers’ group. A new app server joins the app group and gets database access with no IP addresses in any rule. For human access to private instances, use a session manager or a bastion, never a public SSH port.

About the video

“AWS Networking Basics | AWS VPC & Subnets for Beginners” is from KodeKloud, published June 2025, 18 minutes 19 seconds. Its chapters cover what a VPC is, public versus private subnets, internet gateway versus NAT gateway, virtual private gateways, and VPN and Direct Connect. Security groups are not listed in the chapters, so Tiny Technical Tutorials’ security groups video is linked below. The video is AWS-only and ends with a course promotion. English is judged from the title, description and channel. Sam Meech-Ward’s 2021 video builds the same network hands-on, including deploying to a private subnet behind a reverse proxy.

Where this fits

That completes the infrastructure building blocks. The last cloud lesson is about what they cost and who is paying: cloud cost and FinOps.

Check yourself

  1. 01What makes a subnet public?

    Its route table sends 0.0.0.0/0 to an internet gateway, and instances in it have public IPs. A private subnet has no such route.

  2. 02Your database's security group allows port 5432 from the app tier's security group. Why is that better than allowing the app subnet's CIDR?

    It follows the instances, not the addresses. Any instance in the app group can connect, wherever it lands, and nothing else in that subnet can.

  3. 03You picked 10.0.0.0/16 for the new VPC. Why might the network team object?

    10.0.0.0/16 is the most common default and likely overlaps the office network or another VPC you will need to peer or connect by VPN, and overlapping ranges cannot be routed between.

  4. 04Instances in a private subnet need to download OS updates. What do they need?

    A route for 0.0.0.0/0 to a NAT gateway in a public subnet, which lets them start outbound connections without being reachable from the internet.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark