Home > Mobile >  Install newer version of sqlite3 on AWS Lambda for use with Python
Install newer version of sqlite3 on AWS Lambda for use with Python

Time:08-17

I have a Python script running in a Docker container on AWS Lambda. I'm using the recommended AWS image (public.ecr.aws/lambda/python:3.9), which comes with SQLite version 3.7.17 (from 2013!). When I test the container locally on my M1 Mac, I see this:

$ docker run --env-file .env --entrypoint bash -ti my-image
bash-4.2# uname -a
Linux e9ed14d35cbe 5.10.104-linuxkit #1 SMP PREEMPT Thu Mar 17 17:05:54 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
bash-4.2# sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668

However, I use newer SQLite features, so I need to find a way to use a newer version of the library. The most straightforward solution would be to install a binary package as enter image description here

So I needed to do three things:

  1. Change my Pipfile to conditionally install the binary package only on x86_64:

    pysqlite3-binary = { version = "*", platform_machine = "== 'x86_64'" }
    
  2. Tweak the sqlite import, as described in the original answer:

    try:
        import pysqlite3 as sqlite3
    except ModuleNotFoundError:
        import sqlite3  # for local testing because pysqlite3-binary couldn't be installed on macos
    
    print(f"{sqlite3.sqlite_version=}")
    
  3. Set my Docker container to launch in x86 emulation mode locally.

    $ DOCKER_DEFAULT_PLATFORM=linux/amd64 docker build -t my-image .
    $ DOCKER_DEFAULT_PLATFORM=linux/amd64 docker run -ti my-image
    

Et, voilà!

sqlite3.sqlite_version='3.39.2'
  • Related