lambda python function with external library psycopg2 - No module named psycopg2
<p>Error Message - No module named psycopg2<br>
File used in zip - <a href="https://github.com/jkehler/awslambda-psycopg2" rel="nofollow">https://github.com/jkehler/awslambda-psycopg2</a> <br>
Code Snippet -<br></p>

<pre><code>    #!/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();
</code></pre>

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

<p><strong>EDIT</strong>
<br>Recreated the package with zipping the file on Amazon Linux. Still same error.</p>

<p>psycopg2 is a compiled module. Copying the Windows version won't work because Lambda runs on top of Amazon Linux. According to <a href="https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/" rel="nofollow">the docs</a> 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.</p>

python python-2.7 lambda aws-lambda amazon-lambda