Creating Custom Lambda Layers

Introduction:

In the late 2018 AWS announced two new features for Lambda to make serverless deployment much easier. They are:

· Lambda layers — which is a way to manage code and dependencies across multiple lambda functions.

· Lambda Runtime API — to develop lambda functions on any programming language or a specific language version.

Lambda Layers:

In serverless applications, it is quite common to have code or dependencies shared across Lambda functions. For eg., most of the machine learning applications use Numpy as one of its core dependencies. Rather than having to zip Numpy as one of the dependencies in the deployment package, Numpy can one of the lambda layers. There are ARNs (Amazon Resource Names) available for Numpy layer, but it is possible and easy to develop one from scratch. Lambda Layers are easy to manage as it can have multiple versions, and it is immutable.

A Lambda function can only have up to 5 layers, and the standard size limit constraints apply. Please refer https://docs.aws.amazon.com/lambda/latest/dg/limits.html and https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html for more details.

The layer structure is below:

Lambda Layer Structure

Advantages of creating custom layers:

There are many advantages when using Lambda layers. For eg., Use Lambda layers to:

· Separate code and dependencies.

· Make function code look smaller.

· Deploy applications faster as the size of the deployment package is hugely reduced.

Creating custom Lambda layers:

For this post, we will look at a simple text rating service which gives sentiment scores. The model was built using scikit-learn, and other dependencies include Pandas, Numpy, NLTK. Here we will create a custom layer which contains scikit-learn and other libraries like Pandas, Numpy, etc., Let’s go through the process step by step.

1) Requirements:

The requirements.txt contains just 4 dependencies:

1) Scikit-learn

2) Nltk

3) Numpy

4) Pandas

2) Creating the package:

To have a certain Python version and also the matching AWS lambda runtime, the dependencies are built via a docker container. In our case, we used python 3.6. The following script can be used to create the dependency package:

#!/bin/bash

export PKG_DIR=”python”

rm -rf ${PKG_DIR} && mkdir -p ${PKG_DIR}

docker run — rm -v $(pwd):/foo -w /foo lambci/lambda:build-python3.6 \pip install -r requirements_text.txt -t ${PKG_DIR}

All the dependencies will be installed to the target directory which is python. Now we can zip the package.

zip -r custom_text_layer.zip python

The generated zip file can be uploaded directly if the size id <50 MB but our package size is ~80 MB. So we will upload it to an s3 bucket.

3) Publish the layer:

Add the package to an s3 bucket:

aws s3 cp custom_text_layer.zip s3://mybucket/layers/

Then publish the layer:

aws lambda publish-layer-version — layer-name textlayer — content S3Bucket=mybucket,S3Key=layers/custom_text_layer.zip — compatible-runtimes python3.6

The added layer appears in the console like below:

AWS Console showing the created layer

4) Function Layer:

To make it easier we will have an inline function code which loads the model and outputs the sentiment score.

Conclusion:

In this post, we saw how to make scikit-learn as a lambda layer and use it across multiple lambda functions.

Executing ML inference using a full serverless approach can be very effective. If the execution time can be limited and the right batch size is used, it is more affordable than a permanently hosted model and endpoint. Additionally, going serverless even allows handling peaks in load automatically without custom scaling logic/config or adapting the hosted instance size.

Using a layered approach allows us to independently and easily deploy new models without touching and deploying the complete code. Of course, it is also easy to revert back to a prior version.

Masala.AI
The Mindboard Data Science Team explores cutting-edge technologies in innovative ways to provide original solutions, including the Masala.AI product line. Masala provides media content rating services such as vRate, a browser extension that detects and blocks mature content with custom sensitivity settings. The vRate browser extension is available for download via the Chrome Web Store. Check out www.masala.ai for more info.

Mindboard

Case Studies, Insights, and Discussions of our…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store