Skip to content

Instantly share code, notes, and snippets.

@NSLog0
Created August 4, 2021 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NSLog0/f220d61762f5c4895a5a2bd46cde6462 to your computer and use it in GitHub Desktop.
Save NSLog0/f220d61762f5c4895a5a2bd46cde6462 to your computer and use it in GitHub Desktop.
from sshtunnel import SSHTunnelForwarder
import pymongo
EC2 = "<IP>"
MONGO_DB = "test_database"
MONGO_COLLECTION = "post"
# define ssh tunnel
server = SSHTunnelForwarder(
EC2,
ssh_username="ec2-user",
ssh_pkey='./MONGO_TUNNEL.pem',
remote_bind_address=('127.0.0.1', 27017)
)
# start ssh tunnel
server.start()
connection = pymongo.MongoClient(
'mongodb://<username>:<password>@127.0.0.1:27017/?ssl=true&ssl_ca_certs=./rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
db = connection[MONGO_DB]
collection = db[MONGO_COLLECTION]
data = collection.insert_one({"text": "test"})
print(data)
# close ssh tunnel
server.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment