1

I'm trying to use this tutorial to upload a docker container to AWS ECR for Lambda. My problem is that my python script uses psycopg2, and I couldn't figure out how to install psycopg2 inside the Docker image. I know that I need postgres-devel for the libq library and gcc for compiling, but it still doesn't work.

My requirements.txt:

pandas==1.3.0
requests==2.25.1
psycopg2==2.9.1
pgcopy==1.5.0

Dockerfile:

FROM public.ecr.aws/lambda/python:3.8

WORKDIR /app

COPY my_script.py .
COPY some_file.csv .
COPY requirements.txt .

RUN yum install -y postgresql-devel gcc*
RUN pip install -r requirements.txt

CMD ["/app/my_script.handler"]

After building, running the image, and testing the lambda function locally, I get this error message:

psycopg2.OperationalError: SCRAM authentication requires libpq version 10 or above

So I think the container has the wrong version of postgres(-devel). But I'm not sure how to install the proper version? Any tips for deploying a psycopg2 script to docker for lambda usage?

shimo
  • 2,156
  • 4
  • 17
  • 21
Attila Toth
  • 187
  • 1
  • 2
  • 13
  • 2
    You can always try switching out `psycopg2` in your `requirements.txt` with `psycopg2-binary`. That should come prepackaged with all the needed libraries. – Kyle Parsons Aug 09 '21 at 14:41
  • @KyleParsons thank you that actually solves my problem. I was just wondering because [in their docs](https://www.psycopg.org/docs/install.html#psycopg-vs-psycopg-binary), they say not to use the binary version in production – Attila Toth Aug 09 '21 at 14:59
  • Yeah, I don't have insight into why they recommend building from source over using the binary. I've had some success with `psycopg2-binary` but YMMV. – Kyle Parsons Aug 09 '21 at 15:55

1 Answers1

4

This might be a little old and too late to answer but figure I post what worked for me.

FROM public.ecr.aws/lambda/python:3.8

COPY . ${LAMBDA_TASK_ROOT}

RUN yum install -y gcc python27 python27-devel postgresql-devel

RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

CMD [ "app.handler" ]
Josh Smith
  • 111
  • 1
  • 2