I get errors when I deploy my app on AWS Lambda

I am trying to run my flask api on AWS lambda, using serverless framework for deployment.

My app works fine locally when I use sls wsgi serve but I get errors (502 bad gateway) when I deploy to AWS Lambda :

021-03-12T16:26:21.213+01:00	START RequestId: deedf3d6-eb1e-4f02-ac0c-47428c5bd553 Version: $LATEST

2021-03-12T16:26:21.216+01:00	module initialization error: Unable to import app.app

2021-03-12T16:26:21.331+01:00	END RequestId: deedf3b6-eb1e-4f02-ac0c-47428c5bd553

2021-03-12T16:26:21.331+01:00	REPORT RequestId: deedf3b6-eb1e-4f02-ac0c-47428c5bd553 Duration: 115.30 ms Billed Duration: 116 ms Memory Size: 1024 MB Max Memory Used: 28 MB

2021-03-12T16:26:21.331+01:00	module initialization error Unable to import app.app

2021-03-12T16:26:22.072+01:00	Traceback (most recent call last):

2021-03-12T16:26:22.072+01:00	File "/var/task/wsgi_handler.py", line 44, in import_app

2021-03-12T16:26:22.072+01:00	wsgi_module = importlib.import_module(wsgi_fqn_parts[-1])

2021-03-12T16:26:22.072+01:00	File "/var/lang/lib/python3.6/importlib/__init__.py", line 126, in import_module

2021-03-12T16:26:22.072+01:00	return _bootstrap._gcd_import(name[level:], package, level)

2021-03-12T16:26:22.072+01:00	File "<frozen importlib._bootstrap>", line 994, in _gcd_import

2021-03-12T16:26:22.072+01:00

Copy
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 971, in _find_and_load

2021-03-12T16:26:22.072+01:00	File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked

2021-03-12T16:26:22.072+01:00	File "<frozen importlib._bootstrap>", line 665, in _load_unlocked

2021-03-12T16:26:22.072+01:00	File "<frozen importlib._bootstrap_external>", line 678, in exec_module

2021-03-12T16:26:22.072+01:00	File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

2021-03-12T16:26:22.072+01:00	File "/var/task/app.py", line 10, in <module>

2021-03-12T16:26:22.072+01:00	from common.helpers import *

2021-03-12T16:26:22.072+01:00	File "/var/task/common/helpers.py", line 1, in <module>

2021-03-12T16:26:22.072+01:00	from common.db import db

2021-03-12T16:26:22.072+01:00	File "/var/task/common/db.py", line 1, in <module>

2021-03-12T16:26:22.072+01:00	from flask_pymongo import pymongo

2021-03-12T16:26:22.072+01:00	ModuleNotFoundError: No module named 'flask_pymongo'

I have two separated files app.py and db.py in which the Flask_pymongo is imported.

This is my DockerFile

FROM lambci/lambda:build-python3.6
RUN pip3 --no-cache-dir install -r requirements.txt

requirements.txt :

Werkzeug==0.16.1
Flask_PyMongo==2.3.0
Flask==1.1.1
Flask_Limiter==1.3.1
Flask_JWT_Extended==3.22.0
pymongo==3.10.1
pymongo[tls,srv]
dnspython

And my serverless.yml

service : my-flask-app
provider:
  name: aws
  runtime: python3.6
  stage: dev
  region: us-east-1
plugins:
  - serverless-wsgi
  - serverless-python-requirements
custom:
  wsgi:
    app: app.app
    packRequirements: false
    pythonBin: python3
  pythonRequirements:
    dockerizePip: non-linux
    dockerFile: ./Dockerfile
package:
  exclude:
    - node_modules/**
    - venv/**
functions:
  app:
    handler: wsgi_handler.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

Any suggestion ?

1 Like