Skip to content

Instantly share code, notes, and snippets.

@brentonmallen1
Last active February 20, 2024 13:13
Show Gist options
  • Select an option

  • Save brentonmallen1/17ac079204e6f084e4a6d6761c270bb8 to your computer and use it in GitHub Desktop.

Select an option

Save brentonmallen1/17ac079204e6f084e4a6d6761c270bb8 to your computer and use it in GitHub Desktop.
Build psycopg2 for AWS Lambda

This gist is meant to show how to build psycopg2 for use in an AWS Lambda function using Docker. It is influenced by https://github.com/jkehler/awslambda-psycopg2.

Build using Docker

  1. Edit the version environment variables at the top of the Dockerfile as needed.

  2. Build the docker image using docker build -t lambda-psycopg2:latest . in the directory with the DockerFile.

  3. Once the docker image is created, start the container using docker run -it lambda-psycopg2:latest /bin/bash. In this terminal, you can navigate to the /var/output directory to inspect the psycopg2 package.

  4. While the container is running, in a separate terminal, copy over the resulting psycopg2 package to the host machine using docker cp <container id>:/var/output/psycopg2 . The container ID can be obtained using docker container ls.

  5. Terminate the container by exiting the running shell.

Note: If you want to build psycopg2 with ssl, uncomment the # build postgres with ssl section and the following sed line:

sed -i 's/libraries =/libraries = ssl crypto/g' setup.cfg

and comment out the # build without ssl section.

FROM lambci/lambda:build-python3.7
ENV \
POSTGRES_VER=11.4 \
PSYCOPG_VER=2.8.3 \
PSYCOPY_MAJ_VER=2.8 \
PSYCOPG_VER_PATH=PSYCOPG-2-8 \
PYTHON_VER=3.7
RUN yum install -y wget tar postgresql-devel
#setup output dir for library extraction
RUN mkdir -p /var/output
# PSYCOPG2
WORKDIR /var/psycopg
RUN wget -nv https://ftp.postgresql.org/pub/source/v${POSTGRES_VER}/postgresql-${POSTGRES_VER}.tar.gz
RUN tar -zxf postgresql-${POSTGRES_VER}.tar.gz
RUN wget -nv http://initd.org/psycopg/tarballs/${PSYCOPG_VER_PATH}/psycopg2-${PSYCOPG_VER}.tar.gz
RUN tar -zxf psycopg2-${PSYCOPG_VER}.tar.gz
# build postgres with ssl
#RUN cd postgresql-${POSTGRES_VER} && \
#./configure --prefix /var/psycopg/postgresql-${POSTGRES_VER} --without-readline --without-zlib --with-openssl && \
#make && \
#make install
# build without ssl
RUN cd postgresql-${POSTGRES_VER} && \
./configure --prefix /var/psycopg/postgresql-${POSTGRES_VER} --without-readline --without-zlib && \
make && \
make install
# build psycopg2
# need to replace some values in the config file so they point to postgres install and enable ssl
RUN ls
RUN cd psycopg2-${PSYCOPG_VER} && \
sed -ie "s/pg_config =/pg_config = \/var\/psycopg\/postgresql-$POSTGRES_VER\/bin\/pg_config/g" setup.cfg && \
sed -i 's/static_libpq = 0/static_libpq = 1/g' setup.cfg && \
#sed -i 's/libraries =/libraries = ssl crypto/g' setup.cfg
RUN cat psycopg2-${PSYCOPG_VER}/setup.cfg
RUN ls /var/psycopg/postgresql-${POSTGRES_VER}
RUN cd psycopg2-${PSYCOPG_VER} && python setup.py build
# copy compiled library to output to deliever to host
RUN ls
RUN cp -r /var/psycopg/psycopg2-${PSYCOPG_VER}/build/lib.linux-x86_64-${PYTHON_VER}/psycopg2 /var/output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment