You are sitting in a DevOps interview, and the interviewer throws a classic curveball: “I have a server in a private subnet, but it can’t reach the internet to download a security patch. What’s wrong, and how do you fix it?”
Most junior candidates will immediately blurt out, “Just add an Internet Gateway!” But that is a rookie mistake that reveals a fundamental misunderstanding of VPC architecture. If you add an Internet Gateway (IGW) directly to a private subnet, you have just turned it into a public subnet, effectively stripping away the security layers that justified its existence in the first place.
This scenario, highlighted in a recent DevOps Shack technical session, is a litmus test for understanding the “plumbing” of AWS networking. Here is how you actually solve it like a Senior Engineer.
The Anatomy of Isolation
To understand why the private subnet is “blind” to the outside world, you have to look at the Route Table. In the AWS ecosystem, a subnet is defined as “private” not by a checkbox in the UI, but by the absence of a route to an Internet Gateway.
A standard private subnet typically contains resources like databases (RDS) or backend application servers (EC2) that should never be reachable from the public internet. However, these resources still need to “call home” for updates, OS patches, or to hit external APIs. Since they lack a Public IP and a direct path to the IGW, their packets have nowhere to go when they try to leave the VPC.
The Standard Fix: The NAT Gateway
The industry-standard solution is the NAT Gateway (Network Address Translation). The mechanism is simple but the implementation details are where people stumble.
First, you cannot put a NAT Gateway inside the private subnet itself. It would be just as isolated as the server you are trying to fix. You must place the NAT Gateway in a Public Subnet. This public subnet already has a Route Table entry pointing to the Internet Gateway (0.0.0.0/0 -> igw-xxxxxx).
Once the NAT Gateway is deployed, you perform a two-step configuration:
- Allocate an Elastic IP: The NAT Gateway requires a static, public IP address to communicate with the internet.
- Update the Private Route Table: You must go to the route table associated with your private subnet and add a route for
0.0.0.0/0(all outbound traffic) with the target set to the NAT Gateway ID (nat-xxxxxx).
Now, when your private EC2 instance sends a request, the VPC router sees the 0.0.0.0/0 rule, sends the packet to the NAT Gateway in the public subnet, which then “translates” the private IP to its own public Elastic IP and forwards it to the internet via the IGW.
The “Managed Tax” and Technical Trade-offs
While the NAT Gateway is the “correct” answer for 90% of interview questions, a seasoned practitioner knows it comes with a “managed tax.” AWS charges roughly $0.045 per hour just for the privilege of having the gateway exist, plus another $0.045 per GB of data processed. For high-throughput environments, this can quietly become the most expensive line item on your bill.
NAT Gateway vs. NAT Instance
Before 2015, we used NAT Instances regular EC2 instances running Linux and configured with IP masquerading. They are significantly cheaper because you only pay for the EC2 runtime. However, they are a massive operational burden. If that one EC2 instance crashes, your entire private network loses internet access. NAT Gateways, by contrast, are managed by AWS, scale automatically up to 100 Gbps, and offer built-in redundancy within an Availability Zone.
The Multi-AZ Trap
A common architectural flaw is deploying one NAT Gateway for a multi-AZ VPC. If that one Availability Zone goes down, your private subnets in other zones also lose internet connectivity. In production, you should ideally have one NAT Gateway per Availability Zone to ensure zone-independent architecture.
The Smarter Alternative: VPC Endpoints
If your private server only needs to reach other AWS services like S3 for storage or DynamoDB for data using a NAT Gateway is actually poor design. It forces your data to travel over the public internet only to come back into the AWS network.
Instead, use VPC Endpoints.
- Gateway Endpoints: These are free and work for S3 and DynamoDB. They update your route table to keep traffic entirely on the AWS private backbone.
- Interface Endpoints (PrivateLink): These cost a small hourly fee but provide a private IP inside your subnet for services like Secrets Manager, Kinesis, or even third-party SaaS apps.
Final Troubleshooting Checklist
If you have added the NAT Gateway and updated the route table, but it still isn’t working, check these three “silent killers”:
- Security Group Egress: Does the instance’s Security Group allow outbound traffic on port 80/443?
- NACLs (Network Access Control Lists): Since NACLs are stateless, did you remember to allow the “return” traffic on ephemeral ports?
- Source/Destination Check: If you are using an old-school NAT Instance instead of a NAT Gateway, you must disable the “Source/Destination Check” attribute on the EC2 instance, or it will drop any packet that doesn’t originate from its own IP.
In an interview, the “NAT Gateway in a public subnet” answer gets you the job. But mentioning cost optimization via VPC Endpoints and HA strategies via Multi-AZ NAT deployment is what gets you the “Senior” title.
