VPC Fundamentals

Difficulty: Advanced

Question

An EC2 instance in a private subnet needs to download packages from the internet. Which VPC components do you need and how does the traffic flow?

Answer

You need an Internet Gateway attached to the VPC, a NAT Gateway placed in a public subnet with an Elastic IP, a public subnet route table that sends 0.0.0.0/0 to the Internet Gateway, and a private subnet route table that sends 0.0.0.0/0 to the NAT Gateway. The instance can then start outbound connections, but nothing on the internet can start a connection to it.

What makes a subnet public or private

A subnet is not public because of a checkbox. It is public only if its route table has a route to an Internet Gateway. Instances in it also need a public IPv4 address (or Elastic IP) to be reachable. A private subnet has no route to the Internet Gateway at all.

Component by component

  • VPC: your isolated network with a CIDR block like 10.0.0.0/16.
  • Subnets: each lives in one AZ, for example 10.0.1.0/24 public and 10.0.11.0/24 private.
  • Internet Gateway (IGW): horizontally scaled, highly available gateway attached to the VPC. One per VPC.
  • NAT Gateway: a managed service that rewrites the private instance's source IP to its own Elastic IP, then forwards traffic to the IGW. It is AZ-scoped.
  • Route tables: the public one has 0.0.0.0/0 -> igw-xxxx; the private one has 0.0.0.0/0 -> nat-xxxx. Both keep the implicit local route for traffic inside the VPC.
  • Security groups and NACLs: the instance's security group must allow the outbound traffic (default security groups allow all outbound). If you use custom NACLs, allow return traffic on ephemeral ports 1024-65535.

Making it highly available and cheaper

  • A NAT Gateway lives in one AZ. If that AZ fails, private instances in other AZs lose internet access. For production, create one NAT Gateway per AZ and give each AZ's private route table its own NAT.
  • NAT Gateways charge per hour and per GB processed. Heavy traffic to S3 or DynamoDB should go through gateway VPC endpoints, which are free and keep traffic on the AWS network.
  • For other AWS services (ECR, Secrets Manager, CloudWatch Logs), interface endpoints (PrivateLink) avoid the NAT path, but they are billed per hour per AZ, so compare costs.
  • For IPv6, use an egress-only internet gateway instead of NAT.
  • A NAT instance (a small EC2 doing NAT) is cheaper for dev environments but you manage patching, scaling and failover yourself, and must disable the source/destination check.

Troubleshooting checklist

  1. Does the private route table actually have the NAT route, and is the subnet associated with that table?
  2. Is the NAT Gateway in a public subnet? A NAT in a private subnet is a classic mistake.
  3. Does the public subnet route to the IGW?
  4. Do security groups and NACLs allow outbound HTTPS and the return traffic?
  5. Is DNS resolution enabled on the VPC (enableDnsSupport)?

VPC Reachability Analyzer can check the path between two points for you.

Outbound path from a private instance to the internet. Traffic leaves through NAT and the Internet Gateway; replies return the same way, while inbound connections are impossible.
  1. EC2 in private subnet: Has only a private IP like 10.0.11.25.
  2. Private route table: 0.0.0.0/0 points to the NAT Gateway.
  3. NAT Gateway in public subnet: Swaps the source IP for its Elastic IP.
  4. Internet Gateway: Connects the public subnet to the internet.
  5. Internet (e.g. package mirror): Sees only the NAT Elastic IP.

Code examples

Wire up IGW, NAT Gateway and routes with the CLI

VPC=vpc-0abc; PUB=subnet-0pub1; PRIV=subnet-0priv1

# 1. Internet Gateway attached to the VPC
IGW=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id $IGW --vpc-id $VPC

# 2. Public route table: default route to IGW
PUB_RT=$(aws ec2 create-route-table --vpc-id $VPC --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $PUB_RT --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB

# 3. NAT Gateway in the PUBLIC subnet with an Elastic IP
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 wait nat-gateway-available --nat-gateway-ids $NAT

# 4. Private route table: default route to NAT
PRIV_RT=$(aws ec2 create-route-table --vpc-id $VPC --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $PRIV_RT --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $PRIV

In real projects you would do this in Terraform or CloudFormation, but the order of steps is the same and is worth knowing for interviews.

Free S3 gateway endpoint to bypass the NAT

aws ec2 create-vpc-endpoint \
  --vpc-id vpc-0abc \
  --vpc-endpoint-type Gateway \
  --service-name com.amazonaws.ap-south-1.s3 \
  --route-table-ids rtb-0priv1

This adds a prefix-list route for S3 to the private route table, so S3 traffic stops paying NAT data processing charges.

Key points

  • A subnet is public only if its route table points 0.0.0.0/0 at an Internet Gateway.
  • The NAT Gateway must sit in a public subnet and needs an Elastic IP (for public NAT).
  • NAT Gateways are AZ-scoped; use one per AZ for high availability.
  • Gateway endpoints for S3 and DynamoDB are free and cut NAT data charges.
  • NAT allows outbound-initiated traffic only; the internet cannot open connections to private instances.

Common follow-up questions

  • What is the difference between a NAT Gateway and a NAT instance?
  • How would you let private instances reach S3 without going through the internet?
  • How does an egress-only internet gateway work for IPv6?
  • How would you debug a private instance that cannot reach the internet?

Interview tips

  • Walk the packet path out loud: instance, route table, NAT, IGW, internet. It shows real understanding.
  • Point out the one-NAT-per-AZ design and NAT cost; these separate juniors from people who have run production.
  • Mention VPC endpoints as the cost and security improvement for AWS service traffic.

Concepts covered

VPC, Public and private subnets, Internet Gateway, NAT Gateway, Route tables, VPC endpoints

More AWS & Cloud Fundamentals questions