VPC — Networking on AWS

Amazon VPC in depth — CIDR planning, subnets per AZ, route tables, internet and NAT gateways, public vs private subnets, security groups vs network ACLs, VPC endpoints and PrivateLink, peering vs Transit Gateway, VPN and Direct Connect, flow logs, IPv6, and a production reference layout.

Intermediate 7 min readLesson 11 of 19 Interview Q&A

A Virtual Private Cloud (VPC) is your own logically isolated network inside an AWS region: you choose the IP range, carve it into subnets, decide what routes where, and control traffic with firewalls. Nearly every non-serverless resource — EC2, RDS, ECS tasks, load balancers — lives in one. The underlying concepts are in IP addressing and subnetting and Routing.

The reference layout

VPC 10.0.0.0/16AZ aAZ bInternet gatewayPublic 10.0.0.0/24ALB · NAT gatewayPublic 10.0.1.0/24ALB · NAT gatewayPrivate app 10.0.10.0/24EC2 · ECS tasksPrivate app 10.0.11.0/24EC2 · ECS tasksPrivate data 10.0.20.0/24RDS primaryPrivate data 10.0.21.0/24RDS standby

Three tiers of subnets, repeated in each AZ:

  • Public subnets have a route to the internet gateway: load balancers, NAT gateways, bastions if any.
  • Private app subnets have no inbound path from the internet; outbound traffic goes through a NAT gateway in the public subnet.
  • Private data subnets often have no internet route at all.

CIDR and subnets

  • A VPC gets an IPv4 CIDR block between /16 (65,536 addresses) and /28. Use RFC 1918 ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
  • Plan for the future: VPCs that will be peered or connected to on-premises networks must not overlap. Allocate ranges centrally (VPC IP Address Manager) — re-addressing later is painful.
  • Each subnet lives in exactly one AZ. AWS reserves 5 addresses per subnet (network, router, DNS, future use, broadcast), so a /24 has 251 usable.
  • Containers and Lambda in a VPC consume IPs per task or ENI — size app subnets generously (/20 or larger for EKS).
  • IPv6: VPCs can be dual-stack; IPv6 addresses are globally unique, so private subnets use an egress-only internet gateway for outbound-only access.

Routing

Each subnet is associated with a route table; the most specific matching route wins.

Public route table                     Private app route table (AZ a)
Destination     Target                 Destination      Target
10.0.0.0/16     local                  10.0.0.0/16      local
0.0.0.0/0       igw-0abc               0.0.0.0/0        nat-0aaa
                                       pl-s3 (S3)       vpce-s3

A subnet is "public" only because its route table sends 0.0.0.0/0 to an internet gateway — there's no checkbox.

NAT gateways

A NAT gateway lets private instances initiate outbound connections (package updates, third-party APIs) while blocking inbound ones. It's a managed, zonal service:

  • Deploy one per AZ and route each private subnet to the NAT in its own AZ — otherwise an AZ failure breaks outbound traffic everywhere, and cross-AZ traffic costs extra.
  • NAT gateways charge per hour and per GB processed — one of the most common surprise bills. Large S3, ECR or DynamoDB traffic through NAT is wasteful; use VPC endpoints instead.

Security groups vs network ACLs

Security groupNetwork ACL
Attached toNetwork interfaces (instances, tasks, RDS, load balancers)Subnets
StateStateful — return traffic automatically allowedStateless — must allow return traffic (ephemeral ports 1024–65535) explicitly
RulesAllow onlyAllow and deny, evaluated in numbered order
SourcesCIDRs, prefix lists, or other security groupsCIDRs only
Typical useThe primary firewallCoarse subnet-level guardrails, blocking specific IP ranges

Chain security groups by reference — this is the core of network least privilege:

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

New instances join a tier simply by getting the right security group; no IPs to maintain.

Reaching AWS services privately

By default, calls to S3 or DynamoDB from a private subnet go out through the NAT to public endpoints. VPC endpoints keep the traffic on the AWS network:

TypeServicesHowCost
Gateway endpointS3 and DynamoDB onlyA route-table entryFree
Interface endpoint (PrivateLink)Most AWS services (ECR, Secrets Manager, SQS, STS, CloudWatch…) and partner/your own servicesAn ENI with a private IP in your subnet; private DNS resolves the service name to itHourly per AZ + per GB

