Let’s Talk!
Jaiinfoway Us Flag

Jaiinfoway IN Flag
+91 9823885440

AWS Lambda layers development with NodeJS

Flexible code/data sharing within the same or various AWS accounts, AWS lambda layers introduced in 2018. Although AWS Lambda supports a variety of environments, in this article we will exclusively discuss Node.js. I’ll go through how to create lambda layers and how to apply them to Lambda functions.

Because of this, we may utilise custom code anytime lambda functions supporting the node js environment needed. We create that code in the form of node modules, and we must build those node modules in order to make it shareable across lambda levels. Any lambda function may access that code in the end can associated to the Lambda Layer that build has posted.

As Node Modules, common code

The following use cases built for lambda layers in an AWS Lambda environment that supports Node.js:

Packages NPM

Small number of public npm packages natively supported in lambda environments. You will need to establish standard lambda layers that can used throughout your whole AWS setup for all other npm packages.

Individual Node Modules

Node modules that you created specifically for your system’s needs.

Link individual node modules

Installing npm packages from the npm repository only requires typing the command: npm install { package-name }

However, you must turn your customised Node.js code into Node modules. Most node modules have these 3 components:

  • The node modules folder contains all modules.
  • The index.js file exports all node modules.
  • The package.json configuration file.

Why use lambda layers?

Developers frequently need to share common libraries amongst micro services when creating nodeJS microservices apps in AWS. The lambda function’s file size rise if those common libraries rewritten or imported into each microservice, which slow down the deployment process. AWS Lambda layers are a possible remedy for developers.

Intro to Lambda Layers in Python. Setting up a simple Lambda Layer in… | by  Ben Mc | Better Programming

Lambda layers, which are zip archives that can contain extra codes like libraries and dependencies, are highly helpful for exchanging code across lambda functions. This technique minimises the overall size of the bundle published during a deployment by transferring dependencies or libraries from your function code to a layer. Up to five levels can added by developers for each function. Lambda layers are by default private, but developers may make them public or share them with other AWS accounts.

Because they are immutable, lambda layers versioned to control changes. Functions that previously utilised a version will continue to function even after it destroyed or its usage has restricted, but you won’t create new ones.

The storage of lambda layers takes part in the AWS Lambda function storage per region limit which is 75GB.

The advantages of AWS lambda layers

  • Limit the scope of deployments.
  • Each lambda function may include only the code necessary to carry out the specified operation.
  • For all common dependencies, use a single package. Shared dependencies do not need to be packaged with your lambda functions. Create a layer instead, then utilise it for various purposes.
  • Code changes are simpler. Updating a dependent is relatively simple if the common dependencies are controlled in layers since you just need to update the layer in which the dependency is packed.

Permissions for the Lambda Layer

  • One might employ layers inside
  • One’s AWS account
  • between accounts shared
  • public disclosure to the whole development community

Easy reference for lambda layers

In this example, I’ll build a lambda layer for a straightforward response helper so that we can use it across all of our services without having to develop it separately for each one. We must first use sls create -t node-js to establish a serverless project. Then, using nodeJS, we must set up a folder structure similar to the one shown below.

The next step is to use npm init to initialise a npm module within the response-helper directory.

The response helper logic must written in an index.js file in the response-helper directory, which then exported.

You must create your response helper and provide the path in the serverless.yml file beneath the layers.

After everything set up correctly, you must deploy the service using sls deploy. Your lambda layer will appear in the layer section of the lambda function service once you have deployed it.

You can currently view the stack of the lambda layer in the cloudformation.

The qualifiedarn of your lambda layer may seen in the stack’s output section. You may now use it as a reference in your lambda functions.

Must reference the lambda layer’s qualified arn as seen in the following image in the layers portion of your handler function in order to add a lambda layer.

You must import the response helper just as you would a javascript module in order to use it in your service handler method.

The desired answer may obtained as shown below once all the necessary steps taken and your API called using an API client.

Final Word

In this post, I did my best to describe AWS Lambda layers. I hope you discovered what you were looking for. And I hope you took something away from this post.

Because of Jaiinfoway Data’s excellent connection with 100+ data sources (including 40+ free sources), you may enrich and amend your data before exporting and loading it to the destination of your choosing. Jaiinfoway‘s inbuilt Webhooks Connector also allows for the inclusion of data from non-native sources. Following that, you may concentrate on your primary business requirements and conduct intelligent analysis with BI technologies.

Leave a Comment

Your email address will not be published. Required fields are marked *