Since I'm using the new arm64 MacBook Pro, the solution of installing pysqlite3-binary
is not very feasible for me. I also had to work with a controlled pipeline that only recognizes Pipfile.lock
but not the Pipfile
so the other solution also doesn't work for me. Instead I found a way to compile libsqlite3.so.0
from the source code and use it to override the default one on AWS Lambda.
First you want to compile sqlite amalgamation from the source code. I created this Dockerfile
so we can easily compile for linux/amd64 from arm64 machine:
FROM amazonlinux:2
RUN yum install -y tar gzip gcc make
ARG SQLITE_VERSION=3430200
RUN curl -sSLO https://www.sqlite.org/2023/sqlite-autoconf-${SQLITE_VERSION}.tar.gz
RUN tar xf sqlite-autoconf-${SQLITE_VERSION}.tar.gz; \
mv /sqlite-autoconf-${SQLITE_VERSION} /sqlite-autoconf; \
cd /sqlite-autoconf; \
./configure; \
make
Now we can build the Docker image with command docker build --platform linux/amd64 -t sqlite3-builder -f Dockerfile .
The last make
command will put the libsqlite3.so.0
under the directory /sqlite-autoconf/.libs
. We can then use docker cp
to extract it:
id=$(docker create sqlite3-builder)
docker cp $id:/sqlite-autoconf/.libs/libsqlite3.so.0.8.6 lib/libsqlite3.so.0
docker rm -v $id
Now we can upload this libsqlite3.so.0
file with our other code to AWS Lambda.
The last step is to set an environment variable in AWS Lambda that overrides the system SQLite3. For example, LD_PRELOAD=/var/task/lib/libsqlite3.so.0
. Note that the /var/task
(which is value of LAMBDA_TASK_ROOT
variable), is default path to your Lambda function code.