Implementing AWS Lambda in NodeJS

Nisarg Patel
Mindful Engineering
5 min readApr 11, 2020

With the rise of serverless technologies, services such as AWS Lambda have become increasingly popular. AWS Lambda is Amazon’s most reliable serverless computing service. This article gives you an introduction to the features and benefits of using AWS Lambdas and also implementation in a nodeJS.

Problems before AWS Lambda

Suppose, a website is hosted on AWS EC2, in which currently 90–100 users are reading a blog and, in the back-end, the Admin User uploads 10 videos on the website for processing.

This increases the load on the server, which triggers the auto-scaling feature, and hence EC2 provisions more instances to meet this requirement — since hosting and back-end changes are both taking place in the same instance.

However, auto-scaling takes a long time to provision more instances, which eventually becomes a reason for slow actions on the website when the initial spike in the task is received.

This problem can be solved using distributed computing. In this, the website is hosted on one instance and the back-end code runs on another. While the users are reading a blog on the website, the Admin User can upload those 10 videos on the website.

This time, the website will forward the task of video processing to another instance. This makes the website resistant to video processing, and therefore website performance will not be impacted. However, video processing still takes a lot of time, when the load increases, because auto-scaling takes time on EC2.

Here, we need a stateless system to solve this problem, and AWS does exactly this with the launch of AWS Lambda!

How? We shall discuss it as we move along in this blog.

So, let’s first understand what exactly AWS Lambda is?

What is AWS Lambda?

AWS Lambda is one of the computing services provided by AWS, which is event-driven and serverless. It is a stateless serverless system that helps us run our background tasks in the most efficient manner possible.

Here, being serverless doesn’t mean that servers are nowhere in the play. Instead, it means that we don’t have to worry about the provisioning or management of our servers or instances; it just helps us focus on our main goal, i.e., coding. We just have to put our code in AWS Lambda, and we’re good to go!

Whatever resources are required for our code in response to our events, AWS Lambda automatically provides us. The best feature of it is that we just have to pay for every single request made during the time.

Building Blocks of AWS Lambda

  • Lambda function: Whatever custom codes and libraries that we create are called a function.
  • Event source: Any AWS or custom service that triggers our function and helps in executing its logic
  • Log streams: Lambda monitors our function automatically and one can view its metric on CloudWatch, directly, but we can also code our function in a way that it provides us custom logging statements to let us analyze the flow of execution and performance of our function to check if it’s working properly.

Let’s Implement in NodeJS

Let’s start off by creating a Lambda function.

  • Login to your AWS account
  • Go and type AWS Lambda in the AWS Management console
  • Click on Create a Function
  • You’ll see a setup page, in which you have to fill up a few aspects for your function such as the name, runtime, role, etc. You can choose from blueprints as well, but here we’re going to author it from scratch
  • Enter the function name and all other details. In the case of the runtime, you can choose any based on your understanding of that language; we’re choosing NodeJS 12.x from among the options such as Python, Java, .Net, and Go (these are the languages it supports)
  • Execution role — you’ll have to create a new role if you don’t have one.
  • The next step is writing the code for your Lambda function (random number generator).
  • you can choose different editors as shown above image. You can also upload a zip file.
  • Here we will use inline editor and now we will change code from the default one —
‘use strict’;
console.log(‘loading funciton’);
exports.handler = (event, context, callback) => {
let min = 0;
let max = 10;

let generatedNumber = Math.floor(Math.random() * max) + min;

callback(null, generatedNumber);
}
  • Now jumps to the basic configuring section -

memory — Each function runs inside a container and that container has some memory allocated. You can modify it as well. In our case, 128Mb will be enough as it is a very basic function

timeout — if your function hasn’t finished the execution in that particular duration then amazon kills the function and gives the error message.

execution role — we will create a new role to execute this function. you can select any policy template, we will select simple microservice permission.

Now, we are ready to go for testing our function.

Congratulations! You’ve created and executed your first Lambda function successfully!

--

--