错误:“无法导入模块“app”:没有名为“flask”的模块。调用Lambda Python API时出错

2024-04-19 04:28:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将基于Python的lambda REST服务(hello world)部署到AWS中。这是我的代码片段。我正在使用GitHub CI/CD平台进行自动化部署

# app.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

我的GitHub操作是

name: deploy-aws-lambda

on:
  push:
    branches:
      - master
jobs:
  deploy:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [10.x]
        python-version: [3.7]
    steps:
      - uses: actions/checkout@master
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Install Dependencies
        run: npm install flask && npm install
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: eu-central-1
      - name: Install Serverless Framework
        run: npm install -g serverless
      - name: Serverless Deploy
        run: npm run-script deploy
        

在部署期间,它正在部署,我可以在API网关中看到它。但在调用时,我犯了以下错误

Sat Jun 12 04:35:37 UTC 2021 : Endpoint response body before transformations: {"errorMessage": "Unable to import module 'app': No module named 'flask'", "errorType": "Runtime.ImportModuleError", "stackTrace": []}

我的serverless.yaml文件如下

# serverless.yml

service: serverless-flask

provider:
  name: aws
  runtime: python3.7
  stage: dev
  region: us-east-1

plugins:
  - serverless-python-requirements
  - serverless-wsgi

custom:
  wsgi:
    app: app.hello
    packRequirements: false
  pythonRequirements:
    dockerizePip: non-linux
    pythonBin: python3


functions:
  create:
    handler: app.hello
    events:
      - http:
          path: hello
          method: get
          cors: true


Tags: runnameawsnodeappflaskhellonpm
1条回答
网友
1楼 · 发布于 2024-04-19 04:28:50

npm安装烧瓶在我看来不合适。它可能正在安装nodejsFlask,这与python完全不同。登录AWS控制台并检查随lambda函数处理程序文件打包的内容。还要检查是否附着了任何层。并仔细检查使用github CI/CD安装python依赖项的正确方法

另外,如果您愿意更改部署策略,请查看Zappa。它易于部署,并且是为python构建的

相关问题 更多 >