<
Sshh... This AWS Hack, Cut your 89% Cost on NAT Gateway!

Sshh... This AWS Hack, Cut your 89% Cost on NAT Gateway!


If you're using AWS Lambda and got issue on NAT Gateway costs, you're not alone. In this blog post, I'm going to share my experience replacing NAT Gateway with NAT Instance EC2 instead.

First, let's start with what NAT Gateway is. NAT Gateway is a managed network service provided by AWS that enables instances in a private subnet to access the internet or other AWS services, but prevents the internet or other AWS services from initiating connections with those instances. NAT Gateway is a great solution if you're looking for a fully managed service and don't want to worry about maintenance or scaling.

Pricing Strategy

Let us pick an example in Singapore (ap-southeast-1) region. NAT Gateway pricing is $0.059/ hour. EC2 t4g.nano is $0.0053/ hour. See what's impacted? It will reduce your 89.3% cost instantly. Not only this cost, how about data processing? It'll cost you $0.045/ GB.

But how we use EC2 as NAT Gateway?

Step by Step Create NAT Instance with EC2

Back on many years ago, AWS had the image to build this directly. But today, they have removed it. Probably because this is detrimental to the NAT Gateway product. I don't know the details, I just only guess :')

  1. Create EC2 on Public Subnet with Internet Gateway & using image Amazon Linux 2 AMI
  2. Setup Route Table on Private 0.0.0.0/0 to EC2 server
  3. Setup Security Group on EC2, allow all traffic
  4. Disable Source/Destination check on EC2
  5. Open EC2 instance and run this commands:
sysctl -w net.ipv4.ip_forward=1
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Conclusions NAT Instance vs NAT Gateway

Pro NAT Instance:

  • Cost-effective
  • More control over the setup & maintenance
  • Can more save it with RI / Spot / Automatically shut-down in the test environment

Pro NAT Gateway:

  • Not requires more technical expertise
  • No need maintenance like a patch and update instance
  • More effective on high traffic