Created
November 15, 2019 21:41
-
-
Save gerardev/0170843b04baa62e99d4bd60bbcccc04 to your computer and use it in GitHub Desktop.
Connect to AWS Document DB using SSH Tunnel in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment