【问题标题】:AWS Lambda function does not recognize my Python dependency layerAWS Lambda 函数无法识别我的 Python 依赖层
【发布时间】:2021-11-06 12:36:28
【问题描述】:

我的目标是使用存储 python 依赖项的 python 3.7 运行时为 aws lambda 函数上传一个小层,但我似乎无法让它识别模块。

我创建了一个 Makefile 来生成 zip 文件

create-lambda-layer:
    docker run -dit --name AL amazonlinux:latest
    docker cp requirements.in AL:/root/requirements.in
    docker exec -it AL bash -c "yum update -y \
        && yum install -y python3.7 zip  \ 
        && python3 -m pip install --upgrade pip \
        && pip install wheel \
        && mkdir -p /root/lambda/ \
        && pip install -r /root/requirements.in -t /root/lambda/ \
        && cd /root/lambda/ \
        && zip -r python.zip ."
    docker cp AL:/root/lambda/python.zip .
    docker stop AL
    docker rm AL

和我所需的模块在同一文件夹中的 requirements.in

holidays==0.11.2
python-gitlab==2.10.1

我将层放在服务中并通过无服务器框架进行部署,但我也测试了直接使用 AWS 控制台上传相同的 .zip,但失败了。我的 lambda 函数源文件(正确链接到图层)仅是:

import gitlab
import holidays

print("holidays: ", holidays.__version__)
print("gitlab", gitlab.__version__)

def lambda_handler(event, context): 
    return()

我收到以下错误(类似于假期):

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'gitlab'",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}

我的 python.zip 的内容对我来说并不重要,它只是包含它应该包含的依赖项:

PyMeeus-0.5.11.dist-info            hijri_converter                        pytz-2021.1.dist-info
__pycache__                         hijri_converter-2.2.1.dist-info        requests
bin                                 holidays                               requests-2.26.0.dist-info
certifi                             holidays-0.11.2.dist-info              requests_toolbelt
certifi-2021.5.30.dist-info         idna                                   requests_toolbelt-0.9.1.dist-info
charset_normalizer                  idna-3.2.dist-info                     six-1.16.0.dist-info
charset_normalizer-2.0.4.dist-info  korean_lunar_calendar                  six.py
convertdate                         korean_lunar_calendar-0.2.1.dist-info  tests
convertdate-2.3.2.dist-info         pymeeus                                urllib3
dateutil                            python_dateutil-2.8.2.dist-info        urllib3-1.26.6.dist-info
docs                                python_gitlab-2.10.1.dist-info
gitlab                              pytz

我想知道这是为什么。我之前尝试过使用ubuntu创建同层zip,并且成功了,但是要正式实现它,我不能使用ubuntu图像。

有没有人知道我可以做些什么来追踪问题或如何解决这个问题?我在安装依赖项时使用 pip 作为 root 是否会产生任何影响?

谢谢!

【问题讨论】:

    标签: python amazon-web-services aws-lambda aws-lambda-layers


    【解决方案1】:

    我遇到了同样的问题,我花了一周的时间才解决。我认为这个答案可以帮助你: Import libraries in lambda layers

    这里是关键的东西

    您希望确保您的 .zip 在解压缩时遵循此文件夹结构

    python/lib/python3.6/site-packages/{LibrariesGoHere}。

    上传该 zip,确保该层已添加到 Lambda 函数中,您应该一切顺利。

    另外,如果您将标签更新为“aws-lambda-layers”,我认为您的问题会得到更好的关注

    【讨论】:

    • 非常感谢!我很高兴,它成功了!虽然我有点困惑,因为我在 ubuntu-made 层内有相同的目录结构(只是 python/{modules}),并且在使用这一层时,lambda 在查找模块时没有任何问题,我错误地排除了这将是因为 zip 结构。魔法?也感谢您关于标签的提示,我是第一次发帖,只是潜伏在这里 :) 但似乎有人不错已经为我正确标记了它。
    猜你喜欢
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2021-09-25
    • 2017-10-28
    • 2023-01-18
    • 1970-01-01
    • 2021-04-16
    • 2017-02-22
    相关资源
    最近更新 更多