Using Lambda Layers As Libraries/Module

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 https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path).
  4. 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.
  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. https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

Happy reading 🙂

Written by – Shahid Adoni

Share

Search Post

Recent Posts

Accelerate Business Operations with Amazon Aurora
October 4, 2024
Difference Between Dedicated Server and Cloud Server- An Overview
October 3, 2024
5 Reasons Why Multi-Cloud Infrastructure is The Future of Enterprise IT
September 19, 2024

Categories

Tags

Subscribe to the
latest insights

Subscribe to the latest insights

Related Posts

Accelerate Business Operations with Amazon Aurora

Difference Between Dedicated Server and Cloud Server- An Overview

5 Reasons Why Multi-Cloud Infrastructure is The Future of Enterprise IT

Get in Touch!

Are you prepared to excel in the digital transformation of healthcare with Rapyder? Let’s connect and embark on this journey together.

I accept T&C and Privacy