Skip to content
Visual explainer 3h to learn 5 steps

Virtual private clouds

Design a VPC with public and private subnets, route tables and gateways.

By Invicly Tech, , in Computer Networking. About 12 minutes to read.

Topic 44 of 51

In short

A virtual private cloud is your own isolated network inside a cloud provider, defined by a CIDR block and divided into subnets across availability zones. Route tables decide whether a subnet is public, with a route to an internet gateway, or private, reaching out only through a NAT gateway. Security groups and network ACLs filter traffic.

Explained simply

A VPC (virtual private cloud) is your own private network inside a cloud provider like AWS, Azure or Google Cloud. You choose its IP address range, cut it into smaller pieces called subnets, and decide which pieces can reach the internet. Nobody else's servers can see into it unless you allow it.

Subnets live in availability zones (AZs), which are separate data centres in one region. You put copies of your servers in two or three AZs, so if one building has a power problem, the others keep running.

Route tables decide where traffic goes. A public subnet has a route to an internet gateway, so servers there can have public IPs. A private subnet has no such route; its servers can only reach the internet outbound through a NAT gateway, and the internet cannot start connections to them.

Two kinds of firewalls protect a VPC. Security groups wrap each server and remember connections. Network ACLs guard whole subnets and check each packet alone. Most teams rely mainly on security groups and keep NACLs simple.

Press Play on a diagram to walk through it one step at a time.

LayersA typical two-tier VPC (10.0.0.0/16)
  1. 1
    Internet gateway

    Attached to the VPC.

  2. 2
    Public subnets 10.0.0.0/24, 10.0.1.0/24

    Load balancer, NAT gateway; route 0.0.0.0/0 to IGW.

  3. 3
    Private app subnets 10.0.10.0/24, 10.0.11.0/24

    App servers; route 0.0.0.0/0 to NAT.

  4. 4
    Private data subnets 10.0.20.0/24, 10.0.21.0/24

    Databases; no internet route.

MapRoute tables in a two-AZ VPC (10.0.0.0/16)
VPC route tables
  • Public RT
    • 10.0.0.0/16 local
    • 0.0.0.0/0 to igw
    • Used by both public subnets
  • Private RT, AZ a
    • 10.0.0.0/16 local
    • 0.0.0.0/0 to NAT gateway in AZ a
    • S3 prefix list to gateway endpoint
  • Private RT, AZ b
    • 10.0.0.0/16 local
    • 0.0.0.0/0 to NAT gateway in AZ b
  • Data RT
    • 10.0.0.0/16 local only
    • No route to the internet

In depth

Choosing the CIDR block

The VPC's CIDR block is hard to change later, so plan it first. In AWS, an IPv4 VPC block must be between /16 (65,536 addresses) and /28 (16 addresses), and you can add secondary blocks later. Use private RFC 1918 ranges: 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16.

The golden rule is no overlap. If two VPCs, or a VPC and your office, use the same range, you cannot route between them with peering or VPN. A simple plan gives each environment and region its own block, for example 10.10.0.0/16 for production in one region, 10.20.0.0/16 for staging, 10.30.0.0/16 for another region, and keeps the office on 10.100.0.0/16. Inside the VPC, AWS reserves 5 addresses in every subnet: the network address, the first three after it (VPC router, DNS, and one reserved for future use) and the last address. So a /24 subnet gives 251 usable IPs.

  • AWS IPv4 VPC size: /16 to /28
  • 5 reserved addresses per subnet: .0, .1, .2, .3 and the last
  • Never overlap with other VPCs or on-premises networks
  • Leave unused space for future subnets

Public subnets, private subnets and gateways

Subnets are not public or private by a setting; their route table decides. A route 0.0.0.0/0 to an internet gateway (IGW) makes a subnet public. The IGW is attached to the VPC, scales automatically and performs one-to-one NAT between an instance's private IP and its public or Elastic IP. An instance in a public subnet still needs a public IP to be reachable.

