AWS Lambda is Amazon’s “serverless” compute platform that basically lets you run code without thinking (too much) of servers. I used Lambda in the past, though only in the Node.js environment. Wanting to deploy my first Python function, I ran into a couple of problems.
There are two deployment scenarios:
- Simple scenario
- Advanced scenario
The simple scenario applies to you when your function code only requires the AWS SDK library (Boto 3) and no other external resources. Just use Lambda’s inline code editor and you are good to go. No need to read the rest of this article :-)
In the “advanced” scenario your code does have dependencies that need to be imported and are not available in the Lambda environment. You need to upload your dependencies in a zip file via the AWS console, CLI or through S3.
AWS describes both scenarios and the upload process in the Developer Guide: Creating a Deployment Package (Python)
After reading the guide and uploading the code including the dependencies I still had problems. To save you the trouble, here are some resolutions to what I think are common issues.
No module named lambda_function
Unable to import module 'lambda_function': No module named lambda_function
This error appears when you haven’t named your code file or function right. Go to Lambda -> Functions -> Your Function -> Configuration and check the value in the Handler field.
The format is fileName.handlerMethod.
So in this example my function is called lambda.py and the handler function is called handler.
Invalid ELF header
Unable to import module 'lambda': /var/task/lxml/etree.so: invalid ELF header
The problem was that the .so file was compiled on my local machine. This question on stackoverflow lead me to the answer: aws - “Unable to import module…”
To prevent this issue, make sure to spin up an EC2 instance and build your virtualenv on an Amazon Linux. You can find an AMI (Amazon Machine Image) on marketplace: Amazon Linux AMI (HVM / 64-bit).
After building it, download your files via SCP and add them to a zip file including your code file (e.g. lambda.py). As mentioned in the Developer Guide, make sure to put the packages in the root directory of your zip (same level as the function code file).
No module named lxml
Unable to import module 'lambda': No module named lxml
Yep, still not working :-) But this one is an easy one.
I zipped all the packages from
lib/python2.7/site-packages but forgot those in
lib64/python2.7/site-packages. Including these in the zip file fixed this last issue.