Miserlou / Zappa

Serverless Python
https://blog.zappa.io/
MIT License
11.89k stars 1.2k forks source link

Django working fine on desktop venv, {'message': 'An uncaught exception happened'} on AWS (Django/Zappa) #1034

Closed johncarmack1984 closed 7 years ago

johncarmack1984 commented 7 years ago

Expected Behavior

Site should load home app template upon request, as it works fine with $python manage.py runserver on desktop

Actual Behavior

AWS returns this error

"{'message': 'An uncaught exception happened while servicing this request. You can investigate this with the `zappa tail` command.', 'traceback': ['Traceback (most recent call last):\\n', '  File \"/var/task/handler.py\", line 434, in handler\\n    response = Response.from_app(self.wsgi_app, environ)\\n', '  File \"/var/task/werkzeug/wrappers.py\", line 903, in from_app\\n    return cls(*_run_wsgi_app(app, environ, buffered))\\n', '  File \"/var/task/werkzeug/wrappers.py\", line 57, in _run_wsgi_app\\n    return _run_wsgi_app(*args)\\n', '  File \"/var/task/werkzeug/test.py\", line 884, in run_wsgi_app\\n    app_rv = app(environ, start_response)\\n', \"TypeError: 'NoneType' object is not callable\\n\"]}"

zappa tail output:

(venv) $ zappa tail
Calling tail for stage dev..
[1501827217722] Instancing..
[1501827219222] No module named 'home': ModuleNotFoundError
Traceback (most recent call last):
  File "/var/task/handler.py", line 491, in lambda_handler
  return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 240, in lambda_handler
  handler = cls()
  File "/var/task/handler.py", line 143, in __init__
  wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
  File "/var/task/zappa/ext/django_zappa.py", line 20, in get_django_wsgi
  return get_wsgi_application()
  File "/var/task/django/core/wsgi.py", line 13, in get_wsgi_application
  django.setup(set_prefix=False)
  File "/var/task/django/__init__.py", line 27, in setup
  apps.populate(settings.INSTALLED_APPS)
  File "/var/task/django/apps/registry.py", line 85, in populate
  app_config = AppConfig.create(entry)
  File "/var/task/django/apps/config.py", line 94, in create
  module = import_module(entry)
  File "/var/lang/lib/python3.6/importlib/__init__.py", line 126, in import_module
  return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'home'
[1501827230376] [DEBUG] 2017-08-04T06:13:50.376Z 154bd9dc-78dc-11e7-8dcc-d72c37cfcc4a Zappa Event: {'resource': '/', 'path': '/', 'httpMethod': 'GET', 'headers': {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-us', 'CloudFront-Forwarded-Proto': 'https', 'CloudFront-Is-Desktop-Viewer': 'true', 'CloudFront-Is-Mobile-Viewer': 'false', 'CloudFront-Is-SmartTV-Viewer': 'false', 'CloudFront-Is-Tablet-Viewer': 'false', 'CloudFront-Viewer-Country': 'US', 'dnt': '1', 'Host': '7rbrf1etu7.execute-api.us-east-1.amazonaws.com', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8', 'Via': '2.0 c157dc52e9f3c011b626279fe96418f8.cloudfront.net (CloudFront)', 'X-Amz-Cf-Id': 'USlEEC2I6F3aPzbjc9yvuZZXiecLscbZHSJEgjz0mf4wgU62NkVoVA==', 'X-Amzn-Trace-Id': 'Root=1-5984109e-1dc01175046fb73236b5fedd', 'X-Forwarded-For': '24.155.240.73, 216.137.42.52', 'X-Forwarded-Port': '443', 'X-Forwarded-Proto': 'https'}, 'queryStringParameters': None, 'pathParameters': None, 'stageVariables': None, 'requestContext': {'path': '/dev', 'accountId': '735853783919', 'resourceId': 'f1dbiq2lo3', 'stage': 'dev', 'requestId': '153a26a9-78dc-11e7-b666-53368499803b', 'identity': {'cognitoIdentityPoolId': None, 'accountId': None, 'cognitoIdentityId': None, 'caller': None, 'apiKey': '', 'sourceIp': '24.155.240.73', 'accessKey': None, 'cognitoAuthenticationType': None, 'cognitoAuthenticationProvider': None, 'userArn': None, 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8', 'user': None}, 'resourcePath': '/', 'httpMethod': 'GET', 'apiId': '7rbrf1etu7'}, 'body': None, 'isBase64Encoded': False}
[1501827230381] 'NoneType' object is not callable
[1501827246201] [DEBUG] 2017-08-04T06:14:06.201Z 1ea446a3-78dc-11e7-85eb-4de25bf53ec6 Zappa Event: {'version': '0', 'id': '40b44dee-ec8d-4009-8c2b-b94ae5e1d3f4', 'detail-type': 'Scheduled Event', 'source': 'aws.events', 'account': '735853783919', 'time': '2017-08-04T06:13:21Z', 'region': 'us-east-1', 'resources': ['arn:aws:events:us-east-1:735853783919:rule/django-dev-zappa-keep-warm-handler.keep_warm_callback'], 'detail': {}}
[1501827246202] [DEBUG] 2017-08-04T06:14:06.202Z 1ea446a3-78dc-11e7-85eb-4de25bf53ec6 Zappa Event: {}
[1501827484296] [DEBUG] 2017-08-04T06:18:04.295Z ac8efec3-78dc-11e7-ac8b-173dbb88a801 Zappa Event: {'version': '0', 'id': '22e17a6f-744a-4b24-bf1b-63a207e8ecf8', 'detail-type': 'Scheduled Event', 'source': 'aws.events', 'account': '735853783919', 'time': '2017-08-04T06:17:21Z', 'region': 'us-east-1', 'resources': ['arn:aws:events:us-east-1:735853783919:rule/django-dev-zappa-keep-warm-handler.keep_warm_callback'], 'detail': {}}
[1501827484296] [DEBUG] 2017-08-04T06:18:04.295Z ac8efec3-78dc-11e7-ac8b-173dbb88a801 Zappa Event: {}
[1501827724042] [DEBUG] 2017-08-04T06:22:04.41Z 3b757bc7-78dd-11e7-8a9e-f3c9354d1a17 Zappa Event: {'version': '0', 'id': '3137bf67-ef60-4c50-87c0-4721e2194386', 'detail-type': 'Scheduled Event', 'source': 'aws.events', 'account': '735853783919', 'time': '2017-08-04T06:21:21Z', 'region': 'us-east-1', 'resources': ['arn:aws:events:us-east-1:735853783919:rule/django-dev-zappa-keep-warm-handler.keep_warm_callback'], 'detail': {}}
[1501827724042] [DEBUG] 2017-08-04T06:22:04.42Z 3b757bc7-78dd-11e7-8a9e-f3c9354d1a17 Zappa Event: {}
Oh no! An error occurred! :(

==============

Traceback (most recent call last):
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/zappa/cli.py", line 2489, in handle
    sys.exit(cli.handle())
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/zappa/cli.py", line 473, in handle
    self.dispatch_command(self.command, stage)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/zappa/cli.py", line 560, in dispatch_command
    filter_pattern=self.vargs['filter'],
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/zappa/cli.py", line 972, in tail
    filter_pattern=filter_pattern,
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/zappa/core.py", line 2249, in fetch_logs
    orderBy='LastEventTime'
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/client.py", line 253, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/client.py", line 544, in _make_api_call
    operation_model, request_dict)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/endpoint.py", line 141, in make_request
    return self._send_request(request_dict, operation_model)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/endpoint.py", line 170, in _send_request
    success_response, exception):
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/endpoint.py", line 249, in _needs_retry
    caught_exception=caught_exception, request_dict=request_dict)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/hooks.py", line 227, in emit
    return self._emit(event_name, kwargs)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/hooks.py", line 210, in _emit
    response = handler(**kwargs)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/retryhandler.py", line 183, in __call__
    if self._checker(attempts, response, caught_exception):
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/retryhandler.py", line 251, in __call__
    caught_exception)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/retryhandler.py", line 277, in _should_retry
    return self._checker(attempt_number, response, caught_exception)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/retryhandler.py", line 317, in __call__
    caught_exception)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/retryhandler.py", line 223, in __call__
    attempt_number, caught_exception)
  File "/Users/johncarmack/Dropbox/sites/django/venv/lib/python3.6/site-packages/botocore/retryhandler.py", line 359, in _check_caught_exception
    raise caught_exception
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://logs.us-east-1.amazonaws.com/"

