There's already tons of help and guide on how to do this. But for some reason I can't get it working and am not sure how to troubleshoot it.
I've got an RDS postgres instance with the private IP 10.0.122.220
. I also have a bastion host with a (yes) private IP 10.0.94.67
. I'm able to connect to ports on the bastion host, but not the RDS. So I'm trying to forward port 5432
of the bastion host to port 5432
of the RDS instance.
This is the status of the bastion host:
bastion$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP group default qlen 1000
inet 10.0.94.67/19 brd 10.0.95.255 scope global dynamic eth0
...
bastion$ ip route show | grep default
default via 10.0.64.1 dev eth0
bastion$ cat /proc/sys/net/ipv4/ip_forward
1
Then I added two NAT rules:
bastion# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 5432 -j DNAT --to-destination 10.0.122.220
bastion# iptables -t nat -A POSTROUTING -o eth0 -p tcp --dport 5432 -d 10.0.122.220 -j SNAT --to-source 10.0.94.67
bastion# iptables -v -t nat -L -n
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DNAT tcp -- eth0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:5432 to:10.0.122.220
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 443 packets, 32660 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 443 packets, 32660 bytes)
pkts bytes target prot opt in out source destination
0 0 SNAT tcp -- * eth0 0.0.0.0/0 10.0.122.220 tcp dpt:5432 to:10.0.94.67
But I still can't connect to the RDS instance with the help of SSH tunneling:
my-machine$ ssh -v -NL 5432:10.0.94.67:5432 -i my-key ec2-user@10.0.94.67
debug1: Connection to port 5432 forwarding to 10.0.94.67 port 5432 requested.
debug1: channel 2: new [direct-tcpip]
channel 2: open failed: connect failed: Connection refused
debug1: channel 2: free: direct-tcpip: listening port 5432 for 10.0.94.67 port 5432, connect from 127.0.0.1 port 57447 to 127.0.0.1 port 5432, nchannels 3
debug1: Connection to port 5432 forwarding to 10.0.94.67 port 5432 requested.
debug1: channel 2: new [direct-tcpip]
channel 2: open failed: connect failed: Connection refused
debug1: channel 2: free: direct-tcpip: listening port 5432 for 10.0.94.67 port 5432, connect from 127.0.0.1 port 57448 to 127.0.0.1 port 5432, nchannels 3
... keeps repeating the above
What I can confirm is that, RDS is up, running, and responding, and bastion host has access to it, since with the following SSH tunnle I can connect to the database:
my-machine$ ssh -v -NL 5432:10.0.122.220:5432 -i my-key ec2-user@10.0.94.67
What did I miss? How can I troubleshoot it? Thanks.