AWS Builder Center
How a NAT Gateway Lets Private Servers Reach the Internet

How a NAT Gateway Lets Private Servers Reach the Internet

How a NAT Gateway Lets Private Servers Reach the Internet (My First Time Setting It Up)

Student
Intro While learning AWS networking, I built a small VPC with a private subnet. My database server was safely hidden there, with no public IP. Then I hit a problem: I couldn’t even install a package on it. That is when I learned what a NAT gateway is for. Here is how I understand it now.
The problem A server in a private subnet has no route to the internet. That is good for security, but a server still needs to download updates, install packages and sometimes call outside APIs. [Add your own: for me it failed when I ran the install command on the MongoDB server.]
The idea behind NAT A NAT gateway works like a one-way door. The private server can send a request out to the internet and get the reply back, but nobody on the internet can start a connection to the private server. So the server can reach out, but it stays hidden.
How I set it up
  1. Created a NAT gateway inside the public subnet.
  1. Gave it an Elastic IP, a fixed public address.
  1. Edited the route table of the private subnet and added a route:  0.0.0.0/0  pointing to the NAT gateway.
  1. Checked that the public subnet’s route table still points to the Internet Gateway.
After that, my private server could download packages without having a public IP.
The part that confused me I first thought the NAT gateway should sit in the private subnet, next to my server. That is wrong. It must be in the public subnet, because the NAT gateway itself needs a route to the Internet Gateway to send traffic out. The private subnet only needs a route that points to the NAT gateway.
A quick way to remember it
  • Private server sends a request to the NAT gateway.
  • The NAT gateway sends it out through the Internet Gateway.
  • The reply comes back the same way.
Watch the cost A NAT gateway is billed by the hour while it exists, plus data processing charges. If you are only practising, delete it after you finish, and release the Elastic IP too.
Other options For a small project where the private server only needs to reach S3, a VPC endpoint can be cheaper than a NAT gateway. I want to try that next.
What I learned A NAT gateway is not complicated once you see the route table. The private subnet sends internet traffic to NAT, NAT sits in the public subnet, and the Internet Gateway does the rest.
If you’ve set up NAT before, what mistakes did you make? Tell me in the comments.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article