【问题标题】:Error importing a cffi python module from AWS Lambda Layer从 AWS Lambda 层导入 cffi python 模块时出错
【发布时间】:2021-03-28 05:18:45
【问题描述】:

我在将 argon2-cffi python module (version 20.1.0) 从 AWS Lambda 层导入 AWS Lambda 函数时遇到问题。

这似乎适用于所有以 C 为基础的包,因为我用 pandas 测试了它并得到了相同的结果。

我对 Layer 的 cloudformation 配置如下:

  MyLib:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: my-lib
      Description: In-house and 3rd party dependencies for my app.
      ContentUri: lambdas/lib/.
      CompatibleRuntimes:
        - python3.8

/lib里面的文件夹结构是这样的:

lib
├── python
│   ├── app
│   │   └── ...
│   └── lib/python3.8/site-packages
│       ├── argon2
│       │   └── ...
│       ├── argon2_cffi-20.1.0.dist-info
│       │   └── ...
│       ├── ...
│       └── stdnum
│           └── ...
└── requirements.txt

根据recommendations from AWSpython/app 文件夹包含我的自定义库,python/lib/python3.8/site-packages 文件夹包含第 3 方包。

我知道由于 argon2 依赖于 C 代码 it must be installed on the runtime environment that the Lambda function using it will be hosted in,所以我使用 AWS 提供的 docker 镜像通过命令将包安装到 lib/python3.8/site-packages

docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"

我尝试运行的 lambda 函数在 cloudformation 模板中配置为

  AuthorizerFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: lambdas/handlers
      Handler: authorizer.handler
      Runtime: python3.8
      ...
      Layers:
        - !Ref MyLib

并从这些导入开始:

# authorizer.py

import app.utils as utils
import stdnum
from argon2 import PasswordHasher

但是当我尝试运行它时,我收到以下错误

[ERROR] Runtime.ImportModuleError: Unable to import module 'authorizer': No module named 'argon2._ffi'

表示appstdnum都导入成功,但argon2没有。

有什么想法可能是错的吗?

我也欢迎任何关于另一个具有久经考验的散列算法的散列库的建议。

更新:按照推荐的here 更新我的cffipipsetuptools 并没有成功。

更新 2:我可以看到使用 docker 命令安装的氩气是 linux 兼容的,因为输出包括以下几行:

Collecting argon2-cffi==20.1.0
  Downloading argon2_cffi-20.1.0-cp35-abi3-manylinux1_x86_64.whl (97 kB)

【问题讨论】:

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


    【解决方案1】:

    好的,所以我的问题的答案有点尴尬。无论如何,我会在这里发布它,以防它可能加深对某人的理解。

    我正在使用 git 进行源代码控制,并使用 VS Code 中的模板作为 .gitignore 文件。此外,我还设置了一个自动构建和部署流程,它根据 git repo 中的内容构建 Lambda 应用程序。

    在尝试调试此问题几天后,我发现 Lambda 层的 argon2_cffi 文件夹中缺少 .so 文件。罪魁祸首是前面提到的.gitignore 文件,其中包含以下几行:

    # C extensions
    *.so
    

    难怪所有基于 C 的 python 包都不起作用!

    我从 .gitignore 中删除了上述行,并将所有 .so 文件提交到以前从未进入 Lambda 层的 repo,现在一切正常。

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 1970-01-01
      • 2019-02-23
      • 2021-11-07
      • 2016-05-22
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      相关资源
      最近更新 更多