1
votes

Error Message - No module named psycopg2
File used in zip - https://github.com/jkehler/awslambda-psycopg2
Code Snippet -

    #!/usr/bin/python
import psycopg2
import sys
import pprint
import datetime

def lambda_handler(event, context):

#Connect to RedShift
conn_string = "dbname='XXXX' port='5439' user='XXX' password='XXXX' host='XXXXXXXXXXXX'";

conn = psycopg2.connect(conn_string);
cursor = conn.cursor();

cursor.execute("begin transaction");
cursor.execute("truncate table XXXX");
cursor.execute("truncate table XXXX");
cursor.execute("truncate table XXXX");
cursor.execute("delete from XXXX");
cursor.execute("insert into XXXX");
cursor.execute("truncate table XXXX");
cursor.execute("truncate table XXXX");
cursor.execute("truncate table XXXX");
cursor.execute("end transaction");

conn.commit();
conn.close();


Extracted and Copied psycopg2 in windows into my AWS Lambda zip package along-with my python file and site packages.
Did I miss anything?

EDIT
Recreated the package with zipping the file on Amazon Linux. Still same error.

1
That's not valid python code. Also: if you want to "bundle" together packages you should freeze your application instead of manually copying files. - Bakuriu
@Bakuriu - Can you please elaborate where did I mess up in my python code? I am new to python and this code executes on my windows machine. - Rahul Gupta
Perhaps he's referring to the lack of indentation, which is significant in Python. - Jeff Learman
@JeffLearman - Maybe, but I do have indentation in my source code. - Rahul Gupta

1 Answers

1
votes

psycopg2 is a compiled module. Copying the Windows version won't work because Lambda runs on top of Amazon Linux. According to the docs it is possible to run native executables and libraries on lambda, but you'll need to find a way to build psycopg2 for that platform and statically link all its libraries or bundle the dynamic libs. This is likely to be challenging unless you're familiar with the C and Python toolchain or someone else has already done it for you.