What Are 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.
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
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'",
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
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:
(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.
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:
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.
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
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.