javascript 从节点应用程序调用亚马逊 lambda 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33659059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoke amazon lambda function from node app
提问by inside
I am going through a basic AWS on how to create a lambda function:
我正在阅读有关如何创建 lambda 函数的基本 AWS:
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.
在这个例子中,我们正在创建一个图像大小调整服务,触发它的一种方法是监听一些图像被推送到 S3 存储桶,然后 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.
但我试图了解如何从我的 nodejs 应用程序调用这个 lambda 函数,当用户将图像发送到我的节点服务器时,我通过 REST API 将此图像发送到 aws 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.
有什么我可以效仿的例子吗?我对实际调用部分更感兴趣,因为我已经启动并运行了我的 lambda 服务。
Thanks
谢谢
回答by Ryan
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.
由于您使用的是 node.js 服务器,因此您可以直接使用 AWS JavaScript SDK ( https://www.npmjs.com/package/aws-sdk)调用您的 lambda 。这样您就不必担心使用 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
});
See the rest of the SDK docs here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html
在此处查看 SDK 文档的其余部分:http: //docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html
回答by sdgfsdh
Here is an answer that is more idomatic for the latest JavaScript.
这是一个对最新 JavaScript 更符合习惯的答案。
import AWS from 'aws-sdk';
const invokeLambda = (lambda, params) => new Promise((resolve, reject) => {
lambda.invoke(params, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
const main = async () => {
// You shouldn't hard-code your keys in production!
// http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html
AWS.config.update({
accessKeyId: 'AWSAccessKeyId',
secretAccessKey: 'AWSAccessKeySecret',
region: 'eu-west-1',
});
const lambda = new AWS.Lambda();
const params = {
FunctionName: 'my-lambda-function',
Payload: JSON.stringify({
'x': 1,
'y': 2,
'z': 3,
}),
};
const result = await invokeLambda(lambda, params);
console.log('Success!');
console.log(result);
};
main().catch(error => console.error(error));
Update
更新
Rejoice! The AWS SDK now supports promises:
麾!AWS 开发工具包现在支持承诺:
import AWS from 'aws-sdk';
const main = async () => {
// You shouldn't hard-code your keys in production!
// http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html
AWS.config.update({
accessKeyId: 'AWSAccessKeyId',
secretAccessKey: 'AWSAccessKeySecret',
region: 'eu-west-1',
});
const params = {
FunctionName: 'my-lambda-function',
Payload: JSON.stringify({
'x': 1,
'y': 2,
'z': 3,
}),
};
const result = await (new AWS.Lambda().invoke(params).promise());
console.log('Success!');
console.log(result);
};
main().catch(error => console.error(error));