Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (19.1k points)

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?

1 Answer

0 votes
by (44.4k points)

As you are calling context.done which needs to be in the send callback or AWS will terminate the function even before the response is received due to the timeout because AWSRequest sends request asynchronously.

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

        });

};

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...