Private subnets send 0.0.0.0/0 to a NAT gateway instead. The NAT gateway sits in a public subnet with an Elastic IP and translates many private addresses to that one public address, allowing outbound connections only. It belongs to one AZ, so for resilience you create one per AZ and point each private route table at the NAT gateway in its own AZ. NAT gateways are charged per hour and per GB processed, so heavy traffic to AWS services is better sent through VPC endpoints: gateway endpoints for S3 and DynamoDB are free and are added as routes.

FlowA private instance downloading an update
  1. 1
    Private instance

    10.0.10.25 in a private app subnet

  2. 2
    Route table

    0.0.0.0/0 goes to the NAT gateway

  3. 3
    NAT gateway

    In a public subnet, swaps source to its Elastic IP

  4. 4
    Public route table

    0.0.0.0/0 goes to the internet gateway

  5. 5
    Internet

    Reply returns the same way; inbound starts are impossible

Security groups versus network ACLs

A security group is a stateful firewall attached to a network interface. It has only allow rules; anything not allowed is denied. Because it is stateful, a reply to an allowed request always gets back in. A rule's source can be another security group, which is very powerful: "allow 5432 from the app-servers group" keeps working as app servers come and go, with no IP addresses in the rule.

A network ACL is stateless and applies to a whole subnet. It has numbered allow and deny rules, checked from the lowest number up, and the first match wins. Because it is stateless, you must allow return traffic yourself, usually the ephemeral ports 1024 to 65535 in the other direction. The default NACL allows everything; a custom NACL denies everything until you add rules. Use security groups for nearly all filtering, and NACLs for broad subnet-wide blocks, such as denying a known bad range.

CompareSecurity group versus network ACL
Security group
  • Attached to an interface
  • Stateful: replies allowed
  • Allow rules only
  • Can reference other groups
  • All rules evaluated together
Network ACL
  • Attached to a subnet
  • Stateless: allow returns yourself
  • Allow and deny rules
  • Rules by number, first match wins
  • Custom NACL denies all at first

Connecting VPCs: peering and transit

VPC peering joins two VPCs, in the same or different accounts and regions, so they route to each other with private IPs. You create the peering connection, the other side accepts it, and then both sides add routes for the other CIDR pointing at the peering connection, plus security group rules. Peering is not transitive: if A peers with B and B peers with C, A still cannot reach C through B. With many VPCs, a full mesh of peerings grows quickly (n x (n-1) / 2 links for n VPCs).

A transit gateway is a regional hub instead: each VPC, VPN and Direct Connect link attaches once, and the transit gateway's route tables decide who can reach whom. It costs per attachment and per GB, but it is much easier to run at scale. For exposing one service to many VPCs without full network access, PrivateLink (interface endpoints) publishes just that service behind a private IP in the consumer's VPC.

  • Peering: no overlapping CIDRs, no transitive routing
  • Add routes on both sides after accepting a peering
  • Transit gateway: hub for many VPCs and VPNs
  • PrivateLink: share one service, not the whole network

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Choose a VPC CIDR block~35m
  2. 2
    Create public and private subnets~35m
  3. 3
    Attach internet and NAT gateways~35m
  4. 4
    Compare security groups and network ACLs~35m
  5. 5
    Connect VPCs with peering~35m

Key points

  • Choose a VPC CIDR that will not overlap with offices or other VPCs.
  • A subnet is public only because its route table points to an internet gateway.
  • Security groups are stateful and attach to instances; NACLs are stateless per subnet.
  • Peering and transit gateways connect VPCs; peering is not transitive.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
aws ec2 create-vpc --cidr-block 10.0.0.0/16
aws ec2 create-subnet --vpc-id vpc-123 --cidr-block 10.0.0.0/24 --availability-zone us-east-1a
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --vpc-id vpc-123 --internet-gateway-id igw-123
aws ec2 create-route --route-table-id rtb-123 --destination-cidr-block 0.0.0.0/0 --gateway-id igw-123

