I have install psycopg2==2.8.6 module in window 10 (python version 3.8.7) and i am able to import in my code without any issues. But when I zipped and upload it to AWS Lambda giving this error. all the library folders in right place and AWS also with python 3.8.7 but not sure why this error. I tried to downgrade the psycopg2=2.8.5 but no luck. Can some help on priority please
2 Answers
Your question implicates that you have downloaded psycopg2 library on Windows, and are trying to use the same library within Lambda runtime environment, which ultimately uses Linux operating system.
AWS Documentation on Lambda Runtime Environment provides more documentation, but to get this working you have few options
- Run amazon provided Linux container, install and zip dependencies in there. You could achieve this by running following in your project folder
$ docker run --rm -v $PWD:/src --entrypoint '' amazon/aws-lambda-python:3.8 bash
# execute below within container
$ pip3 install psycopg2-binary -t /src/lambdalib
Alternatively, you can build extension on Linux platform, within that container using following
$ docker run --rm -v $PWD:/src --entrypoint '' amazon/aws-lambda-python:3.8 bash
# execute below within container
$ yum -y install cmake c++ gcc postgresql-devel && pip3 install psycopg2 -t /src/lambdalib
This will install linux binary version of psycopg2 within your lambdalib folder. I suggest you execute this from cloud console, or Cloud9 IDE if Windows workstation is not having
- Search for publicly available Lambda Layer for
psycopg2, add it to your lambda function, and avoid packaging this library altogether.
We can you AWS Chalice to deploy a lambda function using python3 with requiremetns
requests
psycopg2-binary
Ensure the environment which runs chalice has default python version 3 (we can use symlink ln -s /usr/bin/python3.8 /usr/bin/python)
⚡ $ chalice --version
chalice 1.21.6, python 3.8.5, linux 5.8.0-49-generic
Full solution here: https://dev.to/vumdao/connect-postgres-database-using-lambda-function-1mca