USING LAMBDA LAYERS AS LIBRARIES/MODULES

January 31, 2022

USING LAMBDA LAYERS AS LIBRARIES/MODULES

What are Lambda Layers?

Lambda Layers are nothing but an additional feature for AWS Lambda Function which helps archive code as libraries/modules or as dependencies which we can use to import the same in 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 does Lambda Layers work?

  1. To get started with Lambda Layers, open your AWS account console and search for lambda and open the lambda dashboard.
  2. Now, click on 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).
  3. After creating the Lambda Function, we are going to create a zip file which will have two modules and each one will be imported in 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 zip of the python folder/directory. For other runtimes you can refer this link https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path).
  4. Create folder named “python” which has a file named “Layer1.py” and write the following code in it which will display a hello message from the first layer. Now as 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.
  5. Next step is to create zip file of the python folder
  6. Open Lambda dashboard again in your AWS console, and select Layers and then click on Create Layer button.
  7. Upload the zipped file of the python folder and add a name, description, and compatible runtimes for your layer.
  8. We are now ready with the layer which has two modules/libraries that we can import in our configured Lambda Function. Click on Layers inside the Lambda Function overview and add the layer that we created above.
  9. After adding the Lambda Layer you can see it in the Layers below the Lambda Function.
  10. 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 and the deployment package does not contain external libraries as you shifted them into the Lambda Layers.
  11. 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.
  12. Testing our Lambda Layer!!
  13. Our function works fine with our Lambda Layer as we have got the required output mentioned in our code.

Closing Words

This was a short example on how to use and import 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

Cloud Computing Insights and Resources

What is a Hybrid Cloud Strategy? What are its Advantages?

A hybrid cloud strategy is a method that companies use to decide which portions of hybrid cloud infrastructure are most …

What is a Hybrid Cloud Strategy? What are its Advantages? Read More »

AWS Launches the Second Infrastructure Region in India – Hyderabad

On November 22, 2022, AWS launched its new AWS region in Hyderabad. The AWS Asia-Pacific Hyderabad region is the second …

AWS Launches the Second Infrastructure Region in India – Hyderabad Read More »

Three Ways Cloud is Improving Customer Experience

Three Ways Cloud is Improving Customer Experience 

Ever since the cloud rose to popularity in the 2000s owing to its various advantages over traditional computing, businesses have …

Three Ways Cloud is Improving Customer Experience  Read More »