简体   繁体   中英

Error deploying Python package to AWS Lambda

AWS Successfully creates the lambda function when I upload the zip file. 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. I tried changing the name of the zip file to "amazonSNS" to match the amazonSNS.py file, but no help, same issue.

在此处输入图片说明

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.

Here are the contents of the python file

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

END RequestId: 76a61551-052a-11e6-b466-8fa0769ac309 REPORT RequestId: 76a61551-052a-11e6-b466-8fa0769ac309 Duration: 0.33 ms Billed Duration: 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.

在此处输入图片说明

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

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

To solve this issue: I searched for libmysqlclient.so.20 (the version number at the end may differ)

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).

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.

How did you install MySQLdb? http://mysql-python.sourceforge.net/FAQ.html says:

ImportError: No module named _mysql If you see this, it's likely you did some wrong when installing MySQLdb; re-read (or read) README. _mysql is the low-level C module that interfaces with the MySQL client library.

Install MySQLdb with pip if you didn't already.

I failed to compile MySQL-python in way to make it work in Lambda. Instead I switched to 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. At least I haven't found it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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