AWS Lambda Layers - Unleashing the Power of Lambda Layers in a Serverless Apps
Understand the essence and importance of AWS Lambda Layers in a nutshell
What is AWS Lambda Layer ?
AWS Lambda layer, an archive with extra code like libraries or runtimes, is added to a lambda function, extracting content to the /opt directory. By shifting runtime dependencies to a layer from function code, it reduces the size of the archive uploaded during deployment. Five layers per function are allowed, counting toward standard Lambda deployment size limits.
Immutable versions of layers are deployed, with version numbers increasing upon publishing a new layer. Permissions apply to a specific layer version.Leveraging layers accelerates application deployment for the serverless apps.
Advantages of Using AWS Lambda Layers
AWS Lambda has size limitations that are easy to hit when working on a large projects.The total bundle size of AWS Lambda is 50MB when it is archived and if we are testing the AWS Lambda in the console, inline editing can be achieved only if it is 3MB.
Inorder to better manage the AWS LAmbda Layers, we can pacakge common code base in layers and then attach these layers to the lambda function during deployment. AWS LAmbda Layers provide following advatages for our severless apps,
Smaller Lambda Function Package Size
Due to AWS LAmbda memory limit, it can be challenging to manage a large project for AWS Lambda, complicating tasks like application maintenance, testing and slower deployment.
To address these concerns and maintain more manageable packages, Layers offer the capability to isolate commonly used features shared by two or more functions. Instead of deploying redundant code multiple times, the Layer undergoes a single deployment, efficiently loaded by the Lambda platform to serve functions dependent on it.
Faster Deployment for Lambda Function
Lambda Layers allows better manage large dependencies or common code to be bundled as layers. This helps to reduce our Lambda function code base and allows for faster deployment.
Lambda layers also helps us to simplifies code management within each layers. When a feature requires modification, updating a single layer suffices. Consumers of the layer also have the flexibility to upgrade at their convenience.
Code Resuability across Lambda Functions
AWS Lambda layers streamline the sharing of a common codebase across multiple lambda functions, eliminating the need for replicating code in different locations—a practice best avoided.
Previously, preventing code duplication involved implementing common features as isolated functions, a viable solution in certain cases. However, this approach required an extra function invocation, occasionally running in parallel, leading to increased function coupling and costs.
Creating a Lambda layer using AWS SAM
Lambda layer creation can be easily done using the AWS SAM CLI. Depending on the use case, the Lambda layer can be created with a full code base or make use of a specific library version which need to be used in multiple lambda functions.
Creating AWS Lambda Layers for a Nodejs Example
In the below example I'm going to create an AWS Lambda Layer for nodejs and SAM. In this example a specific library version of the image processing library Sharp
is used and published it as lambda layer.
- Lets create a new project
- Add
package.json
and mention theSharp
version to use. In this blog, I'm going to use 0.33.2 version
{
"name": "sharp-lambda-layer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"license": "MIT-0",
"dependencies": {
"sharp": "0.33.2"
}
}
- Lets add a SAM
template.yaml
file with below content,
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SDK Layer
Resources:
SDKlayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: Sharp
Description: Sharp NPM package.
ContentUri: './layer'
CompatibleRuntimes:
- nodejs20.x
LicenseInfo: 'Available under the Apache-2.0 license.'
RetentionPolicy: Retain
- Let install the package and then do the deployment using SAM,
npm install
mkdir ./layer/nodejs –p
mv ./node_modules ./layer/nodejs
SAM Deploy: sam deploy --guided
Ensure the AWS Profile is setup in the machine and the correct credentials are passed for the deployment.
AWS Lambda Layers Size Limit
Usage of Lambda Layers is not changing any size limitation for the AWS lambda. With LAmbda Layers the maximum size limit for your package including the layer is 250MB. It is possible to add 5 lambda layers per function and the total size cant be greater than 250mb.
AWS LAmbda Layer Public List
While layers are initially private, Lambda Layers can be sharedto other AWS accounts or making them public. AWS has released a set of lambda code base which can be deployed as Lambda Layers in your account. Along with this there are other companies who released public aws lambda layers which is available for other accounts. This github repo has curated the list of these layers and is very useful if you want to see a layer exist for your use case