Why does Amazon make lambda functions so hard to create? Well my guess is that when you live at the bleeding edge you should expect to get scrapes!!
Everybody is trying to build lambda functions these days. And it’s no wonder. Once you get them running, Amazon takes care of all the infrastructure drudge work! So cool.
Join 32,000 others and follow Sean Hull on twitter @hullsean.
I’ve been spending some time trying to get answers out of AWS support, and let me tell you it’s no fun. Yes all this stuff is new technology, and nobody has expertise in it in the way say you might in Linux or Oracle or another technology that’s been around for a decade.
Still you’d hope the techs would have some clue. In the end it was a slog dealing with support, and I think I was the one teaching them!
I did find Matt Perry’s howto which is pretty good.
Hopefully my own notes can help someone, so read on!
1. No lambda_function?
The very first issue you’re gonna run into is if you name the file incorrectly, you get this error:
Unable to import module 'lambda_function': No module named lambda_function
If you name the function incorrectly you get this error:
Handler 'handler' missing on module 'lambda_function_file': 'module' object has no attribute 'handler'
On the dashboard, make sure the handler field is entered as function_filename.actual_function_name and make sure they match up in your deployment package.
If only the messages were a bit more instructive that would have been a simpler step, but oh well!
2. No module named MySQLdb
This is a very tricky one. I mean after all you just spent all this time building your deployment package specifically for lambda, so what gives??
"Unable to import module 'lambda_function': No module named MySQLdb"
Turns out when you use a virtualenv, files will be installed into proj/lib/python2.7/site-packages/ or lib64. However Lambda wants them in the root proj/ directory! So move them there. I know I know. Weird, but that’s what they want.
3. Can’t find libmysqlclient
If you’re using the MySQLdb library like I was, you’ll eventually bump into this error:
Unable to import module 'lambda_function': libmysqlclient.so.18: cannot open shared object file: No such file or directory
Turns out that /usr/lib/libmysqlclient.so.18 needs to be COPIED from /usr/lib. Don’t do “mv” or your system won’t have the mysql lib anymore!
Related: Are SQL databases dead?
4. Use the Amazon Lambda environment
One thing the support pointed out is that AWS as *supported images* for lambda development.
After all the errors above were resolved, it’s not clear to me that the supported AMI’s are truly required. However if you’re hitting intractable problems building a properly lambda deploy, you might wanna look at building one of these boxes.
5. Build your lambda deploy package
Now let’s roll it all together. Here’s are all the steps to build your deploy package.
- SSH to the instance
- mkdir test
- virtualenv test
- source proj/bin/activate
- sudo yum groupinstall 'Development Tools'
- sudo yum install mysql
- sudo yum install mysql-devel
- pip install MySQL-python
- cd test
- emacs -nw lamdba_function.py
- add your code to that file
- save the lambda_function.py
- mv proj/lib/python2.7/site-packages/* proj/
- mv proj/lib64/python2.7/site-packages/* proj/
- rm -rf proj/lib (don't need dist-packages in the deploy pkg)
- rm -rf proj/lib64 (don't need dist-packages
- zip -r proj.zip *
6. Upload your code
Uploading your code via the AWS dashboard is fine when you’re first testing things. But after a while it’ll get tiring going in the front door.
Create a new lambda function by specifying the basics as follows:
aws lambda create-function \
--function-name testfunc1 \
--runtime python2.7 \
--role arn:aws:iam::996225510001:role/lambda_basic_execution \
--handler lambda_function_file.handler_name \
And when you want to update your function, do the following:
aws lambda update-function-code \
--function-name testfunc1 \
Good luck with lambda. Once you get past Amazon’s weak documentation it’s pretty cool to be in a serverless computing environment. Happy deploying!