Using the Python Library with AWS Lambda

A guide to using V7's darwin-py library in AWS Lambda

Due to some dependencies within our darwin-py library being composed of compiled code, it is necessary to take some extra steps when uploading it on AWS Lambda if your OS is not Amazon Linux.

As well as typically uploading a layer containing darwin-py to your Lambda function, you will also need to add separate layers for the following dependencies:

-numpy
-upolgyon
-PIL

Numpy

The simplest way to add this layer is to use the prebuilt layer created by AWS here and just using the ARN.

Note: Remember to enter the correct region in your ARN

Upolygon

Upolygon is a dependency created by V7 that includes compiled code. It therefore needs to be converted into to be compatible with the Lambda runtime. To do this, you can use the function below:

pip install \
    --platform manylinux2014_x86_64 \
    --target=my-lambda-folder \
    --implementation cp \
    --python 3.9 \
    --only-binary=:all: --upgrade \
    upolygon

Note: To save space, it is worth deleting the numpy files generated by this install before uploading this layer as they have already been added.

Once complete, you can then zip and upload this dependency as a layer.

PIL

Similar to upolygon, you need to generate an Amazon Linux compatible version of PIL. To do this, you can use the command below:

pip install \
    --platform manylinux2014_x86_64 \
    --target=my-lambda-folder \
    --implementation cp \
    --python 3.9 \
    --only-binary=:all: --upgrade \
    pillow

As before, you can then zip and upload this dependency as a layer once the library is installed.

You can see a general list of Python library layers across regions in this list here.

For more guidance, here is a guide from AWS on how to upload layers to Lambda functions and here is a full guide on uploading layers with compiled code more generally.

Notes

-This has been tested on the latest version of darwin-py at the time of writing (0.8.2)
-It is recommended to create the initial zip file for darwin-py on an Amazon Linux runtime.