How to set up connection to AWS rds via ssh to server from python?

1.2k views Asked by At

I have to access an aws rds from a remote server. So far I have had no problems accessing the mysql databases from the command line.

I ssh to the server like: ssh [email protected]

Once I'm in the server I use mysql -hrds_host.amazonaws.com -uuser -ppassword

And then I am able to call sql queries for the different databases from my terminal.

I have been trying to set up this connection through python using an ssh tunnel but fail to do so successfully. I expect the code to look something like:

with SSHTunnelForwarder(
          (server_host, 22),
          ssh_username=username,
          ssh_private_key=private_key,
          remote_bind_address=(rds_host, 3306)
     ) as server:
          conn = sql.connect(host=rds_host,
          port=server.local_bind_port,
          user=user,
          passwd=password,
          db=database)

I continuously get errors like: Could not establish session to SSH gateway or An error occurred while opening tunnels.

The logging level of my SSHTunnelForwardes always shows an error.

I have also been successful setting up a connection with a paramiko ssh.client, just not the tunnel to call queries.

1

There are 1 answers

0
pradipta Dash On
conn = sql.connect(host=rds_host,
          port=server.local_bind_port,
          user=user,

Change this connection to localhost, it should work