简体   繁体   English

脚本在压缩后可在AWS EC2上运行,但不适用于AWS Lambda

[英]Script works on AWS EC2, but not on AWS Lambda after zipping

I am creating a simple AWS Lambda function using M2Crypto library. 我正在使用M2Crypto库创建一个简单的AWS Lambda函数。 I followed the steps for creating deployment package from here . 我遵循了从此处创建部署程序包的步骤。 The lambda function works perfectly on an EC2 Linux instance (AMI). lambda函数可在EC2 Linux实例(AMI)上完美运行。

This is my Function definition: 这是我的功能定义:

CloudOAuth.py CloudOAuth.py

from M2Crypto import BIO, RSA, EVP
def verify(event, context):
  pem = "-----BEGIN PUBLIC KEY-----\n{0}\n-----END PUBLIC KEY-----".format("hello")
  bio = BIO.MemoryBuffer(str.encode(pem))
  print(bio)
  return 

Deployment Package structure: 部署程序包结构:

Lambda部署程序包框架

When I run the Lambda, I get the following issue and I also tried including libcrypto.so.10 from /lib64 directory, but didn't help. 当我运行Lambda时,出现以下问题,我也尝试从/ lib64目录包含libcrypto.so.10,但没有帮助。

Issue when running Lambda 运行Lambda时的问题

/var/task/M2Crypto/_m2crypto.so: symbol sk_deep_copy, version libcrypto.so.10 not defined in file libcrypto.so.10 with link time reference` /var/task/M2Crypto/_m2crypto.so:符号sk_deep_copy,版本libcrypto.so.10未在文件libcrypto.so.10中定义,具有链接时间参考`

Python: 2.7
M2Crypto: 0.27.0

I would guess that the M2Crypto was built with different version of OpenSSL than what's on Lambda. 我猜想M2Crypto是用与Lambda不同的OpenSSL版本构建的。 See the relevant code . 请参阅相关代码 If not (the upstream maintainer speaking here), please, file a bug at https://gitlab.com/m2crypto/m2crypto/issues 如果不是(请上游维护者在这里讲话),请在https://gitlab.com/m2crypto/m2crypto/issues提交错误。

I just want to add some more details on to @mcepl's answer. 我只想在@mcepl的答案中添加更多详细信息。 The most important is that OpenSSL version on AWS Lambda and the environment (in my case ec2) where you build your M2Crypto library should match. 最重要的是,AWS Lambda上的OpenSSL版本与构建M2Crypto库的环境(在我的情况下为ec2)应该匹配。

To check openssl version on Lambda, use print in your handler: 要在Lambda上检查openssl版本,请在处理程序中使用print:

print(ssl.OPENSSL_VERSION)

To check openssl version on your build environment, use: 要在构建环境中检查openssl版本,请使用:

$ openssl version

Once they match, it works. 一旦匹配,它就会起作用。

Don't hesitate to downgrade or upgrade OpenSSL on your build environment to match the Lambda environment. 请不要在您的构建环境上降级或升级OpenSSL以匹配Lambda环境。 I had to downgrade my openssl on ec2 to match lambda runtime environment. 我必须在ec2上降级openssl以匹配lambda运行时环境。

sudo yum -y downgrade openssl-devel-1.0.1k openssl-1.0.1k

Hope it will help anyone trying to use M2Crypto :) 希望它能帮助任何尝试使用M2Crypto的人:)

copying my answer for a similar question here: 在这里复制对类似问题的答案

AWS lambda runs code on an old version of amazon linux ( amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2 ) as mentioned in the official documentation https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html AWS的λ运行的代码在一个旧版本的linux亚马逊的( AMZN-AMI-HVM-2017.03.1.20170812-x86_64的-GP2 )作为官方文件中提到https://docs.aws.amazon.com/lambda/latest/dg/ current-supported-versions.html

So to run a code that depends on shared libraries, it needs to be compiled in the same environment so it can link correctly. 因此,要运行依赖于共享库的代码,需要在同一环境中对其进行编译,以便可以正确链接。

What I usually do in such cases is that I create virtualenv using docker container. 在这种情况下,我通常会使用docker容器创建virtualenv。 The virtualenv can than be packaged with lambda code. 然后可以将virtualenv与lambda代码打包在一起。

Please note that if you need install anything using yum (in the docker container), you must use same release server as the amazon linux version: 请注意,如果您需要使用yum安装任何东西(在Docker容器中),则必须使用与Amazon Linux版本相同的发行服务器:

 yum --releasever=2017.03 install ... 

virtualenv can be built using an EC2 instance as well instead of docker container (though, I find docker method easier). 也可以使用EC2实例而不是docker容器来构建virtualenv(尽管我发现docker方法更容易)。 Just make sure that the AMI used for EC2 is same as the one used by lambda. 只要确保用于EC2的AMI与lambda使用的AMI相同即可。

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

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