Managing packages with AWS Lambda Layer

What is AWS Lambda Layer and why should you care?

Do you remember the example from the last post about you being the medieval age warlord? Yeah. So you are still the same warlord who was able to make the right decisions after reading the post and now you are participating in warfare. Now you hired a brilliant battle strategist like Shikamaru and he is coming up with excellent strategies and battle plans like the movie Red Cliff. He is great and successful so far but there is one thing he specifically needs to be successful – consistency. It is paramount that all the soldiers in a battalion carry identical weapons. Identical in shape, Identical in size, identical in weight, and identical in every other aspect.

Why is that? Look at the above picture. If even a single guy has a shield even one inch bigger than other then the whole system may collapse. It must be a fragile system if it can be brought down by just a few berries? oh, wait. Sorry. Inches of a shield. Yeah, Indeed it is a fragile system.

So, what can we do to avert that fate? We hire a guy whose only job is to take care of all the weapons and make sure all of them remain identical.

So how is this related to AWS Lambda? Yes. How? So you see a soldier here is a Lambda function, The weapons are third party packages or dependencies and the guy who is managing all the weapons In your Lambda layer. Now you may be wondering –

What the heck is a dependency?

You see sometimes while writing code there are some problems that you can not solve yourself like interacting with the database or calling an API or flying a B-21-2 Helicopter. And even if you can solve this problem yourself, It can take a heck more of time than Agent Smith gonna give you but there may be some people who already solved that problem and uploaded the solution in a nicely maintained package on the internet for the world to use. These packages are called third-party packages or dependencies because your code is dependent on it.

So you and your self-esteem bite a bullet and you download the code to use it. but nowadays some smart people have created some registries that manage these packages like NPM for JavaScript and pip for python code. You can install packages from there and upload packages there. Most of these registries come with a command-line tool which can do these operations.

But the problem is, There is no command line in AWS Lambda. So how can we deal with that? We can simply download packages locally then we can upload the entire Lambda function as a zip file.

But the thing is Lambda functions have 50 MB deployment package size limit for direct upload and external libraries can easily bloat the deployment package size and surpass the limit. Seems like another problem! But fear not! I showed you the problem so I will provide you the solution too and the solution is the AWS Lambda layer.

How the hell does this work?

So, It works like this –
You install the packages on your computer. Structure them. You zip these packages like a dead body then upload it as a dependency layer in AWS Lambda then you attach these packages to your Lambda functions and dang! You can use the installed package in your code. Given you did everything correctly.

Enough explanation, Let’s see it in action

So for starter, we are going to do a very basic job which needs a third-party library. We are going to convert a JavaScript Date object into a moment.js object. For those who do not know, moment.js is library which deals with date-time objects and can perform all sorts of cool operations on datetime objects. Unfortunately, it is not available by default with JavaScript so we have to install it from npm registry and using it in a Lambda function would have been a disaster but Fortunately, we now know about Lambda layer so we can add it permanently for all Lambda functions to use.

Create the package locally

To create a new layer, we will have to zip the packages in a well defined folder structure. To do that, open your terminal and create a folder named nodejs and enter it.

mkdir nodejs && cd nodejs

Now inside this folder install whatever packages you want. For us, right now it is moment.js so install moment using

npm i moment

The directory structure should look like this in the end

Lambda layer directory structure

Zip the nodejs folder. You can do this by the below command –

zip -r nodejs.zip ./

Upload the package on AWS Lambda

Okay so we have a package but we can’t just throw it in the air and expect it to work. However we may want but things don’t work like that. We first have to create a layer resource for it on AWS. To do that open your Lambda dashboard and click on Layers.

Lambda dashboard

Here you will see a list of layers that you created. Click on the Create Layer button.

Existing layers

Fill out the layer form by choosing names, runtimes for the layer and by uploading the package.

Layer form

After clicking on Create you will see this screen

and this means voila!!! your layer is created. Here you can see all the information about our layer including ARN(Amazon Resource Name) which is basically a fancy word for a unique name assigned to your every resource in your entire AWS account but don’t let my casual way of talking about it fool you. It is super important so note it. Or not, you can also find it later. Also note your Layer version. Yes! a versioning system for your packages in case you are not ready to upgrade some of your Lambda functions. How cool is that. Try doing that in your non-serverless(should we start calling them servered?) architecture.

Attaching your layer to a lambda function.

So now that we have created an AWS Lambda layer, let’s try attaching it to a Lambda function.

To attach it with a Lambda function, go to your Lambda function list and select the Lambda function and click on Layers.

Scroll down until you see the layers block. Here you will see all the layers we have attached with the lambda function. Yes, we can attach multiple layers. The order in which the code merge will happen and before you ask, yes the order is important. You can’t find a level 100 boss until you haven’t defeated the level 1 crook. But you can change order later simply by clicking on edit and editing the order.

Click on the Add Layer button. You will see the below screen

Add Layer form

AWS Layers provide some prebuilt layers which maybe same as video games providing some initial weapons which you may never want to use in a real battle(*Glaring at my fists in pubg*). Here you can either pick the layer you created from dropdown in custom layers or you can specify the ARN if you have noted down it. Then you have to click on the Add to add the layer and TADA!!!!! you have just merged your layer with your Lambda function and it is ready to use.

How to use your Lambda layer

You won’t believe your ears when I will tell you the secret ingredient of how to use the layer you have just merged.

That’s right!!!! You have to do absolutely nothing to use the packages inside the layer. You can just call them as you usually do. Like for our example, we can simply use the below code to call and use it –


const moment = require('moment'); // require


exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: {time: moment()},
    };
    return response;
};

Now up until now if you have done everything correctly then you should get the below response when you fire up the Lambda –

{
  "statusCode": 200,
  "body": {
    "time": "2023-10-23T17:25:40.227Z"
  }
}

So here it is, You have just created and use AWS lambda. And as Lambda function. Layers also have the advantage of choosing different architectures and environments so you can use them with Lambda functions that do not have the same environment. The only thing is that layer should have the same environment as the Lambda function. There can be different versions of the same layer that can be used with different Lambda. We can use up to 5 layer in a Lambda function and the total size of unzipped Lambda package and the layers should be less than 250 MB.

In the end

So we gotta summarize what we have learned in a breathless monologue. So repeat after me.
AWS Lambda layer is a support feature of AWS Lambda function that can store 3rd party packages that we can import and use in the AWS Lambda function. This helps us in better maintaining code and dependencies as we can move all the common code and dependency in Lambda layer and keep our Lambda function clean. This also helps us in solving the Lambda deployment size issue.

Liked what you read? Check out my other blog post on AWS Cognito Identity Pool

Leave a ReplyCancel reply

Exit mobile version