Back

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

I would like to use the ES6 class syntax in AWS Lambda using Node 6.10, but I cannot get it to work:

class widget {

    constructor(event, context, callback) {

        callback(null, `all seems well!`);

    }

}

 

// module.exports.handler = widget; // "Process exited before completing request"

module.exports.handler = new widget(); // "callback is not a function"

Has anyone had success with using the class syntax? The class constructor does not get seen as a handler function apparently.

closed

1 Answer

0 votes
by (44.3k points)
selected by
 
Best answer

Lambda expects an API and you are not following it. As in this documentation, Lambda expects:

exports.myHandler = function(event, context, callback) {};

Then it would call this:

const handlers = require('your-module');

handlers(); 

The ES6 classes have to create with new. That is the issue. As the Lambda function is expecting a function, the function should be callable and not constructible. For example. Check this:

class widget {

  constructor(event, context, callback) {

    callback(null, `all seems well!`);

  }

}

 

exports.myHandler = function(event, context, callback) {

    new widget(event, context, callback);

}; 

Related questions

Want to get 50% Hike on your Salary?

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

0 votes
1 answer
0 votes
1 answer

Browse Categories

...