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
Does the private route table actually have the NAT route, and is the subnet associated with that table?
Is the NAT Gateway in a public subnet? A NAT in a private subnet is a classic mistake.
Does the public subnet route to the IGW?
Do security groups and NACLs allow outbound HTTPS and the return traffic?
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.
EC2 in private subnet: Has only a private IP like 10.0.11.25.
Private route table: 0.0.0.0/0 points to the NAT Gateway.
NAT Gateway in public subnet: Swaps the source IP for its Elastic IP.
Internet Gateway: Connects the public subnet to the internet.
Internet (e.g. package mirror): Sees only the NAT Elastic IP.