Skip to content

Instantly share code, notes, and snippets.

@MartinHarding
Last active October 16, 2020 16:19
Show Gist options
  • Save MartinHarding/01166d1869c079eacafd07eb9cac712f to your computer and use it in GitHub Desktop.
Save MartinHarding/01166d1869c079eacafd07eb9cac712f to your computer and use it in GitHub Desktop.
Passlib on AWS Lambda Example

Use Passlib on AWS Lambda with no Bullshit

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.

1. Clone this gist somewhere

Or copy the Dockerfile manually.

On your local machine

git clone https://gist.github.com/01166d1869c079eacafd07eb9cac712f.git

2. Build and run the Dockerfile

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

3. Copy your project files to a dist directory

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

4. Install project dependencies

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

5. Zip up your files and copy back out of Docker container

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.

# Amazon Linux version that Lambda runs
FROM amazonlinux:2017.03.1.20170812
# Install Python 3.6.1 and zip
RUN yum -y update
RUN yum -y install curl zip openssl openssl-devel gcc zlib-devel
WORKDIR /tmp
RUN curl -o Python-3.6.1.tgz https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
RUN tar zxf Python-3.6.1.tgz
WORKDIR /tmp/Python-3.6.1
RUN ./configure && make && make install
WORKDIR /tmp
RUN rm Python-3.6.1.tgz && rm -rf Python-3.6.1
# Install libffi-devel for compiling Argon2 backend
# (you can add additional compile dependencies to this RUN command if needed)
RUN yum -y install libffi-devel
from passlib.hash import argon2
def lambda_handler(request, context):
hash = argon2.hash("password")
valid = argon2.verify("password", hash)
response = 'HASH={} VALID={}'.format(hash, valid)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment