3

I'm running into an error after deploying my django app to lambda using zappa.

This is the 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):', ' File /var/task/handler.py, line 540, in handler with Response.from_app(self.wsgi_app, environ) as response:', ' File /var/task/werkzeug/wrappers/base_response.py, line 287, in from_app return cls(*_run_wsgi_app(app, environ, buffered))', ' File /var/task/werkzeug/wrappers/base_response.py, line 26, in _run_wsgi_app return _run_wsgi_app(*args)', ' File /var/task/werkzeug/test.py, line 1119, in run_wsgi_app app_rv = app(environ, start_response)', TypeError: 'NoneType' object is not callable]}

These are my zappa settings:

{
  "production": {
    "aws_region": "eu-west-2",
    "django_settings": "app.settings",
    "profile_name": "deployment",
    "project_name": "app",
    "runtime": "python3.6",
    "s3_bucket": "zappa-deployment-uploads",
    "slim_handler": true,
    "exclude": [".ebextensions/", ".elasticbeanstalk/", "webpack/", "app/static/"],
    "vpc_config" : {
    "SubnetIds": [ "..."],
    "SecurityGroupIds": ["..."]
  }
}

I've ensured that my API Gateway hostname has been added to my allowed hosts setting and redeployed.

I've called zappa tail, but it gives me an even shorter error respons "'NoneType' object is not callable".

Can anyone understand why I would be getting this?

goose
  • 2,502
  • 6
  • 42
  • 69

2 Answers2

3

You have an exception being thrown in your code. I have found NoneType is a generic error when using AWS Lambda & Zappa that is masking the true issue.

Some debugging steps:

  1. Ensure your code works locally when you run python manage.py runserver
  2. Output console messages in various places in your code and run zappa tail to identify the section of code that is having troubles
  3. Repeat above option until you have narrowed the location of the problem.
Edgar
  • 1,174
  • 8
  • 9
  • Thanks Edgar. I’m following your advice and sure enough it’s stopped working on local. Will come back here and let you know how I get on soon. – goose May 11 '21 at 11:05
  • 1
    Frustratingly, even after fixing this on local I'm still getting the same error message after redeploying to Lambda. – goose May 13 '21 at 20:19
  • @goose do you get any console messages successfully printed out before the error occurs? – Edgar May 16 '21 at 19:12
  • Hi @Edgar - I've been peeling my application back towards a "hello world" style app and found that the issue appears to be Django version. It works when I regress to Django==2.0.0. I had been using Django==3.2.3. I'm not sure if this is supported, I can't see it written anywhere what is supported in the docs. I saw on this video (https://www.youtube.com/watch?v=oRaUXvUWJ0U) at 11:48 that it only works out of the box with Django==2.0.0, but you can get it to work with newer versions with some alterations, but it doesn't say what, unfortunately. – goose May 17 '21 at 21:27
  • @goose - sorry for the troubles. I am currently using Django 3.1.5 on the python 3.8 lambci image and Zappa 0.52 - working quite fine. I will try to upgrade to 3.2.x and test. I suggest for now that you bump to 3.1 and try that. – Edgar May 18 '21 at 22:28
  • Thanks Edgar, that's very instructive. I take it you're using Docker? I'm not currently and I'm guessing now this may be a part cause of my troubles. – goose May 19 '21 at 09:50
  • Docker is by far the best way to ensure compatibility with AWS Lambda. In fact, there was just another question about problems with Zappa and it turned out they were building the virtual env with compiled packages on MacOS. Switched to Docker and it worked fine. – Edgar May 19 '21 at 21:34
  • Would you be able to briefly outline your approach to using Docker? Do you use docker compose with a dockerfile? I don't suppose there's any way you could share a redacted version of your dockerfile if you do? I've used Docker one or two times before, but I'm far from being a pro! No worries if not. – goose May 20 '21 at 20:24
  • Ok I think I've gotten a good enough refresher from here - https://docs.docker.com/samples/django/#define-the-project-components, but please feel free to add any pointers. Marking this as the correct answer as ensuring it works on local is a good guide, just with the modification to use docker with the lambci image. – goose May 22 '21 at 14:27
  • I have a course that has a number of free lessons focusing on docker: https://www.newline.co/courses/serverless-django-with-zappa/welcome – Edgar Jun 14 '21 at 13:31
  • Thanks Edgar, will check that out. – goose Jun 15 '21 at 19:41
  • Edgar, probably a bit later than you expected, but after a bit of a rest I've followed most of your course and gotten a Django app deployed on lambda. I still need to actually deploy my own app using the same, but this feels really good like I'm unblocked. Had been slamming my head against the wall for a bit there. Thanks for creating the course. FYI I ran in to "AttributeError: 'Template' object has no attribute 'add_description'". I had to comment out "self.cf_template.add_description("Automatically generated with Zappa")" from zappa core.py file to get this to work. – goose Jul 14 '21 at 21:21
  • Hi again - I'm still running into trouble, but I'm not out of things that I need to try. Out of interest though, does the fact that my lambda function is 159MB large maybe point to a problem? I'm wondering if some large assets are in the mix or whether all django lambda functions end up being this large. My initial deployment always fails to unpack. I alwats have to increase the timeout and run a test to unpack it. Then when re-triggering the function I see the following in the logs: "amazonaws found in host 'NoneType' object is not callable Exception received when sending HTTP request." – goose Jul 15 '21 at 21:23
  • @goose try the `slim_handler` option from https://github.com/zappa/Zappa#large-projects – Edgar Jul 17 '21 at 12:44
  • Yes this was my issue. I had an error in my code (unused imported function) that caused this error. Once I removed it, I was good to go. – B. Latif Nov 11 '22 at 19:12
3

I was running into a similar issue, tried installing different versions of flask, and discovered it was actually in my zappa configuration file. I had "slim_handler" : "True" and removing this solved the issue for me.

S.B
  • 13,077
  • 10
  • 22
  • 49
Joe Webb
  • 61
  • 3