What is AWS Lambda?

AWS lambda is nothing but the cloud function which runs on the cloud without any server infrastructure. It is the service provided by AWS in which we can use any server language we want e.g: Nodejs, Python, Java etc. And we are going to look into AWS Nodejs Lambda in this tutorial.

AWS Nodejs Lambda Overview:

In this AWS Nodejs Lambda tutorial, we are going to dig a little bit into “How AWS Lambda works and how to create it from scratch?”. This tutorial will consist of two parts. As there are lots of things need cover into it. Part 1 will cover the basics of What is AWS Lambda, its purpose, in which scenario we have to use lambda. In part 2 we will create actual HTML to PDF lambda in Nodejs.

To cover whole AWS Lambda from scratch today we will take simple lambda function which will act as an HTML to pdf creator service. This lambda function will be in Nodejs and will use a couple of packages to create HTML to pdf e.g: dynamic-html-pdf. At the end of the tutorial, we will be having our own simple HTML to PDF creating service like Pdfcrowd which is paid(Pricing starting from 11$/month)

Background execution of Nodejs lambda:

Before proceeding with AWS Nodejs Lambda let me clear some fundamental things about the execution of it in Nodejs. AWS Nodejs lambda is nothing but the simple Nodejs server application. Which can be executed in various ways e.g: as an API, as a trigger from lambda, trigger from s3, or directly from the AWS console itself.

Just keep in mind, In lambda and specifically in Nodejs we cannot execute background code like cron jobs or long running tasks.

For example: If we want to update users information into the database using lambda by hitting HTTP post request from the client side and revert back with the successful response to the client from the server(lambda). But at the same time, we want to do some other operations after updating user information into the database. Just like post save kind of thing but without holding client for a response(Like background execution). In this scenario as soon as we send the response back to the client, lambda will automatically terminate all running execution in the background. This won’t happens on our own server or our local machine. This happens on lambda because to prevent the unwanted execution of code on the server for a long time. It will result in the high amount of billing from aws.

By default, the time for execution of lambda function is of 3 seconds. If your code will not get executed in 3 seconds then it will break execution with time limit exceeded error. We can increase this time limit from the aws lambda console, upto 5 mins. But be careful about it, if you are increasing it to 5 mins then it will cost more according to your code execution time. Instead, we can optimize our code to execute within default time limit.

Lambda invocation methods:

1. From any other lambda function.
2. Via API with the help of API gateway service of aws.
3. Via trigger from aws services like S3, Cognito webhooks etc.

We can trigger lambda from any of the server based languages which are having AWS SDK. In Nodejs we can trigger lambda as below:

Lambda invoke
var params = {
    // the lambda function we are going to invoke
    FunctionName: 'lambdaB',
    InvocationType: 'RequestResponse',
    LogType: 'Tail',
    //parameters
    Payload: '{ "name" : "Alex" }'
};

lambda.invoke(params, function (err, data) {
    if (err) {
        context.fail(err);
    } else {
        context.succeed('lambdaB said ' + data.Payload);
    }
})

This code snippet is the example of lambda invocation from Nodejs to Nodejs lambda. We can invoke cross-language lambda as well. But before procedding with the execution of lambda, we have to make sure that lambda is having sufficient permissions to execute another lambda.

Lambda execution permissions:

We have to give following roles to lambda:
AWSLambdaExecute and AWSLambdaBasicExecutionRole
It can be done from IAM service of aws.

For More information about refer to this stackoverflow question.

For more, you can also check tutorial to send email using aws nodejs lambda.

Requirements to get started for creating AWS Lambda from scratch.
1. AWS account. (You can get it by simply signup on amazon aws for 1 year free tier)

Please refer actual implementation of html to pdf lambda from scratch in AWS nodejs lambda part 2 tutorial.

You have already voted.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.