DEV Community

Cover image for Running lambdas locally using Javascript/Node.js
Rashi Agarwal for AWS Community Builders

Posted on

Running lambdas locally using Javascript/Node.js

Tired of deploying to the cloud just to test your AWS Lambda? It’s like mailing yourself a letter just to proofread it—slow, expensive, and totally unnecessary.

What if you could invoke your Lambda locally—right from your JavaScript application? No cloud round-trips, no waiting, just instant feedback. With AWS SAM (Serverless Application Model) and a sprinkle of Node.JS/JavaScript, you can test and debug your Lambdas on your machine, saving time, money🤩.

Pre-Requisites:

I am using a Windows PC and have the following things set-up before proceeding ahead:

  • AWS SDK
aws --version
Enter fullscreen mode Exit fullscreen mode
  • SAM CLI
sam --version
Enter fullscreen mode Exit fullscreen mode
  • Docker
docker --version
Enter fullscreen mode Exit fullscreen mode
  • Node.js
node --version
Enter fullscreen mode Exit fullscreen mode

Let’s get started!

We will start by creating a demo application using init command and then further proceed to invoke lambda within the application code (Nodejs).

1> Navigate to the folder where you want to set up your project.

sam init --runtime nodejs
Enter fullscreen mode Exit fullscreen mode

This will create a basic Hello world application.It has a simple Lambda handler which it takes in the event and returns back the data received from a particular URL, along with a Hello World message.

Let's take a deeper dive into what the files implement and how the code works:

  • SAM Template (template.js) -> it implements HelloWorld Function where it uses the Code URI property set to Hello World SLASH. This implies that the Lambda handler code is inside the Hello World folder.
    • app.js -> This is referred in the Handler property of the SAM template, it contains exported function called Lambda Handler.

You will notice a dependency named 'axios' in package.json, note that the module is simply used to make HTTP requests to the given URL.

2> Install the dependency from package.json by navigating to the folder having the file, then use the command:

npm install
Enter fullscreen mode Exit fullscreen mode

3> Now, start the Lambda service locally by running the command.

sam local start-lambda
Enter fullscreen mode Exit fullscreen mode

So, now the local Lambda service is running on port 3001.

4> We will invoke our lambda functions from within our application code using the AWS SDK. To do that, let's create a sample node.js project. Create a new folder, navigate to the folder and enter the command.

npm init
npm install aws-sdk --save
Enter fullscreen mode Exit fullscreen mode

5> Now create test.js file and reference the AWS SDK, configure the AWS region,instantiate a Lambda object.

const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-west-2' });
const lambda = new AWS.Lambda({
      endpoint: 'http://127.0.0.1:3001'
});

var parameters = {
    FunctionName: 'HelloWorldFunction',
    PayLoad: new Buffer('{}')
};

lambda.invoke(params, (e,data) => {
  if(e){
    console.log("Error: "+e);
  } else{
      console.log(data);
  }
});

Enter fullscreen mode Exit fullscreen mode

The parameter object here, invokes the Lambda Function, replace the function name according to the application code. The endpoint refers to the one on which your local lambda service is started.

6> Now run the command in a new terminal:

node test.js
Enter fullscreen mode Exit fullscreen mode

7> Verification and Testing: When you navigate back to the AWS SAM powershell, you can see it invokes Lambda handler locally, by first loading the function in a Docker container and execute it with our input event data.
Now navigate back to the other terminal, you can see the response from the Lambda function, with status code 200 ✅, message Hello World, and the location IP address😄.

Wrapping Up

And there you have it—a faster, smarter way to test your AWS Lambda functions without ever leaving the comfort of your local setup. By invoking Lambdas locally with JavaScript and NodeJS, you’ve unlocked a whole new level of efficiency. Now you can iterate faster, debug smarter, and build cooler apps—all while keeping your cloud costs low.

Happy coding, and may your Lambdas always run as smoothly locally as they will in the cloud! 🎉

Top comments (0)