Rahul Gupta
Rahul Gupta

Reputation: 1802

lambda python function with external library psycopg2 - No module named psycopg2

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.

Upvotes: 1

Views: 1064

Answers (2)

Sarath Chandra
Sarath Chandra

Reputation: 109

AWS lambda doesn't have all the Python libraries that are in PIP, it is the minimalist amazons python to improve the execution time. So if you want to use other libraries you need to download the source version of them.

You can find them in pypi.org psycopg2

Here is the reference document that might help you. How to create aws lambda layers.

This page shows the detailed step by step on how to create layers.

Upvotes: 0

Paul Kehrer
Paul Kehrer

Reputation: 14089

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.

Upvotes: 1

Related Questions