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

Serverless架构下Lambda Layers导入模块失败,报Runtime.ImportModuleError

问题:Serverless框架中Lambda Layers依赖无法导入

需求与初始目录结构

计划在Serverless应用中基于不同目录的requirements.txt创建3个Lambda Layers,初始目录结构如下:

app-directory/
├── lambda functions codes ...
├── layers/
│   ├── layer_A/
│   │   └── requirements.txt
│   ├── layer_B/
│   │   └── requirements.txt
│   └── layer_C/
│       └── requirements.txt
└── serverless.yml

第一次尝试

操作步骤

  1. 创建虚拟环境并安装依赖:
    python3.9 -m venv python
    pip install -r layers/layer_A/requirements.txt
    
    依赖安装到python/lib/python3.9/site-packages/后,压缩python目录。
  2. 在serverless.yml中配置Layers:
    layers:
      layerA:
        path: layers/layer_A
        compatibleRuntimes:
          - python3.9
    
      layerB:
        path: layers/layer_B
        compatibleRuntimes:
          - python3.9
    
      layerC:
        path: layers/layer_C
        compatibleRuntimes:
          - python3.9
    
  3. 函数中引用Layers:
    migrations:
      handler: src.migrations.lambda_handler
      layers:
        - !Ref LayerALambdaLayer
        - !Ref LayerBLambdaLayer
        - !Ref LayercLambdaLayer
    

执行结果

执行sls deploy和sls invoke stage test --function migrations后,出现导入错误:

{
    "errorMessage": "Unable to import module 'src.migrations.handler': No module named 'alembic'",
    "errorType": "Runtime.ImportModuleError",
    "requestId": "59a33572-9439-4590-b547-de6084e784b3",
    "stackTrace": []
}
Environment: darwin, node 20.8.0, framework 3.35.2 (local) 3.38.0v (global), plugin 7.0.5, SDK 4.4.0
Credentials: Local, "default" profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
Invoked function failed

第二次尝试

操作步骤

  1. 单独创建存放requirements的目录,编写bash脚本自动安装依赖并压缩:
    #!/bin/bash
    
    set -e
    
    LAYER_TARGET_DIR="layers"
    LAYER_REQUIREMENTS_DIR="layers_requirements"
    RUNTIME="python3.9"
    
    for LAYER_PATH in $LAYER_TARGET_DIR/*/; do
      LAYER=$(basename $LAYER_PATH)
    
      printf "Processing $LAYER layer\n"
      if [ -f "$LAYER_PATH/python.zip" ]; then
        echo "Existing python.zip found in $LAYER layer. Deleting...\n"
        rm -rf "$LAYER_PATH/python.zip"
      fi
    
      printf "Installing dependencies for $LAYER layer\n"
      mkdir $LAYER_TARGET_DIR/$LAYER/python
      pip3 install -r $LAYER_REQUIREMENTS_DIR/$LAYER/requirements.txt -t $LAYER_TARGET_DIR/$LAYER/python/
      printf "Dependencies installed for $LAYER layer\n"
        
      printf "Zipping the python folder\n"
      cd $LAYER_TARGET_DIR/$LAYER
      zip -r9 python.zip python
      cd - > /dev/null
    
      rm -rf $LAYER_TARGET_DIR/$LAYER/python
    
      printf "$LAYER layer processing complete"
    done
    
    printf "\nAll layers processed\n--------------------------------------\n"
    
  2. 执行脚本后目录结构变为:
    ├── lambda functions codes ...
    ├── layers_requirements/
    │   ├── layer_A/
    │   │   └── requirements.txt
    │   ├── layer_B/
    │   │   └── requirements.txt
    │   └── layer_C/
    │       └── requirements.txt
    ├── layers/
    │   ├── layer_A/
    │   │   └── python.zip
    │   ├── layer_B/
    │   │   └── python.zip
    │   └── layer_C/
    │       └── python.zip
    └── serverless.yml
    

执行结果

仍出现相同的No module named 'alembic'导入错误。

疑问

已核对目录结构符合AWS要求,请问是否配置有误或遗漏了什么?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 02:06:15