Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda issue with psycopg2

I'm trying to import psycopg2 into my python 3.6 lambda function but I keep running into the error message 'Unable to import module 'lambda_function': /var/task/psycopg2/_psycopg.cpython-36m-x86_64-linux-gnu.so: ELF file's phentsize not the expected size'

I'm uploading the zip file directly into the lambda function through the AWS console. I've tried using https://github.com/jkehler/awslambda-psycopg2 library but it's given me the same message.

I've been able to successfully run my script on an EC2 instance (Amazon Linux AMI 2018.03.0 (HVM)), and I've even tried uploading the EC2 instance psycopg2 library onto lambda, but I still keep getting the same error message.

Any help would be appreciated.

like image 670
Yitzak Hernandez Avatar asked Jul 26 '18 19:07

Yitzak Hernandez


1 Answers

I ran into this issue as well when trying to upload the .zip containing my Lambda. The issue for me was that I was compressing my Lambda from one directory above.

The solution was running the zip command within the directory containing the Lambda function itself as opposed to running zip from its parent directory.

For example, if your lambda function exists within the directory ~/lambda as lambda_function.py along with any additional libraries (i.e. multiprocessing) - you should zip your Lambda Deployment Package by doing the following.

$ cd ~/lambda
$ zip lambda_function.zip lambda_function.py
$ zip -r lambda_function.zip multiprocessing* 

Uploading the resulting .zip file should resolve this issue.

like image 174
A. McLean Avatar answered Sep 28 '22 10:09

A. McLean