I am trying to import the Psycopg2 library into a Lambda Function, but every time it continues to fail, I have tried pip installing the library then zipping it with the .py file, it fails… with “”Unable to import module ‘lambda_function’: No module named ‘psycopg2._psycopg'”. Then I tried to use Docker and create a container to identical to the runtime of lambda, and it failed with the same error message. I even tried to create my own layer and it still continues to fail, here is the code for importing and getting a connection (which it never makes it too):
I also tried with Psycopg-binary which should counter any compatibility issues with the lambda runtime but it still fails.
import sys
import psycopg2
import boto3
import json
import os
try:
connection = psycopg2.connect(host = os.environ["host"],
user= os.environ["username"],
password = os.environ["password"],
dbname = os.environ["dbname"],
port = os.environ["port"])
# except psycopg2.Error as e:
print(f"Connection Attempt Failed, error: {e}")
Things I have tried:
- Tried zipping the .py file and the libraries
- Tried Docker and getting the right versions of everything.
- Tried using Psycopg-binary
- Tried making and using a layer with Psycopg2
- Tried using an already community built layer for psyocpg2
Did all of these things, and it still does not work.
Reece Tredwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1