from sshtunnel import SSHTunnelForwarder from pymongo import MongoClient ssh_tunnel_host = 'our-ec2-hostname.compute-1.amazonaws.com' ssh_tunnel_port = 22 ssh_tunnel_user = 'ubuntu' ssh_tunnel_pkey = 'ec2-hostname.pem' db_user = 'database-user' db_pass = 'database-pass' db_host = 'documentdb-cluster-endpoint' db_port = 27017 db_pkey = 'rds-ca-2019-us-east-1.pem' server = SSHTunnelForwarder( (ssh_tunnel_host, 22), ssh_username=ssh_tunnel_user, ssh_pkey=ssh_tunnel_pkey, remote_bind_address=(db_host, 27017), local_bind_address=('127.0.0.1', 27017) ) server.start() db_uri = f'mongodb://{db_user}:{db_pass}@127.0.0.1:{db_port}' client = MongoClient(db_uri) database = client['test']