我正在尝试在 AWS lambda 上运行我的 Flask api,使用 serverless framework
进行部署。
当我使用 sls wsgi serve
时,我的应用程序在本地运行良好,但在部署到 AWS Lambda 时出现错误 (502 bad gateway
) :
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'
我有两个单独的文件 app.py
和 db.py
,其中导入了 Flask_pymongo
。
这是我的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
还有我的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+}'
有什么建议吗?
答案 0 :(得分:0)
我认为您的 Dockerfile
不正确,您没有复制 requirements.txt
来让 pip 为您安装软件包,因为没有打包依赖项。应该是这样的
FROM lambci/lambda:build-python3.6
COPY requirements.txt requirements.txt
RUN pip3 --no-cache-dir install -r requirements.txt
您的 Dockerfile
出现此错误:
Step 2/2 : RUN pip3 --no-cache-dir install -r requirements.txt
---> Running in 1cda237064f0
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
我不确定您在部署应用程序时是否在构建日志中注意到了这一点。
还有一点,假设您使用的是 serverless-python-requirements 而不是任何其他变体,您不应该直接在 requirements.txt
中包含 Dockerfile
。
pythonRequirements:
dockerizePip: non-linux
dockerFile: ./Dockerfile
Dockerfile
FROM lambci/lambda:build-python3.6
# Install your dependencies in case you want to install any
#RUN yum -y install myfavourite-pacakge
此配置将采用 Dockerfile
并将 requirements.txt
添加到其中。