VPC Networking

Your private network in AWS: subnets, internet and NAT gateways, VPN, and Direct Connect.

What is it?

An Amazon VPC (Virtual Private Cloud) is an isolated network you define inside a Region. You pick its IP range in CIDR notation (for example 10.0.0.0/16) and split it into subnets, each in a single AZ.

  • Public subnet: has a route to an Internet Gateway (IGW), so resources with public IPs can reach the internet and be reached from it.
  • Private subnet: no direct route from the internet; ideal for databases and app servers.
  • NAT gateway: lets private resources open outbound connections (updates, APIs) without accepting inbound ones. It lives in a public subnet.
  • Route tables: decide where a subnet's traffic goes.
  • Virtual private gateway + Site-to-Site VPN: an encrypted tunnel over the internet to your on-premises network.
  • AWS Direct Connect: a dedicated private connection from your site to AWS, steadier than the internet.

Other connectors: VPC peering (connect two VPCs), Transit Gateway (a hub for many VPCs and networks), and VPC endpoints (reach AWS services privately, without the internet).

Scope and sizing: a VPC belongs to one Region. Each subnet sits in exactly one AZ, which is how you choose an EC2 instance's AZ. In CIDR, a bigger suffix means a smaller range: /16 holds 65,536 addresses, /24 holds 256, /32 is one IPv4 address and /0 is every address. A subnet's range must fit inside its VPC's range.

AWS doesn't enforce 'public' or 'private'; the words describe a subnet's routing. To make a subnet public:

  • Create an internet gateway and attach it to the VPC.
  • Add a route 0.0.0.0/0 -> IGW to the subnet's route table.
  • Give instances public IPs, for example by turning on auto-assign public IPv4 for the subnet.

A brand-new route table has only the local route.

Hybrid connectivity options

  • AWS Site-to-Site VPN: connects an office or data center network to a VPC.
  • AWS Client VPN: connects individual users (remote employees) to AWS or on-premises networks.
  • Both build encrypted tunnels over the internet with IPsec, a protocol suite that authenticates and encrypts IP packets.
  • AWS Direct Connect: a dedicated private line from your data center, office or a colocation facility (a shared data center where you rent space and bandwidth) into AWS, bypassing the public internet. Higher, steadier bandwidth, lower latency, and possibly lower data transfer costs. Lower-bandwidth hosted connections come through AWS partners; higher-bandwidth dedicated ones come straight from AWS.
  • AWS PrivateLink (interface VPC endpoints): reach AWS or partner services from your VPC without crossing the public internet.

Exam trap: Direct Connect is private but not encrypted by default. If you need encryption, run a Site-to-Site VPN over it, or use MACsec on supported connections.

Practical notes: resource 'names' in the console are just the Name tag. Many managed services (S3, DynamoDB, Lambda by default) run in AWS-managed networks, so you don't pick a VPC for them. You can't delete a VPC while instances still run in it.

Explain like I'm 10

A VPC is a gated campus. The main gate (internet gateway) opens onto a public plaza (public subnet) with reception and a cafe. Behind it are private office buildings (private subnets) with no street entrance. Staff leave through a controlled side door (NAT), but strangers can't walk in. A private tunnel to head office is the VPN; a dedicated road is Direct Connect.

Examples

Planning a VPC

VPC            10.0.0.0/16        (65,536 addresses)
  public-a     10.0.0.0/24   AZ a   route 0.0.0.0/0 -> IGW   (load balancer, NAT)
  public-b     10.0.1.0/24   AZ b   route 0.0.0.0/0 -> IGW
  private-a    10.0.10.0/24  AZ a   route 0.0.0.0/0 -> NAT   (app servers)
  private-b    10.0.11.0/24  AZ b   route 0.0.0.0/0 -> NAT
  data-a       10.0.20.0/24  AZ a   no internet route        (databases)
  data-b       10.0.21.0/24  AZ b   no internet route

Use the IP/CIDR calculator tool to check ranges and avoid overlaps (important if you later connect to on-premises).

Building a VPC with the CLI

VPC=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
SUBNET=$(aws ec2 create-subnet --vpc-id "$VPC" --cidr-block 10.0.0.0/24 \
  --availability-zone eu-west-1a --query Subnet.SubnetId --output text)
