9

I'm trying to run a λ code that creates a cluster, but nothing happens, maybe I'm misunderstanding the usage on Node (since I'm not that familiar with it.)

The function is as simple as:

// 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');
};

I'm testing it with the grunt-aws-lambda grunt task and in the console, but nothing shows except for:

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

Message
-------
λ Completed

Done, without errors.

Executing it from the AWS console results in the same output and no EMR Cluster is created.

Any thoughts on this?

Preston Badeer
  • 2,658
  • 1
  • 20
  • 21
Diego Magalhães
  • 725
  • 1
  • 10
  • 32

1 Answers1

10

AWSRequest sends requests asynchronously, but you are calling context.done in your main handler. This means that at best this is going to send the request but not wait for a response. context.done needs to be in the send callback or AWS will likely terminate the function before the response is received, or perhaps even before it is sent, depending on how the request is performed within the 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');
        });
};
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • Since it's just a cluster EMR create call, so I'm not really interested in the return. I'll try that as well. – Diego Magalhães Jan 22 '15 at 21:48
  • 2
    Great! with that edit I'm now able to see a validationError by the request, thanks a lot! – Diego Magalhães Jan 23 '15 at 00:01
  • Is it possible to apply the same, or similar logic and instead add a step to an existing cluster? We have a scenario whereby a step may silently fail upon which an SNS is sent. I'd like to cancel the step(most recent) and then effectively clone it – null Feb 17 '17 at 12:06