Getting Started With AWS Lambda and Node.js

In the previous post of this series, we have talked about AWS Lambda, AWS SAMand what is serverless computing in general. In this post, we will write a small function that can be deployed to AWS Lambda using javascript.

Pre-requisites

You will need access to AWS Console and some familiarity with javascript and AWS Lambda. If you would like to read the previous articles in this series, follow the link attached to the keywords.

Creating a function using AWS Console

AWS Lambda provides you with an editor to author your function directly from AWS Console. To begin follow the steps:

  • Once you are logged into the AWS Management Console, navigate to Lambda and click on Create Function.
  • You will see the following screen
Aws Lambda Create Function
  • Let us understand the above options. The selected one is to author the function from scratch using the editor provided by AWS.
  • The second option is to use a blueprint application provided by AWS. These will include the samples for some of the use cases like reading data from DynamoDB etc.
  • If you have a container image that you have to build somewhere else, you can also use it to spin up a Lambda function.
  • Serverless Application Repository contains the sample applications which can be deployed and used by you. Many people have shared their serverless application with the complete setup like required events and resources to make a generic use-case work. You can deploy one and start from there rather than building something from scratch.
  • Once you clicked on the Author from Scratch, you have to provide the name of the function. Keep the rest of the stuff the same as we will talk about them in an upcoming post in the series.
An Editor to Author a function
  • The above image shows the editor which we will use to create a function.
  • Click on index.js to open the file in the editor. it will have some basic code to get you started.
Handler function
  • Index.js contains a handler function which is the starting point to enter into the Lambda function when it is executed.
  • An event is an object which will receive all the information passed on via a trigger like API gateway or custom information if you executed a function with a piece of custom information.
  • let\’s delete everything from lines 2 to 7 and instead console the event over there
console.log(event)
  • The new function will look like below
exports.handler = async (event) => {    console.log(event);};
  • Click on deploy to deploy the changes in the function.
  • To test the function, click on the Test tab next to the Code tab.
Testing a Lambda Function
  • Click on the test button to start the execution of the function. Once the execution is complete, you will receive the following screen with all the details of the execution.
Execution of a function
  • Notice the log output, It printed out the event which we passed on to the function. If you like to change the data passed on to function, change the JSON object in the previous screen and try again. The output will change to the new JSON object.

Congratulations, you have created your first AWS Lambda function using Node.js. In the upcoming post, we will talk about creating a boilerplate and deploying it using AWS SAM. Till then Happy Coding.