0

I am working through an AWS guide for Cognito.

The Lambda function calls context.done() with two parameters, null and event.

I am trying to understand what the null parameter is and what context.done is doing with the event being passed in to it.

exports.handler = (event, context) => {
  if (event.request.session.length === 1 && 
    event.request.session[0].challengeName === 'SRP_A') {
    event.response.issueTokens = false;
    event.response.failAuthentication = false;
    event.response.challengeName = 'CUSTOM_CHALLENGE';
  } else if (
    event.request.session.length === 2 &&
    event.request.session[1].challengeName === 'CUSTOM_CHALLENGE' &&
    event.request.session[1].challengeResult === true
  ) {
    event.response.issueTokens = true;
    event.response.failAuthentication = false;
  } else {
    event.response.issueTokens = false;
    event.response.failAuthentication = true;
  }
  context.done(null, event);
};

I have console logged the context object, but I still don't understand what it's doing.

2022-05-04T22:16:59.591Z    4c2609e6-69d7-4bd9-a25c-8527b7c6653a    INFO    {
  callbackWaitsForEmptyEventLoop: [Getter/Setter],
  succeed: [Function (anonymous)],
  fail: [Function (anonymous)],
  done: [Function (anonymous)],
  functionVersion: '$LATEST',
  functionName: 'cognito-define-auth-challenge',
  memoryLimitInMB: '128',
  logGroupName: '/aws/lambda/cognito-define-auth-challenge',
  logStreamName: '2022/05/04/[$LATEST]280e88c6f58545f8bc6e5d177c13cc5b',
  clientContext: undefined,
  identity: undefined,
  invokedFunctionArn: 'arn:aws:lambda:us-east-1:268280247265:function:cognito-define-auth-challenge',
  awsRequestId: '4c2609e6-69d7-4bd9-a25c-8527b7c6653a',
  getRemainingTimeInMillis: [Function: getRemainingTimeInMillis]
}

  • https://stackoverflow.com/questions/54846513/lambda-trigger-callback-vs-context-done – CollinD May 04 '22 at 22:49
  • @Hotwer I read though that thread but still don't understand what context.done is doing in layman's terms [Function (anonymous)] I understand that context is the execution context of the Lambda function. But in my mind, when the function is ran, the show is over. –  May 04 '22 at 23:02
  • @newGuyThanksForBeingKind he doens't explain, he points out that they're deprecated and why. In the link he refers, there's a web archive with the old removed documentation from the AWS. In the documentation you can read details, but simply put: "context.done() Causes the Lambda function execution to terminate". They're deprecated, and only exists for the purpose of backwards compatibility. In other words, you can just dismiss it and you will be fine, they're no longer required. – Hotwer May 05 '22 at 13:03
  • Thank you. There's a lot of (PUBLISHED) AWS examples still using the depreciated (UNPUBLISHED) termination method. Some are using callback termination, and still others just using "return" keyword. It's like seeing a car on the road with two different color fenders and a missing door. Delopitated. –  May 07 '22 at 12:04

0 Answers0