使用Serverless Framework、poetry、python3.10和fastapi部署AWS Lambda时,`No module named...`

qoefvg9y  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(374)

编辑

看起来问题来自无服务器插件serverless-python-requirements
当使用$ sls package打包时,当使用python = "^3.9"时,依赖项在zip中,但使用python = "^3.10"时,它们不在zip中。其他都是一样的。

编辑结束

我想使用 Serverless Frameworkpoetrypython3.10fastapi 部署AWS Lambda。
我用 python3.9 做了同样的事情,它工作了。它必须与我的本地配置的东西,你能帮助,请?
调用端点https://***********.execute-api.eu-west-3.amazonaws.com/development/api/health-check/时得到的答案是{"message": "Internal server error"}
lambda的日志说:

[ERROR] Runtime.ImportModuleError: Unable to import module 'main': No module named 'fastapi'
Traceback (most recent call last):
# main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from mangum import Mangum

app = FastAPI()

app.add_middleware(CORSMiddleware, allow_origins="*", allow_credentials=True, allow_methods=["*"], allow_headers=["*"])

@app.get("/api/health-check/")
def health_check():
    return {"message": "OK"}

handle = Mangum(app)

我是这样做的:

$ poetry init
.....
Compatible Python versions [^3.9]:  3.10

Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file
.....
[tool.poetry.dependencies]
python = "3.10"
$ poetry add --dev pytest pytest-cov black isort flake8 bandit
The currently activated Python version 3.9.9 is not supported by the project (3.10).
Trying to find and use a compatible version.

Poetry was unable to find a compatible version. If you have one, you can explicitly use it via the "env use" command.

在我的机器上,我有三个版本的Python:

$ python3 --version
Python 3.9.9

$ python3.10 --version
Python 3.10.11

pyproject.toml seams中编辑python版本以解锁情况:

[tool.poetry.dependencies]
python = "~3.10"
$ poetry add --dev pytest pytest-cov black isort flake8 bandit
The currently activated Python version 3.9.9 is not supported by the project (~3.10).
Trying to find and use a compatible version.
Using python3.10 (3.10.11)
Creating virtualenv test-sls-deploy-md9kc90P-py3.10 in /Users/costin/Library/Caches/pypoetry/virtualenvs
.....

$ poetry add fastapi uvicorn httpx
The currently activated Python version 3.9.9 is not supported by the project (~3.10).
Trying to find and use a compatible version.
Using python3.10 (3.10.11)
Using version ^0.95.1 for fastapi
.....

然后我部署与

$ sls deploy --stage development --verbose

我得到一个警告,python3.10不在预期的运行时环境列表中,但它部署正确。
下面是serverless.yml文件:

service: test-sls-deploy-api

frameworkVersion: '3'
useDotenv: true

provider:
  name: aws
  runtime: python3.10
  region: 'eu-west-3'
  stage: 'development'
  logRetentionInDays: 30

functions:
  TEST-DEPLOY:
    handler: main.handle
    memorySize: 512
    events:
      - http:
          path: /{proxy+}
          method: any
          cors:
            origin: ${env:ALLOWED_ORIGINS}
            maxAge: 60

custom:
  pythonRequirements:
    usePoetry: true
    noDeploy:
      - boto3  # already on Lambda
      - botocore  # already on Lambda

plugins:
  - serverless-python-requirements

我本来希望serverless-pytohn-requirements插件能像处理python3.9一样处理fastapi的部署,但是python3.10的情况并没有像预期的那样发生。
你看到什么可能出错了吗?

a11xaf1n

a11xaf1n1#

不确定问题的根本原因,但这样做,解决了问题:

  • 卸载python3.9并使python3.10响应python3
  • pyproject.toml中使用python = "^3.10"而不是python = "~3.10"
  • 我不知道这是否有影响,但我还在项目文件夹中使用
## poetry.toml
[virtualenvs]
in-project = true

为了调试,我使用了$ sls package并比较了.zip,直到存在依赖关系。
部署️时仍然有来自sls的警告,因为它不期望python3.10,但由于AWS CloudFormation是ok的,所以一切都很好,并且lambdas有python3.10运行时。

相关问题