Adding a NAT gateway for private subnets

App servers in private subnets cannot install packages. Step 1: allocate an Elastic IP. Step 2: create the NAT gateway in a public subnet of the same AZ. Step 3: wait for it to become available. Step 4: add a default route to the private route table pointing at the NAT gateway. Step 5: from an app server, curl an external site to test. Repeat per AZ for resilience.

bash
EIP=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
NAT=$(aws ec2 create-nat-gateway --subnet-id subnet-pub-a \
  --allocation-id "$EIP" --query NatGateway.NatGatewayId --output text)
aws ec2 wait nat-gateway-available --nat-gateway-ids "$NAT"
aws ec2 create-route --route-table-id rtb-priv-a \
  --destination-cidr-block 0.0.0.0/0 --nat-gateway-id "$NAT"

Letting only the app tier reach the database

The database must accept PostgreSQL only from app servers. Step 1: create a security group for the database. Step 2: add an inbound rule for TCP 5432 whose source is the app servers' security group, not an IP range. Step 3: attach it to the database. Step 4: any new app server with the app security group can connect at once, while a bastion host or a web server without it is refused. No outbound or return rule is needed, because security groups are stateful.

bash
DB_SG=$(aws ec2 create-security-group --group-name db-sg \
  --description "Postgres from app tier" --vpc-id vpc-123 \
  --query GroupId --output text)
aws ec2 authorize-security-group-ingress --group-id "$DB_SG" \
  --protocol tcp --port 5432 --source-group sg-app123

Quick reference

  • A subnet is public only if its route table sends 0.0.0.0/0 to an internet gateway
  • AWS reserves 5 IPs per subnet, so a /24 gives 251 usable addresses
  • One subnet lives in exactly one availability zone; a VPC spans the region
  • NAT gateway: outbound only, lives in a public subnet, one per AZ for resilience
  • Security groups: stateful, allow-only, can reference other security groups
  • NACLs: stateless, numbered allow and deny, remember ephemeral return ports
  • Peering is not transitive and needs non-overlapping CIDRs
  • S3 and DynamoDB gateway endpoints are free and avoid NAT data charges

Common mistakes

  • Using 10.0.0.0/16 everywhere, then needing to peer overlapping VPCs.
  • Putting databases in public subnets.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1What makes a subnet public in AWS?

Question 2

2How many usable IP addresses does an AWS /26 subnet provide?

Question 3

3VPC A peers with B, and B peers with C. Can A reach C through B?

Question 4

4You add an inbound NACL rule for TCP 443 but clients still time out. Security groups allow 443. What is likely missing?

Question 5

5Why should each AZ have its own NAT gateway?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Split 10.0.0.0/16 into six /24 subnets for public, app and data tiers across two AZs, and say how many usable IPs each has in AWS.

  2. Exercise 2medium

    Write the AWS CLI commands to create a VPC, one public subnet, an internet gateway and a public route table, and associate the table with the subnet.

  3. Exercise 3hard

    Your company has an office on 10.0.0.0/16 and three VPCs that all use 10.0.0.0/16. You must connect everything. Propose a new addressing plan and a connection design.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is a VPC free?
In AWS, creating a VPC, subnets, route tables and security groups is free. You pay for things like NAT gateways, public IPv4 addresses, VPN connections, transit gateways and data transfer.
What is the difference between a NAT gateway and an internet gateway?
An internet gateway gives instances with public IPs two-way internet access. A NAT gateway lets instances with only private IPs start outbound connections while blocking inbound ones.
Should I use security groups or network ACLs?
Use security groups for almost all rules because they are stateful and can reference each other. Add NACLs only for coarse subnet-wide blocks.
What is the default VPC?
AWS creates one default VPC per region with a /16 block and a public subnet in each AZ, so you can launch instances quickly. For real workloads, most teams build their own VPC with a planned layout.

Go deeper

ShareXLinkedInWhatsApp