9

我正在尝试运行创建集群的 λ 代码,但没有任何反应,可能是我误解了 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-lambdagrunt 任务和控制台对其进行测试,但除了以下内容之外什么都没有显示:

aws-emr-lambda$ grunt lambda_invoke
Running "lambda_invoke:default" (lambda_invoke) task

Message
-------
λ Completed

Done, without errors.

从 AWS 控制台执行它会产生相同的输出,并且不会创建 EMR 集群。

对此有什么想法吗?

4

1 回答 1

10

AWSRequest异步发送请求,但您在主处理程序中调用context.done。这意味着充其量这将发送请求但不等待响应。context.done需要在send回调中,否则 AWS 可能会在收到响应之前终止函数,或者甚至在发送响应之前终止函数,具体取决于请求在 AWS 开发工具包中的执行方式。

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');
        });
};
于 2015-01-22T21:12:22.817 回答