Let's write the first AWS Lambda Function ⚑ in NodeJS

Let's write the first AWS Lambda Function ⚑ in NodeJS

First of all a very welcome πŸ™ to all of my readers. In this blog, I'll be describing what is AWS Lambda Function which is also termed as Serverless Function based on FaaS (Function as a Service) Architecture, and How it is different from our Traditional PaaS (Platform as Service) based Architecture in the cloud platforms. And In last, we will see the steps by implementing it. Don't worry guy's I'll be explaining it in detail with the basics.

For the readers who really don't know what is FaaS (Function as a Service) and what is PaaS (Platform as Service). The primary difference between them is composing and deploying your application.

In PaaS based Architecture, we can develop our application in the traditional manner as we were already developing using web frameworks like Angular, ExpressJS, Django, Flask, ASP.NET, Ruby on Rails, Java Servlets, etc. we also don't require to manage the server hardware and software. And we need to deploy our application as a single unit and we can specify the multiple instances for load balancing. Some of the example platforms which offer Paas Architecture are AWS Elastic Beanstalk, Heroku, etc.

But In the FaaS based Architecture, we can develop our application in the traditional manner because it works on the event trigger mechanism for calling the function or code. we also don't require to manage the server hardware and software for scaling because it auto-scales with respect to the load. And as I mentioned it doesn't follow traditional development patterns like frameworks. Some of the example platforms which offer FaaS Architecture are AWS Lambda, google cloud function, etc.

So, now we are good to focus on the specific topic as AWS Lambda Function. And I will be using NodeJS for writing the AWS Lambda Function.

What is Serverless Function?

Do some people think the hack is serverless is that function running without a server? It is quite obvious to get curious about the name. Basically, serverless means we don't need to think about the server software and hardware. we just need to focus on our code. And we also don't need to scale up and down because it automatically performs scaling. It means we don't need to think about managing the server. Hurrey, we are on the track to understand the serverless.

AWS Lambda Function ⚑

Actually, If you know the basics of programming languages like NodeJs, Python🐍, C#, Go, Java, etc then you can create the Lambda Function like a Pro😎.

So, for the pre-requisites for the AWS Lambda Function. You should be already aware of the AWS Management console at least for the proper understanding and implementation steps.

Once you reach the AWS Lambda dashboard it will look like this πŸ‘‡

lambda dashboard.png

So, Let's click on the create function for creating our AWS Lambda Function.

nameyourlabda.png

There will be four options on that page but we need to understand the basics. So, you need to click on the Author from Scratch or leave the default selected one. And enter one function name which suits your requirements. Then Choose your runtime environments as Node.js 14.x and leave all settings as default selected on that page and click on the create function button at the bottom of the page.

Hurray! now you have created a default configuration Lambda function. Let's break down the Lambda function Page for understanding.

functionpage.png

That page contains the area where you can edit code, test, and deploy. you can also do versioning and multiple environments like dev, staging, QA, prod, etc. And you will see this default lambda code πŸ‘‡.

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

First, let's understand what's the meaning of each and every line here. Basically, the AWS Lambda function triggered on some event that's why if you see there is one asynchronous function with the parameter event. And you can also send some context or data inside the function also. And we are exporting that handler function for calling it. And the return value works as an output for the AWS lambda function.

Let's test our default lambda code with a test case like this πŸ‘‡

testcase.png

After click on the test button, you will see the output like this πŸ‘‡

runcase.png

If you see the output as:-

{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}

then congratulations you successfully run the AWS Lambda code.

Now, I'll make a change in code by adding console.log for understanding the Monitor tab in the Lambda function. After updating the code please click on the deploy button for deploying a new updated version of the code.

exports.handler = async (event) => {
    console.log("Hi Readers");
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello Readers!'),
    };
    return response;
};

Then run the test case by clicking the test button. And go to the Monitor tab and click on the View logs in CloudWatch.

monitor.png

Then you will be redirected to cloudwatch page where you need to click on log stream with the latest date.

cloudwatch.png

Then you will see the logs for your AWS Lambda Function.

log.png

Now, We know how to write Lambda function code, how to test it, and also to debug it by logs.

So, you can play with the lambda function by changing the code, adding triggers, and also adding the destination for the output of the Lambda function.

Hope you understand the basics for creating AWS Lambda Function using NodeJs.

Β