DEV Community

Cover image for AWS Serverless: How to Create and Use a Lambda Layer via the Console
Girish Bhatia
Girish Bhatia

Posted on

AWS Serverless: How to Create and Use a Lambda Layer via the Console

In my previous articles, I explored various use cases for AWS Serverless Lambda functions, ranging from a simple "Hello, World" example to more complex scenarios involving API Gateway, DynamoDB, and integrations with Amazon Bedrock for Generative AI applications.

If you are interested in these articles, Please review my previously published content.

In this article, I will demonstrate how to use AWS Lambda Layers and configure them for a Lambda function. I will start by using the AWS Management Console to create a Lambda Layer and integrate it with a simple Lambda function.

In upcoming articles, I’ll also show you how to work with Lambda Layers using AWS SAM (Serverless Application Model), an Infrastructure as Code (IaC) framework for building, validating, and deploying Lambda functions to the AWS Cloud.

Let's look at the architecture diagram!

Image arch

Introduction to Lambda Layers

Let's first review what a lambda layer is! A Lambda Layer is a way to share reusable code and libraries among multiple lambda functions. It makes the size of lambda function deployment smaller. Also, by packaging shared functions in a Lambda layer, you can simplify the deployment process and ensure consistency across functions.

In order to create the layer, first step is to create a zip file that contains your sharing function code or supplement libraries. Once created and uploaded in the AWS using console or IaC, this layer then can be referenced by the lambda functions.

Benefits of Using Lambda Layers

Lambda layers offer several advantages, including:

  • Reusability of Code

For example, a common function can be shared across multiple Lambda functions.

  • Simplified Deployment

By placing common code in a layer, the deployment size of Lambda functions is reduced. This also provides a central location for updating shared code.

  • Inline Function Editing

With reduced deployment size due to layers, Lambda functions can be edited inline in the AWS Management Console without exceeding size constraints.

  • Library Management

Frequently used libraries can be packaged in a layer, making them accessible to multiple functions.

Constraints to Consider When Using Lambda Layers

When using Lambda layers, keep the following constraints in mind:

  • A Lambda function can use up to five layers.
  • Each layer has a maximum size limit of 50 MB (compressed).
  • The total size of the uncompressed function code and layers combined cannot exceed 250 MB.

Additionally, layers are immutable. Any update to a layer creates a new version, and functions must explicitly reference the updated version to incorporate the changes.

Use Cases of Lambda Layers

Lambda layers can be used in various scenarios, including:

  • Shared Libraries
    Shared libraries can be packaged in layers. For example, the Pandas library can be included for use in Python functions.

  • Custom Functions
    Custom functions can be packaged in a layer and reused across multiple Lambda functions.

  • Common Configuration
    Common configuration settings can be packaged in a layer.

  • Logging Tools
    Logging utilities can be included in a layer.

Create a Lambda Layer

In this example, I will create a Lambda Layer to include a reusable Python function that generates a greeting message.

The function, hellogreeting, takes a name as input and returns a personalized message. If no name is provided, it defaults to a generic "Hello, World!" message.

Here’s the function code:

Image fncode

This function will be packaged into a Lambda Layer so it can be reused across multiple Lambda functions without duplicating code. In the current example, I will invoke this layer from one lambda function.

To create this function as layer, Navigate to the Lambda console and select create Layers.

Image createlayer1

Provide the layer name, description and runtime as python 3.13 and upload the zip file created for the layer. This will create the targeted layer in the console.

Image createlayer2

Use the Lambda Layer in a Function

I will use this lambda layer in a Lambda function. To use the layer, let's review the steps:

Import the Required Modules and Layer Functionality

  • The json module is imported to format the response as JSON.
  • The hellogreeting function is imported from the helloworldlayer.

Lambda Handler Function

  • The lambda_handler function is the entry point for the Lambda function.
  • It uses the hellogreeting function, passing the name "girish" as a parameter to generate a personalized greeting message.

Return the Response

  • The response is returned as a dictionary with:
  • statusCode: 200 indicating success.
  • body: json.dumps(msg) where msg contains the greeting message formatted as JSON.

Code is as below:

Image fncode2

Attach the layer with this function as outlined in the screenshot below:

Image attachlayer

Remember, each time you update the layer, a new version will be created. You will need to update the reference if you want to use the updated layer.

Deploy and Validate

Once layer is created and attached with the function, the function can be deployed and validated using AWS Console.

Below is the output expected once the function is executed with name parameter as 'girish'.

Image res

Cleanup - Delete the Lambda function & Layer

Once you have completed the setup, ensure you delete the Lambda function to avoid unnecessary resource usage. Additionally, delete the Lamba layer. If you created any new roles during the process, remember to delete those as well.

Conclusion

In this article, I demonstrated how to configure a Lambda Layer and integrate it with a Lambda function. I explored how Lambda Layers enable the reuse of code, such as shared functions and libraries, and how they can be referenced from one or more Lambda functions. By adopting this approach, you can streamline your serverless applications, making them more maintainable and efficient.

In upcoming articles, I’ll dive into advanced topics, such as using AWS SAM to manage Lambda Layers with Infrastructure as Code, offering a more automated and scalable solution.

I hope you found this article both helpful and informative!

Thank you for reading!

Watch the video here:


https://www.youtube.com/watch?v=5YMvvrUIC-M

π’’π’Ύπ“‡π’Ύπ“ˆπ’½ ℬ𝒽𝒢𝓉𝒾𝒢
𝘈𝘞𝘚 𝘊𝘦𝘳𝘡π˜ͺ𝘧π˜ͺ𝘦π˜₯ 𝘚𝘰𝘭𝘢𝘡π˜ͺ𝘰𝘯 𝘈𝘳𝘀𝘩π˜ͺ𝘡𝘦𝘀𝘡 & π˜‹π˜¦π˜·π˜¦π˜­π˜°π˜±π˜¦π˜³ 𝘈𝘴𝘴𝘰𝘀π˜ͺ𝘒𝘡𝘦
𝘊𝘭𝘰𝘢π˜₯ π˜›π˜¦π˜€π˜©π˜―π˜°π˜­π˜°π˜¨π˜Ί 𝘌𝘯𝘡𝘩𝘢𝘴π˜ͺ𝘒𝘴𝘡

Top comments (0)