10

I'm trying to insert a record into DynamoDB invoking "Pre sign-up" trigger of Cognito User Pool.

Lambda function is pretty simple for testing purposes but there is always an error in client application on AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool.signUp call

Use case 1

Lambda body:

console.log('Received event:', JSON.stringify(event, null, 2));

Result:

InvalidLambdaResponseException: Invalid lambda function output : Invalid JSON

Use case 2

Lambda body:

callback("null", "success");

Result:

InvalidLambdaResponseException: Invalid lambda function output : Invalid JSON

Use case 3

Lambda body:

new AWS.DynamoDB.DocumentClient().put(params, callback);

Result:

InvalidLambdaResponseException: Invalid cognito sign in version. Version should be 1

So, any ideas what might be wrong?

Could the latest error might be related to the beta status of Cognito User Pool?

P.S. I will provide more details if needed. Thanks in advance.

Stanislau
  • 402
  • 1
  • 5
  • 16

3 Answers3

12

You are doing this in node.js and the error indicates you are not returning the service expected event source.

You should call context.done() when your lambda function finishes execution. Also, in any of the trigger sources which Cognito User Pool service generates, you can only edit the "response" part of the the source. For example, "autoConfirmUser" flag in PreSignUp trigger source.

Look at the examples in our developer guide for more details on this.

Chetan Mehta
  • 5,491
  • 1
  • 22
  • 21
  • Yes, exactly. I remember I read somewhere that I need to deal with `context` but after thousand pages of different documentations it's already kinda a mess in the head :) Thanks a lot for your help! – Stanislau May 25 '16 at 15:27
  • @Stanislau - I'm facing the exact problem while trying to insert user data to dynamodb on the post confirmation trigger in user pool. Can you please let me know how you resolve this? – Asanga Dewaguru Jul 06 '16 at 04:03
  • 1
    @Asa - So, as Chetan mentioned in his response, `context.done()` makes the job. In your lambda function you just need to invoke at some point either `context.done(null, event)` or `context.done(err)` (e.g. `exports.handler = (event, context) => {"your code here"; context.done(null, event)}` – Stanislau Jul 06 '16 at 07:55
  • @Stanislau - Thanks a lot. I'll try this out and let you know of the progress. Since I've started working on some other project, it'll take couple of days for me to test this out. – Asanga Dewaguru Jul 06 '16 at 08:01
  • Is `context.done` still the way to go? Or should one be using the 3rd argument to the handler `callback`? I remember reading somewhere that the use of `callback` was a replacement for `context.done` and others but I still see the `context` way used and discussed. – DJSunny Oct 21 '16 at 16:18
  • 2
    Does anybody know how to solve this issue in python lambda? There is no `contex.done()` method, and no `callback` as third param as in `nodejs` lambdas. – Ivan Borshchov Mar 13 '17 at 11:20
  • I just want to point out that calling the callback function with the event works as well. `callback(null, event);` or if you need to return an error `callback(err);` – Michael Leanos Mar 19 '19 at 18:41
  • @ivanBorshchov for this callback("null", "success"); in node is equivalent in python return event – m Piroli Apr 11 '21 at 19:28
  • 1
    fyi: context.done() is now deprecated – astroanu Sep 18 '21 at 16:24
  • 1
    context.done() seems to be deprecated, callback(null, event) is the recommended way, Just in case if someone is using async methods, then you can return Promise.resolve(event) and Promise.reject(error) in a try catch block – Khuram Niaz Jun 19 '22 at 05:21
9

@user3479125 To do the same in python just return an event as is, or with modifications in datasets.

This code is supposed to run between the mobile device and cognito, so it can modify the event and it should return it back, so Sync event will finish successfully.

Some more explanations here

-6

Try returning callback("null", event);

This should solve your problem.

user1268855
  • 9
  • 1
  • 4
  • 7
    `null` should not be in quotes (otherwise it is a string and not null), it should be `callback(null, event)` – MrColes Dec 16 '18 at 00:56