1

I am using a nodejs application and w.r.t this link :- http://blog.rowanudell.com/testing-serverless-functions-locally/.

I was able to call the lambda function locally with the command : $ lambda-local -f index.js on CLI.

Is there a way to trigger the same from inside a .js file of nodejs application?

1
  • You can exec any external command using nodejs.org/api/child_process.html. Would that work for you, or do you need it to be via code that you've imported (e.g. as a required node module)? Commented Jun 7, 2017 at 13:26

1 Answer 1

1

This should do the job:

// set the event data you want here
const event = {};
// mock the context if needed here
const context = {};
// retrieve the result of the lambda here
const cb = (err, res) => {
  console.log(err, res);
};

// load the file containing the Lambda handler
const lambda = require("./path/to/index.js");
// call the handler
lambda.handler(event, context, cb);
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, this works from server side code(or nodejs terminal). However, I want to invoke lambda function from client side code (angularjs code). anythoughts? Is there any way to host my lambda function locally hit that from angularjs client side code?
The question was about "inside a .js file of nodejs application", so I guess this is another question. If you want to expose a Lambda to be able to call it from a browser, you should use API Gateway. I do not understand what you mean by "to host my lambda function locally hit that from angularjs client side code".
You should call API gateway from your Angular application and not try to execute the lambda locally. Otherwise, you'll have to run a Node.js app that simulates API Gateway and calls the function. I would recommend to create a Lambda and an API endpoint for development purpose in AWS.
Yes, the lambda function will be called via APIGateway. So, is there a way to setup the APIGateway locally? I tried using localstack (as described in github.com/atlassian/localstack) , and was able to create lambda function locally(on linux machine); but, not able to configure APIGateway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.