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

A Google Engineer is reviewing all USB Type-C chargers on Amazon to inform customers which ones are safe for use on their devices

The USB Type-C take over has already started, and as this year comes to an end, we expect to see a few more smartphones and tablets hit the market bundling compatibility for the third-generation connection. As it stands, handsets that currently feature Type-C ports only ship with a single cable and that annoyingly doesn’t support computer connectivity, so if users want to transfer files to their devices, they have to purchase an additional wire or adapter. However, these accessories aren’t cheap, so customers veer towards the more affordable, third-party cables readily available on marketplaces, such as Amazon and eBay, but that’s not a good thing. Many consumers have reported that these chargers have caused harm to their handsets, so Google Engineer Benson Leung has taken it upon himself to purchase a selection of Type-C connectors from Amazon to test and see if they meet the standard specifications for use on his Nexus device. So far, Leung has been through seven cables and clai...