简体   繁体   English

Lambda 未加载加密共享库

[英]Lambda Not Loading Cryptography Shared Library

I'm using the cryptography library in AWS Lambda. I've compiled the package using pip in an Amazon Linux VM.我在 AWS Lambda 中使用加密库。我在 Amazon Linux VM 中使用 pip 编译了 package。 I have uploaded the package as a layer.我已将 package 作为图层上传。 Either way, every time I call the library, I have an error which is not descriptive at all:无论哪种方式,每次我调用图书馆时,我都会遇到一个完全没有描述性的错误:

Unable to import module 'lambda_function': libffi-ae16d830.so.6.0.4: cannot open shared object file: No such file or directory

As you can see, the error is not about not finding the lib, is another shared module which I haven't been able to find.如您所见,错误不是因为找不到库,而是我找不到的另一个共享模块。

Here's an example of the code I'm trying to execute on Lambda:这是我尝试在 Lambda 上执行的代码示例:

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.fernet import Fernet

password_provided = "test123" 
password = password_provided.encode() 
salt = b'test_' 
kdf = PBKDF2HMAC(
    algorithm=hashes.SHA256(),
    length=32,
    salt=salt,
    iterations=100000,
    backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(password)) 


message = "message from db".encode()

f = Fernet(key)
encrypted = f.encrypt(message)

print(encrypted)

f = Fernet(key)
decrypted = f.decrypt(encrypted)

print(decrypted.decode("utf-8"))

It's not the first time I've compiled a library to work on AWS Lambda, but in this case even I compile the cryptography lib.这不是我第一次编译库以在 AWS Lambda 上运行,但在这种情况下,我什至编译了加密库。 What should I add or change?我应该添加或更改什么?

Edit: I've found out the library was missing in the zip file I've created, as is inside a hidden folder.编辑:我发现我创建的 zip 文件中缺少该库,就像隐藏文件夹中一样。 I zipped using '.'我用'.'压缩了instead of '*' but now I'm running with a new problem: When I run the lambda, I get this:而不是“*”,但现在我遇到了一个新问题:当我运行 lambda 时,我得到了这个:

Unable to import module 'lambda_function': /opt/cryptography/hazmat/bindings/_constant_time.so: undefined symbol: PyInt_FromLong

any idea?任何的想法?

Even I faced the same issue, while zipping I forgot to include the hidden files(.libs_cffi_backend) in the site-packages.即使我遇到了同样的问题,在压缩时我也忘记了在站点包中包含隐藏文件(.libs_cffi_backend)。 After Including it, I didn't see this error.包含它之后,我没有看到这个错误。

As the library you're using requires native libraries, you have to pack the native.so files as well with the layer.由于您使用的库需要本机库,因此您必须将 native.so 文件与层一起打包。 I ran into a similar issue while trying to run wkhtmltopdf on aws lambda.我在 aws lambda 上尝试运行 wkhtmltopdf 时遇到了类似的问题。

The binaries for the library has to be compiled in the same environment as a lambda instance.库的二进制文件必须在与 lambda 实例相同的环境中编译。 Lambda gets booted up using AWS Linux. Lambda 使用 AWS Linux 启动。

You can either boot up an EC2 running AmazonLinux or use docker, easiest way is to boot up a docker container.您可以启动运行 AmazonLinux 的 EC2 或使用 docker,最简单的方法是启动 docker 容器。

$ sudo docker run -it amazonlinux bash

Now you need to download/unpack all.so files into a directory then zip it.现在你需要下载/解压 all.so 文件到一个目录然后 zip 它。 Also, make sure to keep all.so files inside a folder called lib inside the zip. After zipping, the zip should look something similar to this:此外,请确保将 all.so 文件保存在 zip 中名为 lib 的文件夹中。压缩后,zip 应类似于以下内容:

.
├── lib
│   ├── libcrypto.so.10
│   ├── libcrypto.so.1.0.2k
│   ├── libfontconfig.so.1
│   ├── libfontconfig.so.1.7.0
.......

Then you can just zip it and upload it as a layer.然后你可以只 zip 它并将其作为图层上传。 It will be uploaded to /opt/ in your Lambda Container.它将上传到您的 Lambda 容器中的 /opt/。 AWS looks for library files under /opt/lib amongst many other locations . AWS 在许多其他位置中查找 /opt/lib 下的库文件。

The challenging part for you would be to figure out how to get all the required.so files in order for your dependency to run properly.对您来说具有挑战性的部分是弄清楚如何获取所有必需的 .so 文件以使您的依赖项正常运行。

Previously I manually packed the archive, deployed with aws cloudformation deploy , and had the same problem.以前我手动打包存档,使用aws cloudformation deploy ,但遇到了同样的问题。 I then switched to using sam build --use-container and it worked.然后我切换到使用sam build --use-container并且它起作用了。 My suspicion is that it works because SAM uses a specific container.我怀疑它之所以有效,是因为 SAM 使用了特定的容器。 This is the container it uses: https://gallery.ecr.aws/sam/build-python3.7这是它使用的容器: https://gallery.ecr.aws/sam/build-python3.7

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM