Github Runner部署Lambda时未安装requirements.txt依赖(FastAPI缺失)
问题:AWS Lambda部署后找不到FastAPI模块(Github Actions CI/CD流程)
Github Actions的CI阶段执行完成,但将应用部署到AWS Lambda后,运行时出现报错提示无法找到FastAPI模块,而FastAPI已明确包含在requirements.txt文件中。怀疑问题出在CD部署环节,尝试多种教程后仍未解决。
原Github Actions工作流配置
name: API CI/CD on: # Trigger the workflow on push push: branches: # Push events on main branch - main # The Job defines a series of steps that execute on the same runner. jobs: CI: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: pip3 install -r ./app/requirements.txt - name: Create application ZIP archive run: cd ./app && zip -r app.zip . - name: Upload app.zip artifact uses: actions/upload-artifact@v2 with: name: app path: ./app/app.zip - name: Upload swagger.json artifact uses: actions/upload-artifact@v2 with: name: swagger path: ./app/swagger.json CD: runs-on: ubuntu-latest needs: [ CI ] if: github.ref == 'refs/heads/main' && github.event_name == 'push' steps: - name: Install AWS CLI uses: unfor19/install-aws-cli-action@v1 with: version: 1 env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} - name: Download Lambda app.zip uses: actions/download-artifact@v2 with: name: app - name: Download swagger.json artifact uses: actions/download-artifact@v2 with: name: swagger - name: Upload app.zip to S3 run: aws s3 cp app.zip s3://telidexapigithubdeploy/app.zip env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} - name: Upload swagger.json to S3 run: aws s3 cp swagger.json s3://telidexapiswaggerjson/swagger.json env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} - name: Deploy new Lambda run: aws lambda update-function-code --function-name telidexapi --s3-bucket telidexapigithubdeploy --s3-key app.zip env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
AWS Lambda报错信息
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | timestamp | message | |---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1692052249122 | INIT_START Runtime Version: python:3.9.v26 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:130681a0855afedf31b2b3fbcc2fbf1ca62875e0500edb56fd16cad65045b05b | | 1692052249226 | START RequestId: 9a247ffa-5364-4eb5-9596-e1015c45bd84 Version: $LATEST | | 1692052249226 | [ERROR] Runtime.ImportModuleError: Unable to import module 'main': No module named 'fastapi' Traceback (most recent call last): | | 1692052249228 | END RequestId: 9a247ffa-5364-4eb5-9596-e1015c45bd84 | | 1692052249228 | REPORT RequestId: 9a247ffa-5364-4eb5-9596-e1015c45bd84 Duration: 2.36 ms Billed Duration: 3 ms Memory Size: 128 MB Max Memory Used: 36 MB Init Duration: 103.49 ms | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
问题根源
CI阶段只是把依赖安装到了Github Runner的系统环境里,并没有将依赖包打包到app.zip中。AWS Lambda的运行环境没有预先安装FastAPI,所以执行时找不到对应的模块。
修复方案
修改CI阶段的依赖安装步骤,将依赖包直接安装到app目录下,这样打包时就能把代码和依赖一起包含进去:
- 修改CI中的
Install dependencies步骤,添加-t ./app参数指定安装路径:
pip3 install -r ./app/requirements.txt -t ./app
-t参数会把所有依赖包安装到指定的./app目录,和你的代码文件放在一起。
- 保持后续的打包步骤不变,此时生成的app.zip会包含你的代码以及所有依赖包。
额外检查项
- 确认requirements.txt中确实包含FastAPI,例如
fastapi>=0.100.0 - 确保AWS Lambda的运行时设置为Python 3.9,与CI阶段使用的Python版本一致
修改后的CI阶段示例
CI: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies to app directory run: pip3 install -r ./app/requirements.txt -t ./app - name: Create application ZIP archive run: cd ./app && zip -r app.zip . - name: Upload app.zip artifact uses: actions/upload-artifact@v2 with: name: app path: ./app/app.zip - name: Upload swagger.json artifact uses: actions/upload-artifact@v2 with: name: swagger path: ./app/swagger.json
内容的提问来源于stack exchange,提问作者Kyle Sponable
相关产品推荐
相关产品推荐

