KnowHoper
KnowHoper

Reputation: 4622

Unrecognizable Lambda Output Cognito

I am trying to implement an auto confirm mechanism in AWS, I'm getting an error due to the Lambda response. I can't find the correct return type in the docs.

Lambda:

exports.handler = (event, context, callback) => {
    event.response.autoConfirmUser = true;
    context.succeed(event.response);
};

Exception:

Unrecognizable lambda output (Service: AWSCognitoIdentityProviderService; Status Code: 400; Error Code: InvalidLambdaResponseException; Request ID: 5c7a2436-0515-11e7-b971-41a89adf53ea)

Upvotes: 27

Views: 23896

Answers (5)

DwankyDan
DwankyDan

Reputation: 49

I ran across this error, and the problem was that I simply had not actually deployed the lambda function.

Upvotes: 0

gitmcgee
gitmcgee

Reputation: 177

In 2022, seems like the docs have been updated such that callback(null, event); is needed for successful login. as such this will accept and log to cloudwatch.

exports.handler = (event, context, callback) => {

    // Send post authentication data to Cloudwatch logs
    console.log ("Authentication successful");
    console.log ("Trigger function =", event.triggerSource);
    console.log ("User pool = ", event.userPoolId);
    console.log ("App client ID = ", event.callerContext.clientId);
    console.log ("User ID = ", event.userName);

    // Return to Amazon Cognito
    callback(null, event);
};

Source: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-post-authentication.html

Upvotes: 1

Chris
Chris

Reputation: 111

Ruby lambda people, all cognito wants back is the event object.

def lambda_handler(event:, context:)
    # TODO implement
    return event
end

Upvotes: 11

Is very simple.

  1. Create a Lambda Function with this code : example

    exports.handler = function(event, context) {
    
    /* This Lambda function returns a flag to indicate if a user should be auto-confirmed.
    Perform any necessary validations.Impose a condition that the minimum length of the
    username of 5 is imposed on all user pools. */
    
       if (event.userName.length < 5) {
          var error = new Error('failed!');
          context.done(error, event);
       }
    
    /* Access your resource which contains the list of emails of users who were invited to
    sign up. Compare the list of email IDs from the request to the approved list */
    
       if(event.userPoolId === "yourSpecialUserPool") {
          if (event.request.userAttributes.email in listOfEmailsInvited) {
               event.response.autoConfirmUser = true;
          }
       }
    
       // Return result to Cognito
       context.done(null, event);
    };
    

Note: Role: Lambda basic execution

  1. Create the trigger from cognito console and select the function lambda.

TEST 3. Create the user with the API and DONE.

Upvotes: 4

Chetan Mehta
Chetan Mehta

Reputation: 5671

As shown in the PreSignUp trigger example in Cognito developer guide, you should use context.done(null, event); or context.succeed(event); at the end of your trigger.

Cognito expects the complete event source back in response from your lambda triggers being invoked as part of different Cognito User Pools flows.

Upvotes: 41

Related Questions