IGW=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id "$IGW" --vpc-id "$VPC"
RT=$(aws ec2 create-route-table --vpc-id "$VPC" --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id "$RT" --destination-cidr-block 0.0.0.0/0 --gateway-id "$IGW"
aws ec2 associate-route-table --route-table-id "$RT" --subnet-id "$SUBNET"

A subnet is 'public' because its route table points to an IGW, not because of its name.

How it works

Every packet leaving a subnet is matched against its route table; the most specific route wins. local routes keep traffic inside the VPC. A route to an IGW makes a subnet public (instances also need a public or Elastic IP).

A route to a NAT gateway lets private instances start outbound traffic. The NAT swaps their private addresses for its own public one and passes replies back.

For on-premises links, a VPN is quick to set up and encrypted over the internet. Direct Connect takes longer but gives a private, steadier link. Many designs use both: Direct Connect primary, VPN backup.

   Internet
      |
  [ Internet Gateway ]
      |
 +----+------------------ VPC 10.0.0.0/16 ---------------+
 | Public subnet           Private subnet                  |
 | [ALB] [NAT GW] <------- [App servers]  (outbound only)  |
 |                          |                              |
 |                       [Database subnet - no internet]   |
 +---------+-----------------------------------------------+
           |                      |
   [Virtual private gateway]  [Direct Connect]
        VPN tunnel -------- On-premises network

Why does it exist?

Early cloud servers sat on flat shared networks. A VPC gives you your own network design (address ranges, routing, segmentation), so you expose only what must be public and isolate the rest.

When to use it

Every workload on EC2, RDS and many other services lives in a VPC. Use public subnets for load balancers, private subnets for apps and data, NAT for outbound access, and VPN or Direct Connect for hybrid.

When not to use it

Don't put databases in public subnets 'for convenience'. Don't pick Direct Connect for a quick proof of concept (it has lead time); start with VPN. Skip NAT gateways for AWS service traffic when a VPC endpoint keeps it private and cheaper.

Common mistakes

  • Overlapping CIDR ranges that block later VPC peering or VPN connectivity.

  • Choosing a /16 everywhere without planning, or a range too small to grow.

  • Placing a single NAT gateway in one AZ and losing outbound access if that AZ fails.

  • Forgetting the route table association, so the subnet is not actually public.

  • Believing Direct Connect is encrypted by default (it is private, not automatically encrypted).

  • Reading CIDR backwards: a /24 is much smaller than a /16, and /0 means all addresses, not one (a single address is /32).

  • Naming a subnet 'public' and expecting it to work without an IGW route and public IPs.

  • Mixing up Site-to-Site VPN (whole networks) and Client VPN (individual users).

Practice exercises

  1. Easy:

    Explain why a subnet with an IGW route is called public and what else an instance needs to be reachable.

  2. Medium:

    Use the CIDR calculator to split 10.0.0.0/16 into six /24 subnets without overlap.

  3. Medium:

    Draw the route tables for public, private, and data subnets.

  4. Hard:

    Design connectivity from an office to AWS needing high reliability. Combine Direct Connect and VPN and describe failover.

  5. Easy:

    Without a calculator: which is larger, 10.0.0.0/16 or 10.0.5.0/24? How many addresses does a /32 cover?

  6. Medium:

    Choose Site-to-Site VPN, Client VPN, Direct Connect or PrivateLink for: 200 remote staff, a branch office, a steady multi-gigabit data feed, private access to a partner's SaaS API.

Interview questions

What makes a subnet public?

A route to an Internet Gateway in its route table (plus public IP addressing for the resources).

NAT gateway purpose?

Allows instances in private subnets to initiate outbound connections to the internet while preventing inbound connections from it.

VPN vs Direct Connect?

A VPN is an encrypted tunnel over the public internet, quick to set up; Direct Connect is a dedicated private connection with more consistent latency and bandwidth.

Exam-style: Which component allows internet access for a public subnet?

An Internet Gateway.

Exam-style: A company needs a private, dedicated link between its data center and AWS. Which service?

AWS Direct Connect.

Exam-style: Remote employees need secure access to resources in a VPC from their laptops. Which service?

AWS Client VPN.

Exam-style: How do you encrypt traffic that travels over AWS Direct Connect?

Run an AWS Site-to-Site VPN (IPsec) over the Direct Connect link, or use MACsec where supported. Direct Connect alone is not encrypted.

Exam-style: Which service lets a VPC reach AWS or partner services privately without using the public internet?

AWS PrivateLink (interface VPC endpoints).

Is a VPC global, regional or zonal? What about a subnet?

A VPC is regional; a subnet lives in a single Availability Zone.