Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda to create EMR Cluster don't fire the cluster creation

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?

like image 860
Diego Magalhães Avatar asked Jan 22 '15 20:01

Diego Magalhães


People also ask

Can Lambda trigger EMR?

The EMR cluster can take up to 10 minutes to start. In the meantime, we can trigger our lambda function by sending a sample data to our input bucket. This will cause the lambda function to add the jobs to our EMR cluster. In the AWS EMR UI, you can see that the steps are added to your EMR cluster.

How do I create a new EMR cluster?

Step 1: Navigate to the Analytics section and click on "EMR". Step 2: Navigate to Clusters and select Create Cluster. Step 3: Give the name of the cluster, the location in S3 where you want to store the log file, the applications you want to install, instance type, Key pair and the roles and Click on Create Cluster.


1 Answers

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');
        });
};
like image 148
Dark Falcon Avatar answered Oct 11 '22 07:10

Dark Falcon