A simple way to supply your Lambda function with dependencies

Jordan Chalupka
Jul 14 · 4 min read
Photo by Sebastian Alvarez on Unsplash

What Are Layers?

As serverless technologies become more popular, AWS continues to add new features to its serverless computing service Lambda. One of these new features was layers.

Layers make it possible to run Lambda functions with custom runtime code. One of the most exciting benefits that layers brought to Lambda, was the ability to run Lambda functions in any programming language.

However, another benefit added by layers comes in the form of simplifying dependencies in your Lambda functions. Before layers, deploying Lambda functions with dependencies, added an extra complexity when getting up and running in Lambda.

In short: Lambda layers offer a simple way to supply your Lambda function with dependencies.


The Problem

Today we will be creating a simple Lambda in Python.

Our Lambda function will receive a multidimensional array as input and output the dimensions of that array. Pretty simple, right!

Okay, let’s get started with some boilerplate:

This defines our Lambda handler to take an input event named array and return the number of dimensions in array.

Pretty trivial. Let’s get this code running in Lambda!

Head over to your AWS Console and create a new Lambda function. Be sure to select Python 3.7 as your runtime.

At first, you might be tempted to copy and paste your Lambda function into the AWS Console. It’s simple enough and you just want to test out this function that you wrote.

However, if you try this, you will receive the following error:

{
"errorMessage": "Unable to import module 'lambda_function': No module named 'numpy'",
"errorType": "Runtime.ImportModuleError"
}

At this point, you’re Googling around as to how to add the NumPy dependency to your Lambda function.

You’ll most likely receive advice to download NumPy from pip and zip your Python function with NumPy.

Start by creating a file called lambda_handler.py, if you haven’t already, and paste in your Lambda code.

Then run the following:

This downloads NumPy from pip and zips it up with your lambda_handler function.

Return to your Lambda function in the AWS Console and select Upload a .zip file under the Code entry type dropdown. Select the package.zip file from the file selector that appears.

If you were able to pass this step without any errors, congratulations! However, if you were like me, you got this error:

Lambda package maximum file size error message.
Lambda package maximum file size error message.

(Note that the project that I was working on did not match the code I uploaded exactly, and I included three dependencies scipy, pandas, and numpy which contributed to the package size)

Now what? Amazon provides us with a suggestion: upload the function to S3. This is a totally valid option and would have been the only option until Amazon created Lambda layers.


Layers

To add a layer to your Lambda function, click on the layers button on your Lambda function.

From here, select the name dropdown and you should see the following:

AWS Lambda Layer Scipy dropdown
AWS Lambda Layer Scipy dropdown

This layer will give us access to both SciPy and NumPy.

After adding, head back to your project. You may need to save again. At this point, you should be able to click Test and have your function run successfully.

Congratulations! You have just successfully written a Lambda function in Python with dependencies.


Next Steps

Lambda layers simplified the process of adding dependencies for our function.

But what about dependencies besides SciPy and NumPy?

Lambda layers give the option to create your own layers to fill these needs. Also, it is possible to add up to five layers to a Lambda function, so be sure to compose layers to suit your Lambda function needs.

This page gives lists of many of the most popular Lambda layers: https://github.com/mthenw/awesome-layers


Conclusion

Lambda layers make it easier to deploy Lambda functions which rely on dependencies. This is especially true for standard dependencies such as NumPy.

Another interesting aspect to layers is that they allow you to run Lambda functions in custom runtime environments; I will touch on this in a future post.

Better Programming

Advice for programmers.

Jordan Chalupka

Written by

Software Engineer + Growth Hacker in Toronto

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade