You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Serverless与Bitbucket Pipeline部署AWS Lambda Python遇问题求助

AWS Lambda部署问题解决建议

针对依赖导入失败问题

  • 确保serverless-python-requirements插件配置正确,开启zip后需保证自动解压:
    在serverless.yml的custom段添加:
    custom:
      pythonRequirements:
        zip: true
        unzipRequirements: true  # 确保未被设置为false,默认开启自动解压
        slim: true  # 自动移除依赖中的测试、文档等冗余文件
    
  • 适配Lambda环境安装依赖:pandas、awswrangler包含原生扩展,本地安装包与Lambda的Amazon Linux环境不兼容,需开启dockerizePip让插件在Docker中模拟Lambda环境构建依赖:
    custom:
      pythonRequirements:
        dockerizePip: true
        zip: true
        slim: true
    
  • 检查代码导入语句:确认awswrangler的导入写法为import awswrangler as wr,避免误写为import wrangler;同时验证项目结构与handler路径匹配,比如serverless_myproject文件夹需包含__init__.py,确保是合法Python包。

同时解决包体积超限问题

  • 严格配置包的排除/包含规则,只打包必要文件:
    package:
      exclude:
        - ./**
      include:
        - serverless_myproject/**  # 你的项目代码目录
        - handler.py  # Lambda入口文件(如果不在项目目录内)
    
  • 配合pythonRequirements的slim选项,自动清理依赖包中的冗余内容,进一步压缩体积。

本地验证

  • 使用lambci/lambda:python3.9 Docker镜像模拟Lambda环境,挂载本地代码与依赖,运行测试命令验证依赖是否能正常导入,提前排查环境不兼容问题。

内容的提问来源于stack exchange,提问作者Cotrariello

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 01:50:35