'AWS Lambda layer for psycopg2

I'm trying to create a new lambda layer to import the zip file with psycopg2, because the library made my deployment package get over 3MB, and I can not see the inline code in my lambda function any more.

I created lambda layer for the following 2 cases with Python 3.7:

  • psycopg2_lib.zip (contains psycopg2, psycopg2_binary.libs and psycopg2_binary-2.8.5.dist-info folders)
  • psycopg2_only.zip which contains only the psycopg2 folder.

I added they new created layer into my lambda function. But, in both cases, my lambda_function throws an error as follows: { "errorMessage": "Unable to import module 'lambda_function': No module named 'psycopg2'", "errorType": "Runtime.ImportModuleError" }

The error seems as if something went wrong with my zip file that they are not recognized. But when it works well in my deployment package.

Any help or reason would be much appriciated. Thanks!



Solution 1:[1]

not sure if the OP found a solution to this but in case others land here. I resolved this using the following steps:

  1. download the code/clone the git from: https://github.com/jkehler/awslambda-psycopg2
  2. create the following directory tree, if building for python3.7, otherwise replace 'python3.7' with the version choice: mkdir -p python/lib/python3.7/site-packages/psycopg2
  3. choose the python version of interest and copy the files from the folders downloaded in step 1. to the directory tree in step 2. e.g. if building a layer for python 3.7: cp psycopg2-3.7/* python/lib/python3.7/site-packages/psycopg2
  4. create the zip file for the layer. e.g.: zip -r9 psycopg2-py37.zip python
  5. create a layer in the console or cli and upload the zip

Solution 2:[2]

If you end up on this page in >= 2022 year. Use official psycopg2-binary https://pypi.org/project/psycopg2-binary/

Works well for me. Just

pip install --target ./python psycopg2-binary

zip -r python.zip python

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2 none none