A VPC is your fenced plot, but inside you need to organize it into zones. These zones are called subnets, and the most important decision is which will be public (accessible from the internet) and which will be private (hidden and protected). This is the heart of secure network design in AWS.

What is a subnet

A subnet is a division of the VPC with its own address range (a slice of the VPC's CIDR). Each subnet lives in a specific availability zone (AZ).

Analogy: If the VPC is your fenced plot, the subnets are the different streets or zones within it. One zone faces the main entrance (public); another is inside, with no direct access from outside (private).

For example, if your VPC is 10.0.0.0/16, you could divide it like this:

VPC: 10.0.0.0/16
 ├── Public subnet  A:  10.0.1.0/24   (in AZ-a)
 ├── Public subnet  B:  10.0.2.0/24   (in AZ-b)
 ├── Private subnet A:  10.0.10.0/24  (in AZ-a)
 └── Private subnet B:  10.0.20.0/24  (in AZ-b)

Each /24 gives about 250 addresses. Notice there are subnets in two different AZs: that's for high availability, as we saw in Chapter 3.

Public vs Private Subnet: the key difference

Here's the central concept. The difference is not in the subnet itself, but in whether or not it has a route to the internet:

Public subnet Private subnet
Accessible from the internet? Yes No
Do its resources have a public IP? Yes No
Has a route to the internet? Yes (via Internet Gateway) Not directly
What you put here What needs to be accessible What needs to be protected

Practical definition: a subnet is public if it has a route to an Internet Gateway (the door to the internet, subchapter 6.3). If it doesn't, it's private. That route is what makes it public or not. We'll see this in detail in subchapter 6.4 (route tables).

What goes in each subnet

This is the design decision that defines the security of your architecture:

In the public subnet (the "reception")

Resources that need to be accessible from the internet:

  • Web servers that users visit.
  • Load balancers (Chapter 13) that receive traffic.
  • NAT Gateways (subchapter 6.3).

In the private subnet (the "back office")

Sensitive resources that should not be exposed:

  • Databases (never expose a database to the internet!).
  • Application servers with internal business logic.
  • Internal systems, queues, background processes.

Real example — typical web architecture:

Internet
   │
   ▼
[Public subnet]  ← Load balancer + web server
   │ (communicates internally)
   ▼
[Private subnet]  ← Application server + Database

Users only reach the public subnet. The database, in the private subnet, is invisible from the internet: only the application server (inside the VPC) can talk to it. Even if an attacker compromised something, the database still has no entry point from outside.

The golden security rule

Put in public subnets only what STRICTLY needs access from the internet. Everything else, in private subnets.

This applies the principle of defense in depth: the fewer things you expose, the smaller the attack surface. A database in a private subnet is much more secure than one accessible from the internet, even if both have a password.

Always spread across multiple AZs

Remember subchapter 3.2: for high availability, create subnets in at least two AZs. That way, if one AZ fails, your application keeps running from the other.

That's why the usual "minimum decent" design has four subnets: one public and one private in each of two AZs.

        AZ-a                    AZ-b
 ┌─ public ─┐           ┌─ public ─┐
 │          │           │          │
 ┌─ private ─┐          ┌─ private ─┐
 │  DB       │          │  DB       │   ← replica in another AZ

But then, how does the database access the internet to update itself?

A good question that always comes up: if the database or a server is in a private subnet (with no internet access), how do they download software updates, for example?

The answer is the NAT Gateway, which allows private resources to go out to the internet (to download things) without allowing the internet to come in to them. It's a one-way street outward. We'll see this in detail in the next subchapter.

What you should remember

  • A subnet is a division of the VPC, located in one AZ, with its own IP range.
  • Public = has a route to the internet (for what needs to be accessible). Private = no direct route to the internet (for sensitive things).
  • Golden rule: expose only what's strictly necessary in public subnets; put databases and internal systems in private subnets.
  • Spread subnets across multiple AZs for high availability (typical design: public + private in 2 AZs).
  • Private resources can go out to the internet securely via a NAT Gateway (next subchapter).

In the next subchapter we'll see the two "doors" of your network: the Internet Gateway (public in/out) and the NAT Gateway (secure outbound for private resources).

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved