Black Lives Matter. Please consider donating to Black Girls Code today.

Deploy on AWS lambda

I’m trying to deploy dash app on AWS lambda. Please let me know if anyone tried this before. I’m repeatedly receiving Dash object is not callable error.

I tried to deploy same app in heroku. It did well without any issues.

You need to use the server attribute of your Dash instance rather than the entire Dash instance for the lambda entry point (as with using any other WSGI deployment method)

There is a discussion about getting this working on this github issue: https://github.com/plotly/dash/issues/22

There’s also zappa_dash, a package I’ve just come across that uses Zappa. I haven’t used this, is probably woth exploring. In general, Zappa greatly simplifies deployment of WSGI apps to AWS.

Thanks for the info. I followed all the steps. I’m receiving moduleNotFoundError at the final stage but latest dash version is mentioned in the requirements.txt as well. Any thoughts?

> [1553063476425] Failed to find library...right filename?
> [1553063476563] No module named 'dash.dependencies': ModuleNotFoundError
> Traceback (most recent call last):
>   File "/var/task/handler.py", line 602, in lambda_handler
>   return LambdaHandler.lambda_handler(event, context)
>   File "/var/task/handler.py", line 245, in lambda_handler
>   handler = cls()
>   File "/var/task/handler.py", line 139, in __init__
>   self.app_module = importlib.import_module(self.settings.APP_MODULE)
>   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 994, in _gcd_import
>   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
>   File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
>   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
>   File "<frozen importlib._bootstrap_external>", line 678, in exec_module
>   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
>   File "/tmp/nseoptionalpha/dashmain.py", line 10, in <module>
>   from dash.dependencies import Input, Output, State
> ModuleNotFoundError: No module named 'dash.dependencies'

Did you create the deployment package with dash library in it?

@caiyij No. I didn’t. How to do that?

AWS documentation:
https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

@caiyij I’m using Zappa to package it for me. Do I need to zip it manually?

You should be running Zappa from an active virtualenv that has all your dependencies installed (including Dash). Zappa automatically grabs these dependencies and include them in the package that is uploaded to lambda.

Why did you decide to deploy using AWS Lambda instead of a S3 serverless app?

I currently have a dash app on an EC2 instance with the eventual goal of an s3 app…but I haven’t heard of using AWS lambda to deploy your app.

Did anyone accomplish to deploy it to AWS Lambda successfully? (Or google cloud functions / azure functions)

@dadv92 Have a look at https://github.com/mcrowson/zappa_dash/. The only modification you need to make is described here: https://github.com/mcrowson/zappa_dash/issues/2 at the bottom.

Hi sclavijo,

I am currently trying to deploy on an EC2 instance using gunicorn + a very standard dash app.

I try to start the webserver with:
gunicorn -b 0.0.0.0:8050 app:server

which runs fine:
[INFO] Starting gunicorn 20.0.4
[INFO] Listening at: http://0.0.0.0:8050

But when I visit the webpage at ip_address:8050 all I get is a “Connection timed out” error.

What am I missing? Any help will be much appreciated!

Regards

Here is an example of a basic Dash appllication on AWS Lambda:

The important thing when using awsgi is to disable Flask response compression:
app = dash.Dash(name, compress=False)

otherwise awsgi will get zipped response from Flask and fail on attempt to decode it.