Endpoint policies restrict which buckets or actions can be reached through the endpoint — a strong data-exfiltration control combined with aws:SourceVpce conditions in bucket policies (S3).

PrivateLink also lets you expose your own service (behind an NLB) to other VPCs or customers without peering whole networks.

Connecting networks

VPC peering
  • One-to-one connection between two VPCs (cross-account and cross-region allowed)
  • Not transitive: A↔B and B↔C does not give A↔C
  • No bandwidth bottleneck, no hourly charge (only data transfer)
  • Gets unmanageable beyond a handful of VPCs (n² connections)
Transit Gateway
  • A regional hub router: attach many VPCs, VPNs and Direct Connect
  • Transitive routing with route tables for segmentation (prod can't reach dev)
  • Peer Transit Gateways across regions
  • Per-attachment and per-GB charges

Hybrid connectivity to on-premises:

  • Site-to-Site VPN: IPsec tunnels over the internet — quick to set up, variable performance.
  • Direct Connect: a dedicated private circuit (1–100 Gbps) — consistent latency and lower data-transfer cost; pair with a VPN or a second connection for redundancy.
  • Client VPN for remote users.

VPC Lattice is a newer option for service-to-service connectivity across VPCs and accounts at the application layer, with IAM-based auth.

DNS inside the VPC

The VPC resolver (at the base address + 2, e.g. 10.0.0.2) resolves public names, AWS service names and Route 53 private hosted zones (orders.internal). Resolver endpoints forward queries between the VPC and on-premises DNS (Route 53 and CloudFront).

Visibility and troubleshooting

  • VPC Flow Logs record accepted and rejected connections (5-tuple, bytes, action) to CloudWatch Logs or S3 — useful for security investigations and debugging "why can't A reach B".
  • Reachability Analyzer checks the path between two resources and tells you which route table, security group or NACL blocks it.
  • Traffic Mirroring copies packets to inspection tools.

A connection that times out usually means a security group, NACL or route problem; a connection that's refused means it reached the host but nothing is listening (Network tools).

Interview questions
QWhat makes a subnet public versus private?easy

Only its route table. A public subnet's route table has a default route (0.0.0.0/0) to an internet gateway, so resources with public IPs can be reached from and reach the internet. A private subnet has no route to an internet gateway; it may route outbound traffic through a NAT gateway, which allows connections initiated from inside but blocks unsolicited inbound connections.

QSecurity groups vs network ACLs?easy

Security groups attach to network interfaces, are stateful (responses are automatically allowed), support allow rules only, and can reference other security groups as sources. Network ACLs attach to subnets, are stateless (return traffic on ephemeral ports must be explicitly allowed), support both allow and deny rules, and are evaluated in rule-number order. Security groups are the main tool; NACLs provide coarse subnet-level guardrails such as blocking an IP range.

QYour private instances are pulling terabytes from S3 and the NAT gateway bill is huge. What do you do?medium

Add an S3 gateway VPC endpoint and associate it with the private subnets' route tables. Traffic to S3 then goes directly over the AWS network without NAT processing charges, and gateway endpoints are free. Do the same for DynamoDB, and use interface endpoints for other heavy services such as ECR. Also make sure each private subnet uses a NAT gateway in its own AZ to avoid cross-AZ data charges.

QHow would you connect 50 VPCs across several accounts?hard

Use a Transit Gateway in a central networking account, shared with the other accounts through Resource Access Manager. Each VPC attaches to it, and Transit Gateway route tables segment traffic (for example, prod and dev isolated, both able to reach shared services). Peer Transit Gateways for other regions, attach VPN or Direct Connect for on-premises, and allocate non-overlapping CIDRs with IPAM. Peering would need over a thousand point-to-point connections and isn't transitive.

Key takeaways
  • A VPC is an isolated regional network; subnets live in one AZ and their route tables determine whether they're public or private.
  • Plan non-overlapping CIDR ranges early and size subnets for containers; AWS reserves five IPs per subnet.
  • Use one NAT gateway per AZ for private outbound access, and VPC endpoints to avoid NAT costs for AWS services.
  • Security groups are stateful allow-lists that can reference each other; NACLs are stateless subnet rules with allow and deny.
  • Peering is simple but non-transitive; Transit Gateway is the hub for many VPCs, VPNs and Direct Connect.
  • Debug with Flow Logs and Reachability Analyzer.