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
This will bring to a new window Create the Function as follows:
- Function name
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.
We have to select Author from scratch and fill in the basic information for the lambda function needed for creation.
After filling the function name > click on Create Function
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.
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.
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.
After every edit, the deploy button will pop up to deploy the latest changes to your own lambda function. Click on deploy.
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.
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.
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.
Congratulations you have successfully created your very own lambda function.
To know more about AWS Lamda Function, you can email to us at firstname.lastname@example.org.