Using Lambda Layers as Libraries
January 31, 2022

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?

  1. To start with Lambda Layers, open your AWS account console, search for Lambda, and open the Lambda dashboard.
  2. 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).
  3. 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
  4. Create a folder named “python” with a file named “” 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 “” and copy the below code, which returns a unique id.
  5. Next step is to create a zip file of the Python folder.
  6. Open Lambda dashboard again in your AWS console, 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 we can import into our configured Lambda Function. Click on Layers inside the Lambda Function overview and add the layer 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. 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 the required output mentioned in our code.

Closing Words

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.

Happy reading 🙂

Written by – Shahid Adoni

Cloud Computing Insights and Resources

Role of Augmented and Virtual Reality in EdTech

Role of Augmented and Virtual Reality in EdTech 

There has been a rapid adoption and incorporation of technology in education, especially since the Covid-19 Pandemic, to make education …

Role of Augmented and Virtual Reality in EdTech  Read More »

Optimization and Licensing Assessment for Enterprise Workload

Optimization and Licensing Assessment for Enterprise Workload: An Overview 

Businesses around the world are enhancing cost to performance to achieve 2x growth by migrating their workload. But this does …

Optimization and Licensing Assessment for Enterprise Workload: An Overview  Read More »

4 Ways to Optimize Your Cloud with Modernization

4 Ways to Optimize Your Cloud with Modernization: A Comprehensive Guide

In today’s dynamic IT industry, it is imperative for organizations to incorporate cost-effective measures and efficient budgeting strategies to achieve …

4 Ways to Optimize Your Cloud with Modernization: A Comprehensive Guide Read More »