简体   繁体   English

从 AWS Lambda 层导入 cffi python 模块时出错

[英]Error importing a cffi python module from AWS Lambda Layer

I am having a problem importing the argon2-cffi python module (version 20.1.0) from an AWS Lambda Layer into an AWS Lambda function. I am having a problem importing the argon2-cffi python module (version 20.1.0) from an AWS Lambda Layer into an AWS Lambda function.

This seems to apply to all packages that have C as a base, because I tested this with pandas and got the same results.这似乎适用于所有以 C 为基础的软件包,因为我用pandas对此进行了测试并得到了相同的结果。

My cloudformation config for the Layer is the following:我的图层的 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

and the folder structure inside /lib is this: /lib里面的文件夹结构是这样的:

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

as per recommendations from AWS , where the folder python/app contains my custom library and the folder python/lib/python3.8/site-packages contains 3rd party packages.根据AWS 的建议,其中文件夹python/app包含我的自定义库,文件夹python/lib/python3.8/site-packages包含第 3 方包。

I know that since argon2 depends on C-code, it must be installed on the runtime environment that the Lambda function using it will be hosted in , so I use a docker image provided by AWS to install the packages into lib/python3.8/site-packages using the command我知道由于 argon2 依赖于 C 代码, 它必须安装在运行时环境中,使用它的 Lambda function 将托管在,所以我使用 Z05B6053C41A2130AFD6Z3FC3B158BDA4E 安装包到 AWS 提供的lib/python3.8/site-packages使用命令的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"

The lambda function I'm trying to run is configured in the cloudformation template as我尝试运行的 lambda function 在 cloudformation 模板中配置为

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

and starts with these imports:并从这些导入开始:

# authorizer.py

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

But when I try to run it, I get the following error但是当我尝试运行它时,出现以下错误

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

meaning that app and stdnum both get imported successfully, but not argon2 .意味着appstdnum都成功导入,但不是argon2

Any ideas what might be wrong?有什么想法可能是错的吗?

I would also welcome any suggestions on another hashing library with a tried and tested hashing algorithm.我也欢迎对另一个具有久经考验的散列算法的散列库提出任何建议。

UPDATE: Updating my cffi , pip and setuptools as is recommended here did not do the trick.更新:按照 这里的建议更新我的cffipipsetuptools并没有成功。

UPDATE 2: I can see that the argon being installed using the docker command is the linux compatible one, since the output includes these lines:更新 2:我可以看到使用 docker 命令安装的氩气是与 linux 兼容的,因为 output 包括以下行:

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

Alright, so the answer to my problem is a bit of an awkward one.好吧,所以我的问题的答案有点尴尬。 I'll post it here anyhow in case it might depthen an understanding for someone.无论如何,我会在这里发布它,以防它可能加深对某人的理解。

I am using git for source control and used a template from VS Code for the .gitignore file.我正在使用 git 进行源代码控制,并使用 VS Code 中的模板作为.gitignore文件。 Furthermore, I have set up an automatic build and deployment process, which builds the Lambda application based on the content in the git repo.此外,我还设置了一个自动构建和部署过程,它基于 git 存储库中的内容构建 Lambda 应用程序。

After days of trying to debug this problem I found out that .so files were missing from the argon2_cffi folder in the Lambda layer.经过几天尝试调试此问题后,我发现 Lambda 层中的argon2_cffi文件夹中缺少.so文件。 The culprit was the aforementioned .gitignore file, which contained these lines:罪魁祸首是前面提到的.gitignore文件,其中包含以下几行:

# C extensions
*.so

No wonder none of the C-based python packages were working!难怪基于 C 的 python 包都没有工作!

I removed said lines from .gitignore and committed all the .so files to the repo that had never made it to the Lambda Layer before, and now everything is working fine.我从.gitignore中删除了上述行,并将所有.so文件提交到以前从未进入 Lambda 层的 repo,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 aws-lambda 导入 pysftp 时如何修复导入错误“No module named '_cffi_backend'” - How to fix import error "No module named '_cffi_backend' " when import pysftp from aws-lambda AWS Amplify 和导入 Python 中的 Lambda 层会导致找不到模块错误 - AWS Amplify and importing Lambda Layers in Python gives module not found error 将 Python 模块上传为 AWS Lambda 层 - Uploading Python module as AWS Lambda Layer AWS Lambda:无法导入模块“python_handler”:没有名为“_cffi_backend”的模块 - AWS Lambda: Unable to import module 'python_handler': No module named '_cffi_backend' 在Amazon AWS Lambda上导入Python模块“ Pulp” - Importing Python Module “Pulp” on Amazon AWS Lambda AWS Lambda - 导入 pymssql 时出错(未找到模块) - AWS Lambda - error on importing pymssql (module not found) AWS Lambda 无法从层导入模块 - AWS Lambda unable to import module from layer 将 PyNaCl 导入 Lambda Function - 没有名为“_cffi_backend”的模块 - Importing PyNaCl into Lambda Function - No Module named “_cffi_backend” 将 fbprophet 作为 AWS Lambda 层导入 - Importing fbprophet as a AWS Lambda layer AWS python lambda - lambda 中的导入模块与本地项目的区别 - AWS python lambda - difference in importing module in lambda than from local project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM