Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I am going through a basic AWS on how to create a lambda function:

http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-create-function.html

In this example we are creating an image re-sizing service, one way to trigger it is to listen for some image to be pushed to an S3 bucket and then lambda function will be executed.

But I am trying to understand how to invoke this Lambda function from my nodejs app when the user sends an image to my node server, I send this image to aws lambda via REST API to be re-sized and then receive the new image location as a response.

Is there any kind of example I can follow? I am more interested in the actual invocation part since I already have my lambda service up and running.

Thanks

1 Answer

0 votes
by (44.4k points)

Using AWS Javascript SDK, you can invoke Lambda directly because of the node.js server (https://www.npmjs.com/package/aws-sdk).

To invoke from your server:

var AWS = require('aws-sdk');

// you shouldn't hardcode your keys in production! See http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html

AWS.config.update({accessKeyId: 'akid', secretAccessKey: 'secret'});

var lambda = new AWS.Lambda();

var params = {

  FunctionName: 'myImageProcessingLambdaFn', /* required */

  Payload: PAYLOAD_AS_A_STRING

};

lambda.invoke(params, function(err, data) {

  if (err) console.log(err, err.stack); // an error occurred

  else     console.log(data);           // successful response

});

AWS SDK documentation is here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...