1

I am trying to use AWS lambda together with ECMA6 classes in nodejs. Currently my code looks like this (simplified version of what I really want to do):

class testClass {
    constructor(str) {
        this.str = str;
    }

    async handler(event) {
        return {
            statusCode: 200,
            body: this.str,
        };    
    }
}

module.exports = new testClass('test');

When test this locally with

const testClass = require('./testClass');
const result = await testClass.handler();

It works as expected, but when I add this to a lambda function and call it, it returns

{
  "errorMessage": "Cannot read property 'str' of undefined",
  "errorType": "TypeError",
  "stackTrace": [
    "handler (/var/task/index.js:9:24)"
  ]
}

So it seems, that the constructor of the class is not called in the lambda context. Any idea, why this is the case and how to get around this issue ?

2
  • Why would you do this though? Commented Jan 5, 2019 at 15:50
  • this is just a simplified version of the code, in the real code the class exports also more functions that are used by other modules for pre-deployment stuff Commented Jan 8, 2019 at 12:37

4 Answers 4

3

You can use static function inside a es6 class, AWS expect a function with a callback param which will be executed. with the response, if you pass a class method, without instantiate the class, it will not work, that's why you should use static functions, this way:

// MyAwesomeClassLambda.js

class MyAwesomeClassLambda {
    static async myAwesomeClassFunction(event) {
        // return await someAsyncMethodLikeDataBaseCallOrSomethingLikeThat()
        return { message: 'hello world' }
    }
}

module.exports = MyAwesomeClassLambda

btw the function must be async, which is the equivalent behavior to the callback that is passed in the function version

In the AWS Lambda console, you must specify the function as: file_name.function_name, in this case MyAwesomeClassLambda.myAwesomeClassFunction

Sign up to request clarification or add additional context in comments.

1 Comment

I'm getting offline: Failure: offline: handler 'myAwesomeClassFunction' in /.../.webpack/service/src/handlers/MyAwesomeClassLambda is not a function Any ideas?
1

Unfortunately I didn't come up with any solution for this in a ECMA6 class way. In fact it works as I need it to if I go the old-school way and use a function instead:

function testClass(str) {
    const res = {};
    const input = str;

    res.handler = async (event) => {
        return {
            statusCode: 200,
            body: input,
        };    
    };

    return res;
}

module.exports = testClass('test');

Even though for my current work it is ok for now, in my opinion this is a bug in aws, as I don't see, why this works when run locally or with a function, but not when run on lambda.

Comments

1

Following will work but since you are not returning anything so you will see null in the response.

class testClass {
    constructor(str) {
        this.str = str;
    }

    async handler(event) {
        return {
            statusCode: 200,
            body: this.str,
        };    
    }
}

module.exports.handler = (event) => new testClass('test');

You can also do something like:

class testClass {
    constructor(str) {
        this.str = str;
    }

    async handler(event) {
        return {
            statusCode: 200,
            body: this.str,
        };    
    }
}

module.exports.handler = async (event) => {
    const test = new testClass('test');
    return await test.handler(event);
};

Comments

0

You can export the method itself as your handler.

module.exports.handler = new testClass('test').handler;

3 Comments

but then only the handler function would be exported, but in fact I need the whole class exported, as also other functions will be used (for pre-deployment stuff)
I just gave it a try and this doesn't work either. It fails with the same error message like when exporting the whole class
@Moamen Edited to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.