4

I am trying to implement a simple Node Js example on AWS Lambda,
The code has a sample example of Async library.
The code works fine, but from some reason the Lambda Function return null response.
I am new to Node as well, please help.

Following is the code -

var async = require('async');

exports.handler = async (event, context, callback) => {
    async.waterfall([
        function(callback) {
            console.log("ONE");
            callback(null, 1);
        },
        function(resp, callback) {
            console.log("TWO : ", resp);
            callback(null, 2);
        },
        function(resp, callback){
            console.log("THREE : ", resp);
            callback(null, "Done");
        }
    ],
    function(err, resp) {
        let response = {};
        if (err) {
            console.log("Error",err);
            response = {
                statusCode: 500,
                body: JSON.stringify('Error!'),
            };
            return response;
        } else {
            console.log("Success",resp);
            response = {
                statusCode: 200,
                body: JSON.stringify('Ok!'),
            };
            return response;
        }
    });
};

Following are the CloudWatch logs -

START RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861 Version: $LATEST
2019-03-21T15:15:26.597Z    ab9aa426-dfc9-44ac-8d96-a4f102e30861    ONE
2019-03-21T15:15:26.597Z    ab9aa426-dfc9-44ac-8d96-a4f102e30861    TWO :  1
2019-03-21T15:15:26.597Z    ab9aa426-dfc9-44ac-8d96-a4f102e30861    THREE :  2
2019-03-21T15:15:26.597Z    ab9aa426-dfc9-44ac-8d96-a4f102e30861    Success Done
END RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861
REPORT RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861  Duration: 37.28 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 67 MB  

I used the sample Node blueprint, which works fine -

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};
7
  • How are you invoking the lambda? Commented Mar 21, 2019 at 15:23
  • using the test option from console Commented Mar 21, 2019 at 15:24
  • Wouldn't there be a conflict here with async from the library async.js and native async? Commented Mar 21, 2019 at 15:24
  • @AniruddhaRaje When you invoke it from the console, what invocation type are you using? Event? RequestResponse? Commented Mar 21, 2019 at 15:25
  • I used the default params but, my code doesn't process them, just does a simple async lib use and should return the "response" object Commented Mar 21, 2019 at 15:28

3 Answers 3

6

Since you're already using Node 8, you don't need to use the outdated, confusing callback approach anymore. Use await instead

exports.handler = async (event) => {
    try {
        await somePromise1 
        await somePromise2
        await somePromise3 
        console.log("Success", resp);
        response = {
            statusCode: 200,
            body: JSON.stringify('Ok!'),
        };
        return response;
    } catch (err) {
        console.log("Error", err);
        response = {
            statusCode: 500,
            body: JSON.stringify(err),
        };
        return response;
    }
};

where somePromise1, somePromise2 and somePromise3 are your promisified callbacks.

More on async/await here.

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

3 Comments

But Promise.all launches all the Promises in parallel (I believe), while async.waterfall launches each Promise after the other, passing the result to the next. But I agree async/await is the way to go now, compared to a library like async.js (which, despite the name, is completely different!)
I initially had this post as await promise1 await promise2 and await promise3 because I thought waterfall was running them sequentially. Then I was like: "Nah, this must be in parallel", so I changed to Promise.all. Then you dropped this comment and I checked the docs and you're right, it's sequential, so I am changing it back to three awaits instead. Thanks for your input!
Thank you very much, sir! :)
3

Try to remove the async from the handler and use callback instead of return:

var async = require('async');

exports.handler = (event, context, callback) => {
    async.waterfall([
        function(callback) {
            console.log("ONE");
            callback(null, 1);
        },
        function(resp, callback) {
            console.log("TWO : ", resp);
            callback(null, 2);
        },
        function(resp, callback){
            console.log("THREE : ", resp);
            callback(null, "Done");
        }
    ],
    function(err, resp) {
        let response = {};
        if (err) {
            console.log("Error",err);
            callback(null, {
                statusCode: 500,
                body: 'Error!',
            });
        } else {
            console.log("Success",resp);
            callback(null, {
                statusCode: 200,
                body: 'Ok!',
            });
        }
    });
};

Comments

1

to return the response use this : return { statusCode: 200, body: JSON.stringify(response) }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.