1
votes

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.

enter image description here

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.

enter image description here

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

4
I would strongly suggest always using virtual environments when developing lambda functions. Tutorials on usage are out there. This allows you to build and test your software with exactly the packages you need. Then, you can just copy the contents of pip --version (run with the virtualenv activated) into the top directory of your uploaded zip file, and make sure you're getting every (Python) file you need. - T.C. Proctor
Unfortunately, it appears that getting all the Python files you need doesn't seem to cover your bases on some software that is dependent on the system files. @OmarZairi's answer shows you a work around that should be pretty universal when files that aren't in your virtualenv's site-packages directory are showing up in errors. - T.C. Proctor
Just realized that using pip --version to identify the location of your virtualenv's site-packages directory might not work if you're not using Pipenv or virtualenvwrapper. You should probably use one of those anyway. - T.C. Proctor
Here's info on the virtualenv strategy from Amazon's (frankly, generally terrible) official documentation. - T.C. Proctor
If you spend a bunch of time creating Lambda processes, it might be worthwhile to test them in a virtual environment created in the same environment as the lambda process, which can be done by creating an EC2 instance using the official image. - T.C. Proctor

4 Answers

5
votes

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>
1
votes

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.

P.S. I wonder why there is no official recommendations about suggested MySQL driver on amazon. At least I haven't found it.

1
votes

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.

0
votes

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.