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:
Did all of these things, and it still does not work.
You can create a lambda layer. I've created a layer for psycopg2 myself a few months ago. Without details it is hard to say what was wrong with your attempt to create a lambda layer. As for a community built layer maybe it was for a different python minor version, hard to say.
Here is how to create a layer.
You need a machine with AWS 2023 Linux operational system. Any Linux version is not going to work (ubuntu will not work, for example), and do not waste time on attempts to use files from python installed on Windows. You can launch a free-tier EC2 instance with AWS 2023 Linux.
Install python of exactly the version you are going to use. Then install the psycopg2.
Create a zip archive of exactly the following folder structure:
file.zip\python\lib\python3.11\site-packages...
This will only work for python 3.11. 4. Create a lambda layer using the zip file.
As for me, it took time to create a package file, and it only worked after a few unsuccessful attempts, as i tried to use files from ubuntu installation, and then I had to fix a few times folder structure.
P.S. You also use wrong syntax for extracting environment variables. It should be just:
user=os.environ['username']