Вы можете использовать библиотеку sshtunnel
.
Например:
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
('XX.XXX.XX.XX', 22),
ssh_username='ec2-user',
ssh_pkey='~/.ssh/bastion_key.pem',
remote_bind_address=('xxxxxx.rds.amazonaws.com', 3306),
local_bind_address=('0.0.0.0', 3307)
) as tunnel:
# do stuff with tunnel