I am stuck with first time deployment of flask application on Elastic Beanstalk. It fails establishing connection to mongodb with a error "mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection".
Details
Application uses flask, Connexion for API config, mongoengine as mongodb glue.
connexion==1.1.15
Flask==1.0.1
mongoengine==0.15.0
pymongo==3.6.1
PyJWT==1.6.1
Example shown here was deployed on Python 3.4 (but app runs on Python 3.6 as well locally as well on the Elastic Beanstalk with same set of problems)
Same application works fine in local environment or on EC2 (Amazon Linux AMI) when run as a python application (with Apache ProxyPass config).
Here are the relevant code:
__init__.py
import logging.config
import connexion
import mongoengine as me
from api_server import encoder
app = connexion.App(__name__, specification_dir='./config/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('api_config.yaml', arguments={'title': 'My App'})
logging.config.fileConfig("./config/logging.conf")
# set the WSGI application callable to allow using uWSGI:
# uwsgi --http :8080 -w app
def main():
db = me.connect(alias="default", host="mongodb+srv://*[DB URL]*
if __name__ == '__main__':
main()
application.py
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/opt/python/current/app/')
from api_server import app as application
Directory Structure
Base Dir(Elastic Benstalk Root dir)
-- application.py
-- api_server
-----__init__.py
-----config
--------app_config.ymal
wsgi.conf apache mod_wsgi config created by ElasticBeanstalk utility on server:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /opt/python/run/baselinenv
WSGISocketPrefix run/wsgi
WSGIRestrictEmbedded On
<VirtualHost *:80>
Alias /static/ /opt/python/current/app/static/
<Directory /opt/python/current/app/static/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /opt/python/current/app/application.py
<Directory /opt/python/current/app/>
Require all granted
</Directory>
WSGIDaemonProcess wsgi processes=1 threads=15 display-name=%{GROUP} \
python-home=/opt/python/run/venv/ \
python-path=/opt/python/current/app:/opt/python/run/venv/lib64/python3.4/site-packages:/opt/python/run/venv/lib/python3.4/site-packages user=wsgi group=wsgi \
home=/opt/python/current/app
WSGIProcessGroup wsgi
</VirtualHost>
LogFormat "%h (%{X-Forwarded-For}i) %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
Application initializes and basic request response is served, but only when it requires to connect to mongodb, it throws following error:
[Sun May 27 09:37:30.839005 2018] [:error] [pid 16181] Traceback (most recent call last):
[Sun May 27 09:37:30.839064 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 2292, in wsgi_app
[Sun May 27 09:37:30.839125 2018] [:error] [pid 16181] response = self.full_dispatch_request()
[Sun May 27 09:37:30.839174 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1815, in full_dispatch_request
[Sun May 27 09:37:30.839227 2018] [:error] [pid 16181] rv = self.handle_user_exception(e)
[Sun May 27 09:37:30.839276 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1718, in handle_user_exception
[Sun May 27 09:37:30.839325 2018] [:error] [pid 16181] reraise(exc_type, exc_value, tb)
[Sun May 27 09:37:30.839378 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/_compat.py", line 35, in reraise
[Sun May 27 09:37:30.839426 2018] [:error] [pid 16181] raise value
[Sun May 27 09:37:30.839472 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1813, in full_dispatch_request
[Sun May 27 09:37:30.839513 2018] [:error] [pid 16181] rv = self.dispatch_request()
[Sun May 27 09:37:30.839544 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1799, in dispatch_request
[Sun May 27 09:37:30.839574 2018] [:error] [pid 16181] return self.view_functions[rule.endpoint](**req.view_args)
[Sun May 27 09:37:30.839604 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/decorator.py", line 66, in wrapper
[Sun May 27 09:37:30.839633 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839661 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/validation.py", line 122, in wrapper
[Sun May 27 09:37:30.839708 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839739 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/validation.py", line 293, in wrapper
[Sun May 27 09:37:30.839769 2018] [:error] [pid 16181] return function(request)
[Sun May 27 09:37:30.839798 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/decorator.py", line 42, in wrapper
[Sun May 27 09:37:30.839827 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839856 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/parameter.py", line 218, in wrapper
[Sun May 27 09:37:30.839885 2018] [:error] [pid 16181] return function(**kwargs)
[Sun May 27 09:37:30.839913 2018] [:error] [pid 16181] File "/opt/python/current/app/api_server/auth/auth_service.py", line 21, in wrapper
[Sun May 27 09:37:30.839942 2018] [:error] [pid 16181] queryObj= CampaignOwner.objects(campaignOwnerId=g.campaign_owner_id)
[Sun May 27 09:37:30.839971 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
[Sun May 27 09:37:30.840000 2018] [:error] [pid 16181] queryset = queryset_class(owner, owner._get_collection())
[Sun May 27 09:37:30.840029 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/document.py", line 191, in _get_collection
[Sun May 27 09:37:30.840058 2018] [:error] [pid 16181] db = cls._get_db()
[Sun May 27 09:37:30.840086 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/document.py", line 180, in _get_db
[Sun May 27 09:37:30.840115 2018] [:error] [pid 16181] return get_db(cls._meta.get('db_alias', DEFAULT_CONNECTION_NAME))
[Sun May 27 09:37:30.840144 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/connection.py", line 226, in get_db
[Sun May 27 09:37:30.840173 2018] [:error] [pid 16181] conn = get_connection(alias)
[Sun May 27 09:37:30.840201 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/connection.py", line 146, in get_connection
[Sun May 27 09:37:30.840230 2018] [:error] [pid 16181] raise MongoEngineConnectionError(msg)
[Sun May 27 09:37:30.840261 2018] [:error] [pid 16181] mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection
Need help in figuring out what is going wrong. 1. Is it anything missing in WSGI configuration? 2. Tried using flask-mongoengine, app.config["MONGODB_SETTING"] with same result, is there any other way I should set DB setting? 3. How mongo engine/pymongo reads default setting 4. Is there any other problem in the way I have written the startup code which is leading to this.
Please help with whatever information you can. Tried to look up all possible information so far, unable to rectify the issue.