Psycopg2 layer

From Kautepedia
Jump to navigation Jump to search

Background[edit | edit source]

Postgres instance connectivity is a critical component of our current pipeline. This library however is not available by default in AWS Lambda which is quite a PITA.

Current version[edit | edit source]

As at August 2024, there are two psycopg layers available:[1]

Layer name Psycopg version Python runtime
psycop9 1.3.8 3.9
20240828_psycopg2[2] 3.2 3.12

Obviously layer psycop9 is more or less EOL and should not be implemented in new functions.[3]

Layer 20240828_psycopg2 is the most current available at the time of writing and should be used by default for any Postgres connection.

Updating/making new layers[edit | edit source]

For psycop specifically, but also generally, the following steps are necessary to make a functional layer for use in AWS Lambda:

  1. Create a new folder and navigate to it
  2. Create requirements.txt specifying anything necessary. For psycopg2: echo 'psycopg2-binary' > requirements.txt
  3. Install required library locally using the following filepath exactly: pip install -r requirements.txt -t python/lib/python3.12/site-packages/ --upgrade.[4]
    1. The filepath /lib/python3.12/site-packages/ is super duper important!!
  4. Zip up the dir you just made: zip -r amazing.zip python
  5. $$$

Above instructions/examples for linux only - no idea how to do any of this on Windows or Mac.

TODO[edit | edit source]

References[edit | edit source]

  1. ↑ Note that psycopg2 is a legacy term that refers to major version 2 of the library. Version 3 was released in October 2021, and is called as psycopg instead. Unfortunately this syntax is used a bit interchangeably for historic reasons, so double check to avoid any confusion.
  2. ↑ There are multiple versions - this relates to the latest version only.
  3. ↑ Existing functions should also be transitioned from this as soon as feasible.
  4. ↑ Python version in this example should be amended depending on version available on your local machine AND the intended Lambda runtime.
  5. ↑ Probably just security or LTS releases but yeah nah.