What are Lambda Layers?
Lambda Layers are nothing but an additional feature for AWS Lambda Function, which helps archive code as libraries/modules or dependencies that we can use to import into our Lambda Function.
This helps to reduce the size of the deployment package, and the Lambda Layer will be independent of any Lambda Function, and the same Lambda Layer can be used for multiple Lambda Functions.
This is a fundamental practice of code reusability in any development lifecycle.
How do Lambda Layers work?
- To start with Lambda Layers, open your AWS account console, search for Lambda, and open the Lambda dashboard.
- Now, click the Create Function button and configure the setting, as shown in the screenshot below. (You can use the settings as per your requirements, I’m using Python runtime for demonstration purposes).
- After creating the Lambda Function, we will create a zip file with two modules, each of which will be imported into our Lambda Function.
Note: Before creating modules/files, we need to place the files in a directory/folder named python for python runtime Lambda Functions and then make the python folder/directory zip. For other runtimes, you can refer to this link https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path).
- Create a folder named “python” with a file named “Layer1.py” and write the following code to display a hello message from the first layer. As in the same step above, in the same folder, “python” create another file named “Layer2.py” and copy the below code, which returns a unique id.
- Next step is to create a zip file of the Python folder.
- Open Lambda dashboard again in your AWS console, select Layers and then click on Create Layer button.
- Upload the zipped file of the Python folder and add a name, description, and compatible runtimes for your layer.
- We are now ready with the layer, which has two modules/libraries we can import into our configured Lambda Function. Click on Layers inside the Lambda Function overview and add the layer we created above.
- After adding the Lambda Layer, you can see it in the Layers below the Lambda Function.
- As you can see above, we also have a Layer version, so you can change the code in a particular layer by uploading new code into the layer by creating a new version of the layer. This helps in code reusability as you are not changing the code in your main Lambda Function, and you can change the Lambda Function code by using inline editing as your deployment package size is reduced. The deployment package does not contain external libraries, as you shifted them into the Lambda Layers.
- It’s time to import the layers into our main Lambda Function. After importing both modules as given below, you can use the dot operator to get the functions present in the respective module.
- Testing our Lambda Layer!!
- Our function works fine with our Lambda Layer, as we have the required output mentioned in our code.
This was a short example of using and importing Lambda Layers into your Lambda Function to increase code reusability and make your code look much cleaner.
To learn more about Lambda Layers, check the below documentation link. https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
Happy reading 🙂
Written by – Shahid Adoni