简体   繁体   English

将Python软件包部署到AWS Lambda时出错

[英]Error deploying Python package to AWS Lambda

AWS Successfully creates the lambda function when I upload the zip file. 当我上传zip文件时,AWS成功创建了lambda函数。 But it's giving this error when I test it out. 但是当我测试它时,它给出了这个错误。

{
  "errorMessage": "Unable to import module 'amazonSNS'"
}

Following are the contents of the zip file that I created. 以下是我创建的zip文件的内容。 I tried changing the name of the zip file to "amazonSNS" to match the amazonSNS.py file, but no help, same issue. 我尝试将zip文件的名称更改为“ amazonSNS”,以匹配amazonSNS.py文件,但没有帮助,同样的问题。

在此处输入图片说明

The Lambda handler in the Configuration of the Lambda function is set to "amazonSNS.handler" where amazonSNS is the filename and handler is the function name that needs to be called, as they have instructed in the documentation. Lambda函数的配置中的Lambda处理程序设置为“ amazonSNS.handler”,其中amazonSNS是文件名,而handler是需要调用的函数名,如文档中所指示。

Here are the contents of the python file 这是python文件的内容

import boto3
import MySQLdb

client = boto3.client("sns")
rds = boto3.client("rds")

def handler(event, context):
    conn = MySQLdb.connect("host", "username", "password", "database")
    cur = conn.cursor(MySQLdb.cursors.DictCursor)
    query = "select * from login.login limit 10"
    cur.execute(query)
    print cur.fetchall()
    print conn

What might be the issue here? 这里可能是什么问题?

Here is the Log output 这是日志输出

START RequestId: 76a61551-052a-11e6-b466-8fa0769ac309 Version: $LATEST Unable to import module 'amazonSNS': No module named _mysql START RequestId:76a61551-052a-11e6-b466-8fa0769ac309版本:$ LATEST无法导入模块'amazonSNS':没有名为_mysql的模块

END RequestId: 76a61551-052a-11e6-b466-8fa0769ac309 REPORT RequestId: 76a61551-052a-11e6-b466-8fa0769ac309 Duration: 0.33 ms Billed Duration: 100 ms END RequestId:76a61551-052a-11e6-b466-8fa0769ac309 REPORT RequestId:76a61551-052a-11e6-b466-8fa0769ac309持续时间:0.33 ms计费持续时间:100 ms

UPDATE 更新

I added a few more files from "site-package" folder that I thought was part of the MySQLdb package, Here are the current contents of the zip file. 我从“ site-package”文件夹中添加了一些其他文件,这些文件我认为是MySQLdb软件包的一部分,这是zip文件的当前内容。

在此处输入图片说明

And after this the new Log of the error is. 在此之后,将出现新的错误日志。

START RequestId: c0715d9a-0531-11e6-9409-a3b194fd4afd Version: $LATEST Unable to import module 'amazonSNS': libmysqlclient.so.18: cannot open shared object file: No such file or directory START RequestId:c0715d9a-0531-11e6-9409-a3b194fd4afd版本:$ LATEST无法导入模块'amazonSNS':libmysqlclient.so.18:无法打开共享对象文件:没有这样的文件或目录

END RequestId: c0715d9a-0531-11e6-9409-a3b194fd4afd REPORT RequestId: c0715d9a-0531-11e6-9409-a3b194fd4afd Duration: 0.35 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 22 MB END RequestId:c0715d9a-0531-11e6-9409-a3b194fd4afd REPORT RequestId:c0715d9a-0531-11e6-9409-a3b194fd4afd持续时间:0.35 ms计费时间:100 ms内存大小:128 MB使用的最大内存:22 MB

To solve this issue: I searched for libmysqlclient.so.20 (the version number at the end may differ) 解决此问题的方法:我搜索了libmysqlclient.so.20(末尾的版本号可能不同)

find /. -name "libmysqlclient.so.20"

My ouput was 我的输出是

/./usr/lib/x86_64-linux-gnu/libmysqlclient.so.20

I then copied that file into the root directory of my package 然后,我将该文件复制到包的根目录中

cp /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20 <your package path>

I had this issue when using mysqlclient (the MySQLd fork which works on Python3). 使用mysqlclient(在Python3上运行的MySQLd fork)时出现了这个问题。

Since I use Zappa for easy deployment, the solution was simple: just switch to the original MySQLd package (which does not support Python 3, though): pip install mysql-python Zappa comes with a pre-compiled version of it. 由于我使用Zappa进行轻松部署,因此解决方案很简单:只需切换到原始MySQLd软件包(尽管它不支持Python 3): pip install mysql-python Zappa附带了它的预编译版本

How did you install MySQLdb? 您是如何安装MySQLdb的? http://mysql-python.sourceforge.net/FAQ.html says: http://mysql-python.sourceforge.net/FAQ.html说:

ImportError: No module named _mysql If you see this, it's likely you did some wrong when installing MySQLdb; ImportError:没有名为_mysql的模块如果看到此消息,则可能是在安装MySQLdb时做错了; re-read (or read) README. 重新阅读(或阅读)自述文件。 _mysql is the low-level C module that interfaces with the MySQL client library. _mysql是与MySQL客户端库连接的低级C模块。

Install MySQLdb with pip if you didn't already. 如果尚未安装,请通过pip安装MySQLdb。

I failed to compile MySQL-python in way to make it work in Lambda. 我无法编译MySQL-python以使其在Lambda中工作。 Instead I switched to pymysql . 相反,我切换到pymysql I am not sure about performance, but at least this works. 我不确定性能,但至少能奏效。

PS I wonder why there is no official recommendations about suggested MySQL driver on amazon. PS我想知道为什么在亚马逊上没有关于建议的MySQL驱动程序的官方建议。 At least I haven't found it. 至少我没有找到它。

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

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