flaskaws-documentdbflask-pymongo

Flask with AWS DocumentDB


I am trying to connect to my AWS DocumentDB using flask and flask_pymongo. TLS is enabled for my AWS cluster. I am able to connect to the DB using the python shell and just pymongo but when I do the below in flask I can't get it to connect. The URI below is customized in my app to my username, password and cluster. And the pem file is in the same directory as app.py. Any suggestions out there?

from flask import Flask, jsonify
from flask_restful import Resource, Api
from flask_pymongo import PyMongo

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'mydb'
app.config['MONGO_URI'] = 'mongodb://<dbusername>:<dbpassword>@mycluster.node.us-east-1.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred'

mongo = PyMongo(app)
api = Api(app)

Solution

  • If you're using a recent version of Flask-PyMongo (2.0 or later), then MONGO_DBNAME will have no effect. With 2.0, you must put the db name in the URI (so it would be 'mongodb://<dbusername>:<dbpassword>@mycluster.node.us-east-1.docdb.amazonaws.com:27017/mydb?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred' in this case.