How to test AWS Lambda handler locally using NodeJS?

debug aws lambda python
run aws lambda locally nodejs
aws lambda uuid nodejs
aws sam local
how to deploy node js application on aws lambda
aws lambda nodejs
aws-lambda-node js-example-project
test lambda handler locally python

I am following these instructions to create a basic web scraper that executes in Lambda. I have experience writing selenium code, but not with Node JS. I got the project running in Lambda, but when I tried editing the project locally in order to execute the selenium code I want, It doesn't work. Anything in the exports.handler doesn't get executed when I run node index.js. How would I execute this project locally? Thanks!

You need to call your handler function from another file lets say testHandler.js in order to run via NodeJs.

This will be done like this

//import your handler file or main file of Lambda
let handler = require('./handler');

//Call your exports function with required params
//In AWS lambda these are event, content, and callback
//event and content are JSON object and callback is a function
//In my example i'm using empty JSON
handler.handlerEvent( {}, //event
    {}, //content
    function(data,ss) {  //callback function with two arguments 

Now you can use node testHandler.js to test your handler function.

EDIT: Sample Event and content data as requested


    "resource": "/API/PATH",
    "path": "/API/PATH",
    "httpMethod": "POST",
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
        "cache-control": "no-cache",
        "CloudFront-Forwarded-Proto": "https",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-Mobile-Viewer": "false",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Is-Tablet-Viewer": "false",
        "CloudFront-Viewer-Country": "IN",
        "content-type": "application/json",
        "Host": "",
        "origin": "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop",
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
        "Via": "2.0 (CloudFront)",
        "X-Amz-Cf-Id": "XXXXXXXXXX51YYoOl75RKjAWEhCyna-fuQqEBjSL96TMkFX4H0xaZQ==",
        "X-Amzn-Trace-Id": "Root=1-XXX03c23-25XXXXXX948c8fba065caab5",
        "x-api-key": "SECUREKEY",
        "X-Forwarded-For": "XX.XX.XXX.XXX, XX.XXX.XX.XXX",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    "multiValueHeaders": {
        "Accept": [ "*/*" ],
        "Accept-Encoding": [ "gzip, deflate, br" ],
        "Accept-Language": [ "en-GB,en-US;q=0.9,en;q=0.8" ],
        "cache-control": [ "no-cache" ],
        "CloudFront-Forwarded-Proto": [ "https" ],
        "CloudFront-Is-Desktop-Viewer": [ "true" ],
        "CloudFront-Is-Mobile-Viewer": [ "false" ],
        "CloudFront-Is-SmartTV-Viewer": [ "false" ],
        "CloudFront-Is-Tablet-Viewer": [ "false" ],
        "CloudFront-Viewer-Country": [ "IN" ],
        "content-type": [ "application/json" ],
        "Host": [ "" ],
        "origin": [ "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop" ],
        "User-Agent": [ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" ],
        "Via": [ "2.0 (CloudFront)" ],
        "X-Amz-Cf-Id": [ "XXXXXXXXXhCyna-fuQqEBjSL96TMkFX4H0xaZQ==" ],
        "X-Amzn-Trace-Id": [ "Root=1-XXXXXXX67339948c8fba065caab5" ],
        "x-api-key": [ "SECUREAPIKEYPROVIDEDBYAWS" ],
        "X-Forwarded-For": [ "," ],
        "X-Forwarded-Port": [ "443" ],
        "X-Forwarded-Proto": [ "https" ]
    "queryStringParameters": null,
    "multiValueQueryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "resourceId": "xxxxx",
        "resourcePath": "/api/endpoint",
        "httpMethod": "POST",
        "extendedRequestId": "xxXXxxXXw=",
        "requestTime": "29/Nov/2018:19:21:07 +0000",
        "path": "/env/api/endpoint",
        "accountId": "XXXXXX",
        "protocol": "HTTP/1.1",
        "stage": "env",
        "domainPrefix": "xxxxx",
        "requestTimeEpoch": 1543519267874,
        "requestId": "xxxxxxx-XXXX-xxxx-86a8-xxxxxa",
        "identity": {
            "cognitoIdentityPoolId": null,
            "cognitoIdentityId": null,
            "cognitoAuthenticationType": null,
            "userArn": null,
            "apiKeyId": "xxXXXXxxxxxx",
            "userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
            "accountId": null,
            "caller": null,
            "sourceIp": "",
            "accessKey": null,
            "cognitoAuthenticationProvider": null,
            "user": null
        "domainName": "",
        "apiId": "xxxxx"
    "body": "{\n    \"city\": \"Test 1 City\",\n    \"state\": \"NY\",\n    \"zipCode\": \"11549\"\n}",
    "isBase64Encoded": false


    "callbackWaitsForEmptyEventLoop": true,
    "logGroupName": "/aws/lambda/lambda-name",
    "logStreamName": "2018/11/29/[$LATEST]xxxxxxxxxxxb",
    "functionName": "lambda-name",
    "memoryLimitInMB": "1024",
    "functionVersion": "$LATEST",
    "invokeid": "xxxxx-xxx-11e8-xxx-xxxxxxxf9",
    "awsRequestId": "xxxxxx-xxxxx-11e8-xxxx-xxxxxxxxx",
    "invokedFunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxx:function:lambda-name"

Step-Through Debugging Node.js Functions Locally, The following is an example that shows how to debug a Node.js function with Code for step-through debugging Node.js functions with the AWS SAM CLI, use the This was tested with Microsoft Visual Studio Code versions 1.26, 1.27, and 1.28 for Previous topic: Step-Through Debugging Lambda Functions Locally . The handler is the method in your Lambda function that processes events. When you invoke a function, the runtime runs the handler method. When the handler exits or returns a response, it becomes available to handle another event.

This is what I did:


exports.handler = async (event) => {
    console.log('hello world');

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!')

    return response;


"scripts": {
  "locally": "node -e \"console.log(require('./index').handler({}));\""


npm run locally


> node -e "console.log(require('./index').handler({}));"

hello world
Promise { { statusCode: 200, body: '"Hello from Lambda!"' } }

Step-Through Debugging Lambda Functions Locally, You can use AWS SAM with a number of AWS toolkits to test and debug your serverless on port 5858 sam local invoke -d 5858 <function logical id> # Start local API Gateway in debug Step-Through Debugging Node.js Functions Locally. Let’s test with my basic go to frameworks, Mocha and Chai, and use a very simple Lambda function just to facilitate the examples in this article. The following represents the Lambda function

In your index.js, just defined and exported a handler function, but no one calls it. In the Lambda environment, some AWS code will call this handler with message. In your local environment, you have to call your handler by yourself.

You could also have a look of this doc, it is a way to "simulate" Lambda in local environment.

AWS Lambda with Node.js: A Complete Getting Started Guide, many people. So let's talk AWS Lambda, serverless, and how to build a scalable image processing app using AWS Lambda and Node.js. It allows you to run code without having to deal with servers in the cloud. An event Your Lambda function won't live on your local environment forever. It needs to  In this tutorial, we will see how to use AWS Lambda And Node.js. In this post, we will see how we can configure the serverless on mac and up and running with aws lambda with nodejs from scratch. We will look at how we can deploy a simple test aws-nodejs application. But before the start, we need to understand some terminology.

You can check out lambda-local. It's a little fancier than the accepted answer above. For example, it supports passing environment variables and using JSON files for your payloads.

lambda-local, Lambda-local lets you test NodeJS Amazon Lambda functions on your local As an API: You can also use Lambda local directly in a script. profilePath, optional, path to your AWS credentials file Level 2 dismiss handler() text, level 1 dismiss lambda-local text and level 0 dismiss also the result. callback  This is a follow up to my earlier article on doing integration tests on lamda locally. But what about unit tests on serverless nodejs lamdas. The following is a guide to developing unit tests using…

Debug AWS Lambda functions in Node.js on VS Code, Perform step-through debugging using your own local IDE with SLAppForge. Debugging AWS Lambda functions in Node JS using VS Code The Lambda function connects with the IDE through the SLAppForge Debug Next, let's create a test event and invoke it to see if our Lambda works correctly. In order to test Lambda using step debugging, you'll need to focus on two domains - the hardware on which it runs, and the way in which your Lambda function is invoked. The hardware is challenging to emulate, as AWS keeps the particulars of the machine instances that run your lambda functions secret.

Testing [Nodejs] Lambda functions - DEV, Let's consider a typical Nodejs Lambda function callback is just a function passed in to return information, we can create similar objects and use them for our local test. var context = { awsRequestId: Math.random(). I'm not sure if this question is still relevant or not, but I'm using DEEP Framework to test the code locally and/or deploy it on AWS Lambda. Check this out: npm install deepify -g deepify run-lambda --help run-lambda@1.6.8 - Run Lambda function locally Usage example: deepify run-lambda path/to/the/lambda -e='{"Name":"John Doe"}' Arguments: path: The path to the Lambda (directory of handler

Running and debugging AWS Lambda functions locally with the , For Node.js at least, the Serverless framework and VS Code Emulate an invocation of your AWS Lambda function locally using Put the two together and you have the ability to locally run and debug your Lambda functions. 1) A crash course on Serverless with Node.js 2) Building a Serverless REST API with Node.js and MongoDB 3 13 3) Migrating your Node.js REST API to Serverless 4) A crash course on securing Serverless APIs with JSON web tokens 5) Getting Started with AWS Lambda and Node.js 6) How to deploy a Node.js application to AWS Lambda using Serverless 7) Solving invisible scaling issues with

  • You need to call your function, not just export the declaration. Import your exported handler.js assigned to a variable in index.js and call your function for example. (Assuming exports.handler is a function.)
  • Can you show an example of what event usually looks like when called on AWS Lambda?
  • @PetrusTheron added sample event and content.
  • The events look different depending on the trigger for the event. In the web UI to edit a lambda, the test event selection dropdown has "Configure Test Event" as one of the choices. In there, they offer prototype events for all the types of triggers.