Compiling Postgresql Static libpq library for use in AWS Lambda

  • skeeved's picture
  • Posted on: 8 September 2017
  • By: skeeved

AWS Lambda uses AWS Linux as the execution environment. All code/libraries should be build using that environment as well.

  • Download latest postgresql source code from https://ftp.postgresql.org/pub/source/...
  • Download latest psycopg2 source code from http://initd.org/psycopg/download
  • Extract postgresql code into a directory (tar xvzf ...)
    • ./configure --prefix /home/ec2-user/postgresql-9.4.9 --without-readline --without-zlib && make && make install
    • Enter the psycopg2 source directory and edit the setup.cfg file with the following:


pg_config=/bin/pg_config
static_libpq=1

  • Execute python setup.py build in the psycopg2 source directory

We use virtualenv to create the run-time environment for our lambda function. All of our code would go in the root of the new virtualenv:

  • Create a new virtualenv


virtualenv path/to/my/virtual-env

  • Activate the new virtualenv


source path/to/my/virtual-env/bin/activate

  • Install any required packages


pip install requests
pip install simplejson
pip install pytz

  • To install the statically compiled version of psycopg2, copy it from the build dir:


rsync -av ~/psycopg2-2.6.2/build/lib.linux-x86_64-2.7/psycopg2/ lib64/python2.7/site-packages/psycopg2/

  • Create the zip file:


#!/bin/bash
cd ~/lambda/domains
zip -9 domains.zip domains.py

cd ~/lambda/domains/lib/python2.7/dist-packages
zip -r9 ~/lambda/domains/domains.zip *

cd ~/lambda/domains/lib/python2.7/site-packages
zip -r9 ~/lambda/domains/domains.zip *

  • Upload the zip file to AWS