Search code examples
node.jsamazon-web-servicesaws-lambdaconsole.logaws-glue

Reason why lambda function calling AWS glue using Node.JS does not console.log?


I'm trying to kick off a AWS glue job using a lambda function, using node.js. I can test the lambda function fine, but nothing appear to happen after the script has run its course. I've added a few console.log lines, but during the SDK method call to start the AWS glue job, none of the console.log lines log anything (I am checking the output on the lambda code configuration page, and on CloudWatch). Am I missing something here? I tested the below using the in-browser "TEST" button.

var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-2'});

var glue = new AWS.Glue();

exports.handler = async (event) => {

    console.log("Hello!")
    var params = {
            JobName: 'ETL-store-inventory',
        };

    //Invoke job run
    glue.startJobRun(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });

    console.log("Done")
    
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

I get the following from the console:

Response:
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}

Request ID:
"e205ec08-dce1-4710-b944-f490544b1486"

Function Logs:
START RequestId: e205ec08-dce1-4710-b944-f490544b1486 Version: $LATEST

2019-05-03T17:17:55.427Z    e205ec08-dce1-4710-b944-f490544b1486    Hello!

2019-05-03T17:17:55.525Z    e205ec08-dce1-4710-b944-f490544b1486    Done

END RequestId: e205ec08-dce1-4710-b944-f490544b1486

REPORT RequestId: e205ec08-dce1-4710-b944-f490544b1486  Duration: 324.11 ms

Billed Duration: 400 ms     Memory Size: 128 MB Max Memory Used: 68 MB

Solution

  • Your function is returning and closing before the callback from your glue job returns. You can move the return inside the callback to make the function complete once the callback returns

    var AWS = require('aws-sdk'); AWS.config.update({region: 'us-east-2'});
    
    var glue = new AWS.Glue();
    
    exports.handler = async (event) => {
    
    console.log("Hello!")
    var params = {
            JobName: 'ETL-store-inventory',
        };
    
    //Invoke job run
    return glue.startJobRun(params, function(err, data) {
      if (err) {
        console.log(err, err.stack); // an error occurred
        const response = {
          statusCode: 200,
          body: JSON.stringify('An error occurred!'),
        };
        return response
      } else { 
        console.log(data);           // successful response
        console.log("Done")
        const response = {
          statusCode: 200,
          body: JSON.stringify('Hello from Lambda!'),
        };
        return response;
      }
    });