AWS Lambda function with Nodejs

Ruminder Singh
6 min readApr 3, 2021
Src:// https://docs.aws.amazon.com/

AWS Lambda make it easy to deploy our code without setting up any server. All the logic of our features can be written on Lambda functions, which run only when request is made to them. It automatically scales depending on the requests.

In this post we will see

  1. How to create functions in Node.js using different libraries.
  2. Accessing Lambda function using API Gateway
  3. Brief intro on how to secure your Lambda function to be accessed only by authorized users.

Lambda Function

Function where we put our application logic to be run on servers without managing them. Before starting with Lambda function, make sure that your code can run in the runtime environment provided in AWS Lambda. It support most used environment like Python, Java, Node, Ruby, .Net and Go.

  • Login to you account and search for AWS Lambda service or click on this.
  • Click on create function. It will open the page to setup runtime environment of the lambda function.
  • Keep the “Author from scratch” selected as we are going to create our function from scratch.
  • Enter function name and select Node 14.0 which is latest version on node at the time of writing. Keep rest of the setting same.
Lambda function create with simple function.

It will create a folder with index.js file. Runtime setting are at bottom of the page which provide us information of runtime and handler. Handler can be thought as method which will be actually called when request is received. By default it handler, as in our index.js file handler method is only written. If you change the method name, change it in runtime settings as well, else no code will run.

If you are not using any other libraries, you can directly edit your code and save it. However if you are using libraries like lodash or axios, you will not be able to install the packages directly. To do that, create function on our local machine with all the libraries and then upload it to the AWS Lambda.

Initialize the node project using “npm init” command. We will be using axios package for demo. Run “npm i axios” command to install. Create index.js file and add following code to it.

Code making use of axios to hit numbersapi and send back response.

The above code uses axios library to call NumbersAPI and send response from it back to the client. After adding all the libraries and files, create a zip file by selecting all the files, as we want these files at function level not under the subfolder.

Click on the Upload from, select from .zip option and upload your zip folder created.

After uploading folder you code with node_module and package.json files will be reflecting under the function folder.

Files and code after uploading zip folder.

Whenever you make change always click on Deploy to check your changes else your code will not be updated on deployment and you will be using old code. To test this code create test case by clicking on Test.

Sample test case created for testing our function.

AWS provides event template for testing. We can either use them or edit to our own requirements. I have added number and type in json. We can give any name to event name (giving sample case a name).

After creating the test case and deploying our latest code, when we run it will give following error.

It occurred as we have renamed the handler to “lambdaHandler”. Just update in runtime settings and our code will work fine. Format of handler is “fileName.handlerName”.

Updated Runtime settings.
Test result of running code, showing the logs and time for which it run (Billed duration).

API Gateway

We have create the AWS Lambda function, but how do we access from our application. Thats where API Gateway comes. It creates link between application (or front end) with the code in AWS Lambda.

We will create an API which will be linked to our Lambda function just created. To create an API follow this link. Select Rest API with complete control.

API options to link lambda function.

Select “New API” and enter the name of the API. You can give any name, try to give it what your lambda function is doing. It will be easy for you to know which API is linked to which Lambda functions.

Create page of API.

Further we will first create resource as we do not want our all the APIs under root resource. Click on Actions dropdown and select “Create Resource”. Enter the name. You can select to “Enable API Gateway CORS” but it will not affect much.

Creation of new Resource “first-lambda”.

Now we will add our method to link Lambda function to it. We will follow same step by selecting the created resource, click on Action dropdown to create method. Now to keep it simple we will set type of method to POST. If you are setting method type to GET, you will need to perform further step of linking parameters. When you start typing in Lambda function, it will automatically show you all the Lambda function you have created starting with entered keyword.

Integrating Lambda function myFirstFunction with API Gateway.

We have created our API, which needs to be deployed now. Click on “Actions” and select deploy API. We will create a new stage or select from existing stages. Stages can be like testing, prod as our product grows we will be modifying our function so to keep the API function running for customers while we test our functionalities at other stage. Before deploying your API you can alway test it by selecting the method and clicking on “Test”. Pass the data inside the body or query string and click test. It will tell you if your are passing the data correctly.

Deployed API settings.

The url of API will be shown at the top after deploying API. You can always find the url by going to your Lambda function. It will show you the Lambda function linked with API Gateway. In details of API Gateway, you will find the API end point.

API Gateway linked to the Lambda function.
Using API end point on Postman .

Our Lambda function through API Gateway is working perfectly. Currently, anybody with the URL can invoke Lambda function. It is fine as of now our code is not doing much. However you would want to secure your Lambda functions to be invoked by 3rd party. There are different ways to secure the API Gateways access which are.

  1. You can allow only specific IP can access the API.
  2. Using Amazon Cognito User pool to attach access token while accessing API.
  3. Using API Gateway Lambda Authorizer which will use Lambda function to verify the user.

I hope it will help you creating your first AWS Lambda. AWS Serverless is not just AWS Lambda, but it will help getting started with serverless technology which is already popular now. Stay tuned for further posts.

If you have any questions, please feel free to connect with me on LinkedIn.

--

--