I am facing this error when I try to run psycopg3 on AWS Lambda.
[ERROR] Runtime.ImportModuleError: Unable to import module 'function': no pq wrapper available.
Attempts made:
- couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
- couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary'
- couldn't import psycopg 'python' implementation: libpq library not found
I reference it as a dependency in my .venv
based on this in my setup.py
install_requires=[
'boto3',
'pydantic',
'python-dotenv',
'psycopg',
'twine',
'wheel',
'xlsxwriter'
],
extras_require={
'binary': ['psycopg-binary'],
},
I have copied the installation process as the official site.
2
Answers
So, the solution for me was to remove the
psycopg[binary]
dependency from the main pypi repository package, and only reference it when deploying the lambda, which needs to be done via docker (I suppose once deployment is hooked up to an AWS VM it will be better). The pypi package will only havepsycopg
as a dependency inrequirements.txt
and also needs docker deployment.Here is the code for final deployment of the function:
And then you may upload this zipped file to AWS lambda.
As per my understanding, you will need a binary too.
Some External libraries like GPG, psycopg3 require them to be statically compiled as a binary using Amazon Linux 2 OS to run on amazon lambda.
You can build it using a docker image and zip it and push as lambda layer to be used in lambda.
This process is not straightforward.
Instead I would suggest to take a look at pg8000 driver.
https://github.com/tlocke/pg8000
references to use: https://stackoverflow.com/a/72571215/5235168