Search code examples
pythondockerubuntuaws-lambdapython-cryptography

Python AWS Lambda in error because of pyjwt[crypto] (cryptography)


I have the following error when i run my AWS lambda under python 3.9 :

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': /lib64/libc.so.6: version `GLIBC_2.28' not found (required by /var/task/cryptography/hazmat/bindings/_rust.abi3.so)
Traceback (most recent call last):

I am aware that this is somehow a compilation issue so here the steps i have done until the AWS lambda deployment :

  • Create a Dockerfile :
# syntax=docker/dockerfile:1
FROM ubuntu:latest
ENV TZ=Europe/Paris
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update -y
RUN apt-get install software-properties-common -y
RUN add-apt-repository ppa:deadsnakes/ppa
# Install py39 from deadsnakes repository
RUN apt-get install python3.9 -y
# Install pip from standard ubuntu packages
RUN apt-get install python3-pip -y
RUN apt-get install zip -y

RUN apt-get install libc6 -y

RUN mkdir /home/packages

RUN pip install --target=/home/packages pyjwt[crypto]==2.6.0 
RUN pip install --target=/home/packages pymongo[srv]==4.3.3
  • Inside the docker container, i do : cd /home/packages
  • Then : zip -r ../package.zip .
  • Then i use docker cp to copy the package.zip to my MacOS host.

I use zip -g package.zip lambda_function.py and i upload the .zip file using boto3.

enter image description here

I would like to know why this is not enough or what am i missing here ?

Note : i need to keep using the zip method to upload the lambda package for other reasons, unless there is no other choice of course..

EDIT : In response to @Tom 's answer if i add the mentioned pip command in my Dockerfile :

 > [12/14] RUN pip install --platform --platform manylinux2014_x86_64 --implementation cp --python 3.9 --only-binary=:all: --upgrade --target /home/packages cryptography:                                                      
#19 3.185 ERROR: Could not find a version that satisfies the requirement manylinux2014_x86_64 (from versions: none)
#19 3.186 ERROR: No matching distribution found for manylinux2014_x86_64

Solution

  • You're installing pyjwt[crypto] in an Ubuntu container but this environment is going to be totally different to your AWS Lamda environment.

    AWS Lambda does not provide the .so libraries that the cryptography module is expecting. When you run pip install on an ubuntu system, pip assumes that the module will be executed on the current platform and takes the required system libraries for granted.

    To address this, we can make pip do the heavy lifting for us, the cryptography package comes with a manylinux2014_x86_64 platform that ships with the required libraries:

    RUN pip install --platform manylinux2014_x86_64 --implementation cp --python 3.9 --only-binary=:all: --upgrade --target /home/packages cryptography
    RUN pip install --target=/home/packages pyjwt[crypto]==2.6.0 
    RUN pip install --target=/home/packages pymongo[srv]==4.3.3