AWS Lambda Layers and NodeJs Runtime

AWS Lambda – Serverless

AWS Lambda is serverless service. It lets you run your code without managing or provisioning servers. And that is why being a developer I am found of serverless computing. I believe it provides ease to the developer. That they can focus only on the development and learning the technologies they are interested in. And develop and push the code in serverless. And the best thing is, serverless computing scale automatically when needed. In cloud computing or AWS will only charge when function gets hits or executed. If the function is not get executed AWS will not charge you anything.

AWS Lambda support multiple languages. You can write your code in the language you already know. Lambda natively support below languages,

  • Java
  • C#
  • Python
  • Nodejs
  • PowerShell
  • Ruby

But apart from these you use almost all other languages.

AWS Lambda Layer

I mostly use NodeJs from my lambda development. And I feel very comfortable in editing my code using AWS Lambda environment. But the one problem is that if you are using multiple node packages and its size get increase the environment will not be able to show your code. And you have to modify locally and upload the code either directly in Lambda or first in S3 bucket. Another problem I faced in that to include new package I have to add the node package first on my laptop and then upload back to Lambda. This slow down my development. And I have to repeat the process again add the NodeJs module and upload back to S3 bucket.

AWS Lambda Layer is a service, which can be used to keep the dependencies at a common place and attach with more than one function. I feel this very useful, that instead of adding the node module locally and then zip and push back to AWS. I can use Layers. I can attach Layers with my function and this will help in reduce actual function size of the function and also code repetition is reduced.

Let’s start with a simple example how can you use Layers. I am going to create a simple function. And then I want to use npm package lodash in it. I created a NodeJs function. And keep the default code as it is as shown below.

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Now I am going to create a Layer which will have lodash npm package. For this purpose I created a folder on my local machine name testlambdalayers and inside I created another folder name nodejs. Note that for nodejs project you have to keep the folder name nodejs. For more information please see the AWS documentation. Goto the nodejs folder from terminal window. For Windows user it will be command prompt instead of terminal. From the terminal type the following command to initialize npm by npm init -y

Now install the npm modules you want. I just want to use lodash for this example.

npm i --save lodash

Now the folder structure on my local computer is as below,

Zip the nodejs folder. And open the AWS Lambda management console. In the Lambda function you can click and expand the menu and click on Layers. Click on create Layers and give a name to layer and you and either upload the zip file first to S3 bucket or directly upload from your local machine. In this example I will directly upload from my local machine. Choose the run time environment, in my case I selected Node.js12.x and click on create.

After successfully creating the layer open the lambda function and click on Layers in the designer. Click on add layer and in the window select custom. From the dropdown you can select the layer and its version. Or you can also add layer by giving its ARN number. Another best thing is that for one layer you can have multiple versions. It means if new version of lodash introduced but I want to keep existing for backward compatibility I can create a new version instead of updating the existing version.

Now to use the lodash package, I just need to add it in the code. And I am good to go. Just to make the example simple I use upperCase function of the package. Below is the default Lambda code I modified.

var lod = require('lodash');

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: lod.upperCase(JSON.stringify('Hello from Lambda!')),
    };
    return response;
};

Create an empty test event and test the Lambda function and you will see the below response.

Response:
{
  "statusCode": 200,
  "body": "HELLO FROM LAMBDA"
}

You can see that how easy it is to use the npm package using Layers and it reduce the package size but also we can use the same layer in other functions as well. I make the example simple, but you can group multiple npm packages and create Layers. You can keep versions as well. I found AWS Lambda Layers very useful and helps is speedy development.

I hope you like the post. Please do share your feedback.