从节点应用程序调用amazon lambda函数 [英] Invoke amazon lambda function from node app
问题描述
我正在通过一个关于如何创建lambda函数的基本AWS:
I am going through a basic AWS on how to create a lambda function:
在这个例子中,我们正在创建一个图像重新调整大小的服务,触发它的一种方法是侦听一些图像被推送到S3存储桶然后执行lambda函数。
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 a S3 bucket and then lambda function will be executed.
但我试图了解如何从我的nodejs应用程序调用此lambda函数,当用户将图像发送到我的节点服务器时,我通过REST API将此图像发送到aws lambda以重新调整大小,然后接收新的图像位置作为响应。
But I am trying to understand how to invoke this lambda function from my nodejs app, when user send 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.
我可以效仿任何一种例子吗?我对实际的调用部分更感兴趣,因为我已经启动并运行了我的lambda服务。
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.
谢谢
推荐答案
由于您使用的是node.js服务器您可以使用AWS JavaScript SDK直接调用lambda( https://www.npmjs.com/package/aws-sdk )。这样您就不必担心使用API网关了。
Since you are using a node.js server you can just invoke your lambda directly with the AWS JavaScript SDK(https://www.npmjs.com/package/aws-sdk). This way you don't have to worry about using API Gateway.
从您的服务器调用非常简单:
Invoking from your server is as simple as:
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
});
请参阅此处的其余SDK文档: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html
See the rest of the SDK docs here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html
这篇关于从节点应用程序调用amazon lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!