Skip to main content

Exposing a private Amazon RDS instance with iptables NAT rules

I needed to expose a private Amazon MySQL RDS instance to a 3rd party SaaS tool. I tried several approaches and finally found one that seemed to work pretty well.

I ended up creating a small EC2 instance in the same VPC as the RDS instance, and applied these iptables NAT/masquerading rules to it, mapping local port 3307 to port 3306 on the RDS instance, whose internal IP address is in this case 172.16.11.2.

# cat iptables_tunnel_port_3307.sh
#!/bin/bash

iptables -F
iptables -F -t nat
iptables -X
iptables -t nat -A PREROUTING -p tcp --dport 3307 -j DNAT --to 172.16.11.2:3306
iptables -A FORWARD -p tcp -d 172.16.11.2 --dport 3306 -j ACCEPT
iptables -t nat -A OUTPUT -p tcp -o lo --dport 3307 -j DNAT --to 172.16.11.2:3306
iptables -t nat -A POSTROUTING  -j MASQUERADE

I also had to enable IP forwarding on the EC2 instance:

# sysctl net.ipv4.ip_forward
# sysctl -p

At this point, I was able to hit the external IP of the EC2 instance on port 3307, and get to the private RDS instance on port 3306. I was also able to attach the EC2 instance to an EC2 Security Group allowing the 3rd party SaaS tool IP addresses to access port 3307 on the EC2 instance.

My thanks to the people discussing a similar issue on this thread of LinuxQuestions. Without their discussion, I don't think I'd have been able to figure out a solution.

Comments

Popular posts from this blog

Google loosens grip on Android One partners in attempt to revitalize the program

Bloomberg Android One never really got anywhere, but Google isn’t giving up. Since its launch back at Google I/O 2014 , the low-cost Android One initiative was never the saving grace we had all once hoped it would be. The program promised solid Android-powered hardware, timely software updates straight from Google, a simple and easy to use software experience, and even more ways to save on high data costs . As it turns out, not many people were interested in these One devices, as the lower-cost competition began heating up in these countries, often offering users better devices for the money. Now that it’s been awhile since we’ve seen any new Android One hardware, what’s next for the program? According to  The Wall Street Journal , Google is taking another crack at it. Sourcing “people familiar with the matter”, this new report claims that Google plans to relax its rules and give Android One partners more options when it comes to choosing features and price. Google is being ...

ARM Announces a New Low-Power Cortex-A35 CPU

ARM has just announced a brand new Cortex-A35 CPU that is said to be low power and ultra-high efficiency. This chip is said to be perfect for wearables, embedded platforms and even low-end smartphones/tablets. When compared to the Cortex-A53 CPU, the new A35 chip has a 25% smaller core, uses 32% less power and is 25% more efficient.