==============

Need help? Found a bug? Let us know! :D
File bug reports on GitHub here: https://github.com/Miserlou/Zappa
And join our Slack channel here: https://slack.zappa.io
Love!,
 ~ Team Zappa!

Possible Fix

??? No clue.

Steps to Reproduce

Launch and update what amounts to a "hello world" to AWS from Zappa from macOS Sierra... I don't know what I could have done differently.

Your Environment

Python 3.6 Django 1.11 Zappa 0.43.1 macOS 10.12.6 (Sierra)

Output of pip freeze:

argcomplete==1.8.2
base58==0.2.4
boto3==1.4.4
botocore==1.5.40
certifi==2017.7.27.1
chardet==3.0.4
click==6.7
Django==1.11.4
docutils==0.14
durationpy==0.4
future==0.16.0
futures==3.1.1
hjson==2.0.7
idna==2.5
jmespath==0.9.3
kappa==0.6.0
lambda-packages==0.16.1
placebo==0.8.1
python-dateutil==2.6.1
python-slugify==1.2.4
pytz==2017.2
PyYAML==3.12
requests==2.18.3
s3transfer==0.1.10
six==1.10.0
toml==0.9.2
tqdm==4.14.0
troposphere==1.9.5
Unidecode==0.4.21
urllib3==1.22
Werkzeug==0.12
wsgi-request-logger==0.4.6
zappa==0.43.1

Project is here: link

Content of zappa_settings.json:

{
    "dev": {
        "aws_region": "us-east-1",
        "django_settings": "stockpicker.stockpicker.settings",
        "profile_name": "default",
        "s3_bucket": "zappa-rrkhhyowh"
    }
}
johncarmack1984 commented 7 years ago

That ModuleNotFound error says that there is no module 'home' which is the folder for the one app in the project. The folder is in fact there, and again, this works fine on desktop. :-/

johncarmack1984 commented 7 years ago

Okay here's my code in urls.py that seems to be the offending part. Works fine on desktop, shoots up an error on Amazon.

from django.conf.urls import include, url
from django.contrib import admin
from home import views

urlpatterns = [
    url(r'^$',views.index,name='index'),
    url(r'^admin/', admin.site.urls),
]

I have tried modifying the import statement to say "from . import views" but that leads to an error on desktop and does not fix the issue on Amazon.

Somewhere between my desktop venv and AWS, the interpreter is not understanding that the home folder is a module inside of the django project.

johncarmack1984 commented 7 years ago

This was resolved by using a custom storage backend as recommended here: https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/