Lesson 8 of 11 · 11 min · AWS networking
VPC: subnets, route tables, gateways and NAT
What you will learn
- What a VPC is and how to choose its CIDR block
- Public and private subnets and what really distinguishes them
- Route tables, the internet gateway and NAT gateways
- VPC endpoints for private access to AWS services
- Building it step by step with the CLI, and the equivalent design
In simple words
A VPC is your own private network inside the cloud, like a fenced plot of land. You divide it into areas: some with a gate to the internet (public) and some without (private). Web servers go near the gate. Databases stay deep inside, where the internet can't reach them directly.
Words to know
- VPC: virtual private cloud: your own isolated network in the cloud.
- Subnet: one section of the VPC, with its own range of addresses.
- Route table: the rules that say where traffic from a subnet is sent.
- Internet gateway: the VPC's door to the internet.
- NAT gateway: lets private machines reach out to the internet without being reachable from it.
The VPC
A Virtual Private Cloud (VPC) is your own isolated virtual network inside a region: you choose its private IP range, subnets, routing and gateways (the Networking course's IP addressing, subnets and routing, applied). A VPC spans all AZs of its region. Nothing enters or leaves unless you permit it.
Choosing the CIDR block: use RFC 1918 space (10.0.0.0/16 gives 65,536 addresses). Rules from the Networking course apply: do not overlap with other VPCs, on-premises networks or partners you may connect later; leave room for growth; a /16 per VPC and /24 subnets is a common start (AWS allows /16 to /28 sizes). Each subnet loses 5 addresses to AWS (network, router, DNS, future use, broadcast). A new account has a default VPC (172.31.0.0/16) with public subnets, convenient for experiments and unsuitable for production, where you design your own.
Subnets
A subnet is a range inside the VPC, in one availability zone. You create subnets in several zones. A typical layout, as in the Terraform course:
| Subnet | CIDR | Purpose |
|---|---|---|
| public-a / public-b | 10.0.1.0/24, 10.0.2.0/24 |
load balancers, NAT gateways, (bastion) |
| private-app-a / -b | 10.0.11.0/24, 10.0.12.0/24 |
application servers/containers |
| private-data-a / -b | 10.0.21.0/24, 10.0.22.0/24 |
databases, caches |
What makes a subnet public? Not a checkbox, but its route table: a subnet is public if its route table sends internet-bound traffic (0.0.0.0/0) to an internet gateway (IGW); it is private if not. Instances in a public subnet also need a public IP (or Elastic IP) to be reachable from the internet; those without it can only initiate outbound traffic.
Route tables
Every subnet is associated with exactly one route table: a list of destination → target rules; the most specific (longest prefix) match wins, as in the Networking course. Every route table has an implicit local route for the VPC's own CIDR, so all subnets in the VPC can reach each other by default (subject to security groups and NACLs).
We built a VPC with the CLI. A public route table:
aws ec2 create-route --route-table-id $RTB --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW
aws ec2 describe-route-tables --route-table-ids $RTB --query 'RouteTables[0].Routes[].[DestinationCidrBlock,GatewayId]' --output text
10.0.0.0/16 local
0.0.0.0/0 igw-2d7756ac
and the VPC's main route table, which applies to any subnet you did not associate explicitly:
10.0.0.0/16 local
Only the local route, so those subnets have no internet access. This is a good safe default: create your own explicit route tables and associate them, and never add an IGW route to the main route table (new subnets would become public by accident).
Internet gateway
An internet gateway is a horizontally scaled, redundant VPC component (no bandwidth limit, no extra charge for the gateway itself) that allows two-way communication between the VPC and the internet for resources with public IPs. One IGW per VPC:
IGW=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id $IGW --vpc-id $VPC
An instance in a private subnet cannot be reached from outside, and cannot reach out either, because it has no route to the IGW. Often it must reach out: for OS updates, pulling images, calling third-party APIs.
NAT gateways
A NAT gateway lives in a public subnet, has an Elastic IP, and lets instances in private subnets start outbound connections to the internet while blocking inbound connections started from outside. The private subnet's route table points 0.0.0.0/0 at it:
EIP=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
NAT=$(aws ec2 create-nat-gateway --subnet-id $PUB --allocation-id $EIP --query NatGateway.NatGatewayId --output text)
aws ec2 create-route --route-table-id $RT2 --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT
10.0.0.0/16 local
0.0.0.0/0 nat-fb1d8e04f1b2b9af6 <- private route table
The packet flow, matching the Networking course's NAT: instance (10.0.11.5) → private route table → NAT gateway in the public subnet → NAT rewrites the source to its Elastic IP → public route table → IGW → internet; replies come back the same way and are translated back to the instance.
Operational and cost points:
- A NAT gateway is zonal. For resilience create one per AZ and route each AZ's private subnets to its own NAT; a single NAT in AZ a means AZ a's failure cuts off egress for everyone, and cross-AZ traffic through it costs extra.
- They are billed per hour plus per GB processed. A chatty private fleet (pulling Docker images, S3 transfers) can produce large bills (cost lesson). Avoid pushing AWS-service traffic through NAT with VPC endpoints.
- A NAT instance (an EC2 you manage) is cheaper for tiny setups but you patch and scale it. IPv6 uses an egress-only internet gateway instead of NAT.
- Fully private workloads that never need the internet need no NAT at all.
VPC endpoints
Services like S3, DynamoDB, ECR, Secrets Manager and CloudWatch Logs are reached through public endpoints. A VPC endpoint gives private access:
- Gateway endpoints (S3 and DynamoDB only): a route-table entry targeting the service's prefix list. Free, no NAT or IGW needed, highly available. Create them in every VPC that uses S3.
- Interface endpoints (AWS PrivateLink): an elastic network interface with a private IP in your subnet for the service, billed per hour and GB. Use for ECR, SSM, Secrets Manager, CloudWatch, STS and for private access to third-party SaaS. Endpoint policies restrict what can be done through them.
Benefits: traffic stays on the AWS network, instances need no internet path, security improves (policies can require the endpoint, e.g. an S3 bucket policy aws:SourceVpce), and NAT costs fall.
Connecting networks
- VPC peering: private connection between two VPCs (non-overlapping CIDRs; not transitive).
- Transit Gateway: a hub that connects many VPCs and on-premises networks transitively.
- Site-to-Site VPN / Direct Connect: connect to your office or data centre (VPN over the internet; Direct Connect a dedicated line).
- PrivateLink: expose a service privately to other VPCs/accounts without peering.
The design to remember
VPC 10.0.0.0/16
public subnets (AZ a, AZ b): route 0.0.0.0/0 → IGW : ALB, NAT gateways
private app (AZ a, AZ b): route 0.0.0.0/0 → NAT in same AZ : app servers / tasks
private data (AZ a, AZ b): local only (no internet route) : RDS, ElastiCache
S3 gateway endpoint on the private route tables
Only the load balancer is exposed. The data tier has no route to the internet at all. This is the same design that the Terraform course created, with aws_vpc, aws_subnet and a security group.
Flow logs and troubleshooting
- VPC Flow Logs record accepted/rejected traffic metadata (source, destination, port, action) per interface to CloudWatch Logs or S3: invaluable for "who is blocking this connection?" and for security analysis.
- Reachability Analyzer simulates a path between two resources and tells you which route table, security group or NACL blocks it.
- Standard checks when "A cannot reach B": (1) are they in the same VPC or is there peering/routing? (2) route tables on both sides, (3) security groups (stateful), (4) NACLs (stateless, both directions), (5) the OS firewall and whether the service listens on the right address, (6) DNS resolution (VPC DNS settings, private hosted zones). The Linux and Networking troubleshooting lessons apply on the instance.
Common mistakes
- Overlapping CIDR blocks that block peering later.
- A subnet "public" by accident (IGW route in the main route table).
- Databases in public subnets, or with public IPs.
- A single NAT gateway for all AZs.
- Sending S3 traffic through NAT instead of a free gateway endpoint.
- Subnets too small (a /28 leaves 11 addresses; ENIs for containers and Lambda consume IPs quickly).
- Using the default VPC for production.
- No flow logs when you need them.
Try it yourself
- Create a VPC with two public and two private subnets across two AZs, an IGW, a NAT gateway, and the associated route tables (CLI, console or Terraform). Print each route table.
- Launch an instance in a private subnet and verify it reaches the internet through NAT but cannot be reached from outside.
- Add an S3 gateway endpoint and check the route table entry.
- Use Reachability Analyzer or Flow Logs to find why a connection is blocked.
Check yourself
What makes a subnet public?
Its route table has a route for 0.0.0.0/0 to an internet gateway (and instances there need public IPs to be reachable). A private subnet has no such route.
What is the difference between an internet gateway and a NAT gateway?
An IGW provides two-way connectivity for resources with public IPs. A NAT gateway lets private resources initiate outbound connections only, hiding them behind its Elastic IP.
Why create a NAT gateway in every availability zone?
A NAT gateway is zonal. One per AZ avoids a single point of failure and avoids cross-AZ data charges.
What is the advantage of a gateway VPC endpoint for S3?
Private, free access to S3 over the AWS network without NAT or internet gateway, improving security and cutting NAT costs.
Interview questions
Design a VPC for a three-tier application.
A /16 VPC across two or three AZs with public subnets (ALB, NAT), private app subnets routing egress to a per-AZ NAT, private data subnets with no internet route, S3/DynamoDB gateway endpoints, security groups chained by tier, flow logs on, and CIDRs chosen to avoid overlap with other networks.
An instance in a private subnet can't reach the internet. How do you troubleshoot?
Check its subnet's route table for a default route to a NAT gateway, that the NAT gateway is in a public subnet whose route table has an IGW route and an Elastic IP, security group egress rules, NACLs, DNS, and the NAT's state.
How is VPC peering different from Transit Gateway?
Peering is a one-to-one non-transitive link between two VPCs; Transit Gateway is a central hub allowing many VPCs and on-premises networks to connect transitively with central routing.
Summary
- A VPC is a private network in a region; subnets are per-AZ ranges; route tables decide where traffic goes; an IGW route makes a subnet public.
- NAT gateways give private subnets outbound-only internet; deploy one per AZ and mind its costs.
- Gateway endpoints (S3/DynamoDB) and interface endpoints keep AWS-service traffic private.
- Layout: public (ALB, NAT), private app, private data (no internet route); troubleshoot with route tables, security groups, NACLs, flow logs and Reachability Analyzer.
Going deeper
- Amazon VPC User Guide: all VPC components.
- Route tables: how routing works, including priority.
- NAT gateways: setup, pricing considerations and high availability.
- VPC endpoints (PrivateLink): gateway and interface endpoints.
- VPC Flow Logs: capturing traffic metadata.
- Reachability Analyzer: path analysis tool.
See how much of this you can explain
A free mock interview asks you new questions on Cloud platforms and AWS fundamentals for DevOps Engineer and scores every answer, with what to fix.