我正在尝试运行一个创建集群的λ代码,但没有任何反应,也许我误解了Node上的用法(因为我对它并不熟悉。)
该功能非常简单:
// configure AWS Dependecies
var AWS = require('aws-sdk');
exports.handler = function(event, context) {
// EMR Client
var emr = new AWS.EMR({apiVersion: '2009-03-31', region: 'us-east-1'});
var params = {... dozens of params describing jobs ...};
var AWSRequest = emr.runJobFlow(params);
AWSRequest
.on('success', function(response){ console.log("success => " + response)})
.on('error', function(response){ console.log("error => " + response)})
.on('complete', function(response){ console.log("complete => " + response)})
.send( function(err, data){
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
context.done(null, 'λ Completed');
};
我正在使用grunt-aws-lambda
grunt任务在控制台中测试它,但除了:
aws-emr-lambda$ grunt lambda_invoke
Running "lambda_invoke:default" (lambda_invoke) task
Message
-------
λ Completed
Done, without errors.
从AWS控制台执行它会产生相同的输出,并且不会创建EMR群集。
对此有何想法?
答案 0 :(得分:10)
AWSRequest
异步发送请求,但您在主处理程序中调用context.done
。这意味着最好这将发送请求但不等待响应。 context.done
需要在send
回调中,或者AWS可能会在收到响应之前终止该功能,或者甚至可能在发送之前终止该功能,具体取决于在AWS SDK中执行请求的方式。 / p>
exports.handler = function(event, context) {
// EMR Client
var emr = new AWS.EMR({apiVersion:'2009-03-31', region:'us-east-1'});
var params = {... dozens of params describing jobs ...};
var AWSRequest = emr.runJobFlow(params);
AWSRequest
.on('success', function(response){ console.log("success => " + response)})
.on('error', function(response){ console.log("error => " + response)})
.on('complete', function(response){ console.log("complete => " + response)})
.send( function(err, data){
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
context.done(null,'λ Completed');
});
};