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
第一次尝试
操作步骤
- 创建虚拟环境并安装依赖:
依赖安装到python3.9 -m venv python pip install -r layers/layer_A/requirements.txtpython/lib/python3.9/site-packages/后,压缩python目录。 - 在
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 - 函数中引用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
第二次尝试
操作步骤
- 单独创建存放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" - 执行脚本后目录结构变为:
├── 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
相关产品推荐
相关产品推荐

