Saturday, March 12, 2016

Problems using MySQL with AWS Lambda in Python

Leave a Comment

I am trying to get up and running with AWS Lambda Python (beginner in Python btw) but having some problems with including MySQL dependency. I am trying to follow the instructions here on my Mac.

For step number 3, I am getting some problems with doing the command at the root of my project

sudo pip install MySQL-python -t / 

Error:

Exception: Traceback (most recent call last): File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main status = self.run(options, args) File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 311, in run os.path.join(options.target_dir, item) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 292, in move raise Error, "Destination path '%s' already exists" % real_dst Error: Destination path '/MySQL_python-1.2.5-py2.7.egg-info/MySQL_python-1.2.5-py2.7.egg-info' already exists

I end up writing my following lambda function, which is:

import MySQLdb  def lambda_handler(event, context):    # Open database connection    db = MySQLdb.connect(...)     # prepare a cursor object using cursor() method    cursor = db.cursor()     sql = "SELECT * FROM Users"     try:       # Execute the SQL command       cursor.execute(sql)       # Fetch all the rows in a list of lists.       results = cursor.fetchall()       for row in results:          fname = row[0]          lname = row[1]          age = row[2]          sex = row[3]          income = row[4]          # Now print fetched result          print ("lname=%s" %(lname))    except:       print "Error: unable to fecth data"     # disconnect from server    db.close() 

What I went on to do is go to /Library/Python/2.7/site-packages and copying over the the MySQLdb folders/files that were downloaded when I did sudo pip install MySQL-python (without -t /) (I'm sure I'm doing something wrong here), to my lambda project, and then zipped the content along with the lambda_function.py and uploaded to AWS Lambda.

Then I get:

Unable to import module 'lambda_function': No module named MySQLdb

Grateful for any help and suggestions!

EDIT

Was able to do make sudo pip install MySQL-python -t /pathToProject work but now I get this when runing the lambda function:

Unable to import module 'lambda_function': /var/task/_mysql.so: invalid ELF header

1 Answers

Answers 1

I believe your issue is mostly down to missing development packages. I think you will need the following:

sudo yum -y install mysql-devel

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment