Basic guide to packaging Python projects that use certain problematic libraries (such as Passlib) for Lambda. Requires Docker.
Basically, if you've gotten an error such as no backends available -- recommend you install one
or Unable to import module: libffi-bce22613.so.6.0.4: cannot open shared object file: No such file or directory
, this is a potential solution.
Or copy the Dockerfile manually.
On your local machine
git clone https://gist.github.com/01166d1869c079eacafd07eb9cac712f.git
Replace /path/to/local/project
with the absolute path to your Python project.
On your local machine
cd 01166d1869c079eacafd07eb9cac712f
docker build . -t lambda-build
docker run -v /path/to/local/project:/project -it lambda-build:latest
Lambda requires all modules in the root directory, so this keeps your source from getting cluttered with dependency modules.
In the Docker container
rm -rf /dist && cp -r /project /dist
Use one of the options below, depending on how your project is setup. The magic part is the --no-binary :all:
argument, which tells pip
to compile everything from scratch instead of using system dependencies.
In the Docker container
cd /dist
# --no-binaries will make pip compile dependencies on the current system
pip3 install --no-binary :all: argon2_cffi passlib -t . --upgrade # Manually
pip3 install --no-binary :all: -r requirements.txt -t . --upgrade # requirements.txt
pip3 install --no-binary :all: . -t . --upgrade # setup.py
zip -r /lambda_build.zip ./* && cp /lambda_build.zip /project/lambda_build.zip
You should have a lambda_build.zip
file in your project directory that you can upload directly to AWS Lambda.
For the example.py
file, you can use example.lambda_handler
as the Handler in the AWS Lambda console.