Created
August 4, 2021 12:18
-
-
Save NSLog0/f220d61762f5c4895a5a2bd46cde6462 to your computer and use it in GitHub Desktop.
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 | |
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