Build Your First AWS Lambda Function with Node JS

by SEO

Build Your First AWS Lambda Function with Node JS

by SEO

by SEO

In this post, we’ll learn what Amazon Web Services (AWS) Lambda is. In order to show how useful Lambda can be, we’ll walk through creating a simple AWS Lambda Function with Node JS programming language.

We’ll test it out, as well as take a look at what Lambda provides for metrics and logging.

By the end of this tutorial, you’ll be ready to start integrating other AWS serverless frameworks using Node JS Lambda functions as the glue to bind them all together.

Note that the usage of the term Lambda here is not related to anonymous functions in Node JS. Which are also known as lambda functions.

Serverless OR why you don’t need a server!

This is still the proper way to handle lots of situations, sometimes you just need to run a piece of code to deal with a few kinds of events.

For instance, let’s say you have an application that permits customers to upload a photo for a profile picture. These images are stored in AWS S3 Bucket.

Now, say you want to resize the photo to a max resolution, you can virtually manage this project using the conventional models.

But because this code runs totally on an event (the file addition), Amazon S3 can fire an event and cause the execution of code to deal with the photograph resize.

AWS Lambda is the glue that binds many AWS services together, including S3, API Gateway, and Dynamo DB.

You won’t have to pay for server costs and its uptime; you only have to pay for the request you make to a function.

This is very much cheaper and it is auto-scalable as Amazon automatically scales and load balances it.

Start by creating your first AWS Lambda Function with Node JS

Create the Lambda function on the AWS Lambda homepage by clicking the Create a Function button

creating your first Node JS Lambda function

This will bring to a new window Create the Function as follows:

  • Function name
  • Runtime

For Permissions, Lambda will automatically create a basic execution role so that the Lambda function can access Cloud Watch for logs. We’ll just leave it at that for now.

After Clicking on the CREATE Function you will see many options or ways to create your Lambda function using the existing repository, a blueprint, or author it from scratch.

We will choose the author from scratch.

Create Function AWS

We have to select Author from scratch and fill in the basic information for the lambda function needed for creation.

lambda function

After filling the function name > click on Create Function

Lambda Console

Lambda console will be launched once your function is created which will hardly take few seconds provided your internet connection is stable.

The lambda console consists of an overview where you can add triggers and destinations and other information in context to your lambda function.

Scroll down and you will have your own code editor.

Code Editor

This is your code editor where you can write, deploy and test on this live environment.

Click on the index.js which is your main handler function.

Index.js file

A pre-writer boilerplate code is already written for you in Node JS, you just need to customize and modify as per your needs.

Let’s change the body function to an introductory congratulations message.

Congratulations Message

After every edit, the deploy button will pop up to deploy the latest changes to your own lambda function. Click on deploy.

Lamda Function2

Now, your lambda function is all ready to go for its use as an API or a stand-alone function but the lambda console provides a test environment to test locally by providing raw inputs.

Click on Test.

Test Command

You need to configure a test event for your lambda function. Provide an event name and input for your function but as we have nothing to input.

We are just getting a hello message from the lambda function, for now, we will pass an empty JSON and configure it.

Lamda Function3

So we have configured it, let’s create a test for it. Click on create.

After clicking on create, the test will run on your lambda function. Which will get you the following results if it has been executed successfully.

Lamda Function Created Successfully

Congratulations you have successfully created your very own lambda function.

To know more about AWS Lamda Function, you can email to us at info@troology.com.

Top
Close Bitnami banner
Bitnami