Lambda Function - Handler and Callback

When a lambda function is created using NodeJS, a default code is created which the handler. The handler defines the entry point for the function and is executed every time the function is called.



    exports.handler = (event, context, callback) => {
        //todo
        callback(null, 'success');
    };

The event parameter provides information about the event data. If the lambda is triggered with file operations in S3, then it could contain information about the S3 bucket and the file name (key name) that triggered the event.

The context parameter provides information about the runtime of the running lambda function. For example, it could provide getRemainingTimeInMillis() or the log group name.

The callback parameter is optional and is used to return information. If not used, null is returned to the caller. The callback could provide information in few different ways as shown below.



    callback();     // success but no information returned
    callback(null); // success but no information returned
    callback(null, "success");  // success with "success" string returned
    callback(error);    //  error with error object returned

Here is an example of a lambda function returning dummy values.



    exports.handler = (event, context, callback) => {
    
    //write to logs
    console.log('s3_bucket: ' + process.env.s3_bucket);
    console.log('api_url: ' + process.env.api_url);
    
    var ans = false;
    
    if(ans)
        callback(null, 'failure');
    else
        callback(null);
    };