This content originally appeared on DEV Community and was authored by Girish Bhatia
In my previous articles, I covered various use cases for AWS Serverless Lambda functions, ranging from a simple "Hello, World" example to more advanced scenarios involving API Gateway, DynamoDB, and integrations with Amazon Bedrock for Generative AI applications.
I also shared an article demonstrating how to create a Lambda layer using the AWS Management Console and reference it for the lambda function using AWS SAM.
If you're interested in these topics, please visit my content page for these published articles.
In this article, I’ll show you how to work with Lambda Layers using AWS SAM, an Infrastructure as Code (IaC) framework for building, testing, and deploying Lambda functions to the AWS Cloud. This will include creating a Lambda layer using AWS SAM and configuring a Lambda function to reference the layer.
There are several ways to create Lambda functions using the IaC approach, including AWS SAM, CloudFormation, and Terraform. While I typically use AWS SAM, I may share a few examples using Terraform in the future.
Let's look at the architecture diagram!
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 Lamba 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. This layer can also be created using AWS SAM.
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.
Review the common function for Lambda Layer
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:
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.
I will use AWS SAM to build this layer.
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:
Now that we have both the Lambda layer and the Lambda function code, let's proceed to build the layer and reference it in the Lambda function.
To develop and validate the layer and Lambda function locally, I will use AWS SAM (Serverless Application Model). AWS SAM is especially useful for testing and debugging Lambda functions before deploying them to the cloud.
I’ve previously posted several videos on AWS SAM and Lambda, covering the steps in detail. If you’re new to AWS SAM and Lambda, I recommend watching those videos for a better understanding.
Reviewing the template.yaml for AWS SAM
Let's review the template.yaml for the AWS SAM:
This template.yaml file has the code to create the layer as well as the code to reference the layer in the function. When I build the function using this template.yaml, it will build both the layer and the lambda function.
Build and Validate
Since I am using AWS SAM for this function, let’s begin by building the function. Use the following command to build the function:
Command: sam build
Once the build is successful, you can invoke the function locally using the sam local invoke command:
Command: sam local invoke
For example:
sam local invoke helloGreetingFn
When executed with the name parameter set to 'girish', you should expect the following output:
Deploying to AWS Cloud
After validating the function locally, you can deploy it to the AWS Cloud using the sam deploy command:
Command: sam deploy
This concise workflow demonstrates how to create, validate, and deploy Lambda functions using AWS SAM. By leveraging AWS SAM, you can streamline the development process and ensure your functions are tested thoroughly before deployment.
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.
Since these resources were build and deployed using AWS SAM, these can be deleted using AWS SAM command.
Command: sam delete
You can also delete the resources via AWS Console.
Conclusion
In this article, I demonstrated how to create a Lambda layer and reference it in a Lambda function using AWS SAM. In my previous articles, I showed how to create a layer using the AWS Management Console and then reference it in a Lambda function. However, in this article, the entire process from creating the layer to referencing it in the Lambda function, and then building and validating was accomplished using AWS SAM.
I hope you found this article both helpful and informative!
Thank you for reading!
Watch the video here:
https://www.youtube.com/watch?v=o5sLtIK2DYc
𝒢𝒾𝓇𝒾𝓈𝒽 ℬ𝒽𝒶𝓉𝒾𝒶
𝘈𝘞𝘚 𝘊𝘦𝘳𝘵𝘪𝘧𝘪𝘦𝘥 𝘚𝘰𝘭𝘶𝘵𝘪𝘰𝘯 𝘈𝘳𝘤𝘩𝘪𝘵𝘦𝘤𝘵 & 𝘋𝘦𝘷𝘦𝘭𝘰𝘱𝘦𝘳 𝘈𝘴𝘴𝘰𝘤𝘪𝘢𝘵𝘦
𝘊𝘭𝘰𝘶𝘥 𝘛𝘦𝘤𝘩𝘯𝘰𝘭𝘰𝘨𝘺 𝘌𝘯𝘵𝘩𝘶𝘴𝘪𝘢𝘴𝘵
This content originally appeared on DEV Community and was authored by Girish Bhatia
Girish Bhatia | Sciencx (2025-01-11T20:58:35+00:00) AWS Serverless: How to Create and Use a Lambda Layer via the AWS SAM – Part 2. Retrieved from https://www.scien.cx/2025/01/11/aws-serverless-how-to-create-and-use-a-lambda-layer-via-the-aws-sam-part-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.