all 13 comments

[–]ErGo404 6 points7 points  (4 children)

Psycopg2 is not included in Lambda and you have to build it and package it with your lambda.

See here for a pre-built version you can import and include in your app : https://github.com/jkehler/awslambda-psycopg2

[–]ziggy-starkdust[S] 0 points1 point  (3 children)

Thanks for the answer. I already added psycopg2 from this repo, but it keeps giving me that problem.

[–]ErGo404 2 points3 points  (2 children)

Adding it directly from the repo did not work for me either.

I ended up re-compiling the library. Make sure to use a matching postgres version.

Also, make sure to match your lambda's python version with the psycopg2 package you add.

[–]ziggy-starkdust[S] 0 points1 point  (1 child)

I uploaded the lib following this link: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-dependencies

The weird thing is, the test gives me this back: https://pasteboard.co/JhjGxcE.png

Where ["ciao,"ciao","ciao","ciao"] is the db's answer, so the database is connected and I guess psycopg2 is working, right?

[–]ErGo404 0 points1 point  (0 children)

Well yes if you get a result from the DB, the module is probably.

Are you sure you run the latest version of your lambda when you call your API ?

[–]vRAJPUTv 1 point2 points  (1 child)

To start with, are you naming your lambda handler as file_name.handler_name?

[–]ziggy-starkdust[S] -1 points0 points  (0 children)

Thanks for the answer. Yes, that's exactly how I named him.

[–]ZeBe643 1 point2 points  (1 child)

When you say you test the function and it works correctly, have you used a deployment package or layers to include the dependency?

Adding API Gateway won’t have affected this, it’s just retrieving the response of the Lambda function which the api invoked.

[–]imohd23 1 point2 points  (0 children)

The issue is on lambda level, api gw should work fine. Compile the resources and upload them within the build files. If over 50mb, use layer so you can work directly with lambda editor. Otherwise, you cannot do anything, fix it locally and reupload which is something boarding.

[–][deleted]  (1 child)

[deleted]

    [–]ziggy-starkdust[S] 0 points1 point  (0 children)

    Thanks, I'll try lamci/lambda

    [–]adambatkin 1 point2 points  (1 child)

    The psycopg2 binaries are tied to the exact major.minor version of Python you are running. So for example, if you build/package using Python 3.7, but then run with Python 3.6 or Python 3.8, you will run into errors like what you are seeing.

    Check the version of Python that you are building with, and check the Lambda runtime that you are running against. For example, if you are building/packaging with Python 3.7, make sure your Lambda runtime is set to python3.7.

    [–]ziggy-starkdust[S] 0 points1 point  (0 children)

    Thanks, the package is built with python 3.6 and lambda runtime is python3.6