Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context vs Callback in AWS Lambda

I am loving using lambda functions in AWS. It ideally reduced my time in maintaining the servers anymore. My question is when using lambda there is context object and the callback function to terminate the function. Is there any use case of using callback over context.

Could anyone tell me the behavior of context.succeed() to callback(error,message)

var startedAt = new Date();

var interval = setInterval(function () {
    console.log(startedAt, new Date());
}, 1000);

exports.handler = function (event, context, callback) {
    setTimeout(function () {
        console.log('returning');
        // v1:
        return callback(null);
        // v2:
        // return context.succeed();
    }, 5000);
};
like image 935
Naveen Kerati Avatar asked Dec 20 '17 12:12

Naveen Kerati


People also ask

What is context in AWS Lambda?

When Lambda runs your function, it passes a context object to the handler. This object provides methods and properties that provide information about the invocation, function, and execution environment.

What is event and context in AWS Lambda?

An event is a JSON-formatted document that contains data for a Lambda function to process. The Lambda runtime converts the event to an object and passes it to your function code. It is usually of the Python dict type.

What is callback in AWS?

Each service object method that creates an AWS.Request object can accept an anonymous callback function as the last parameter. The signature of this callback function is: function(error, data) { // callback handling code } This callback function executes when either a successful response or error data returns.

What is context succeed?

context. succeed is the older way of doing things, and is supported under the 0.10. 42 runtime (where the callback parameter specifically isn't). If you're running on the newer runtimes (4.3 and 6.10), it's included for backwards compatibility, but the "proper" way is now to use the callback functionality.


1 Answers

context.succeed is the older way of doing things, and is supported under the 0.10.42 runtime (where the callback parameter specifically isn't). If you're running on the newer runtimes (4.3 and 6.10), it's included for backwards compatibility, but the "proper" way is now to use the callback functionality.

like image 134
James Thorpe Avatar answered Sep 19 '22 01:09

James Thorpe