Marnus Steyn
Marnus Steyn

Reputation: 1093

Dotnet Core AWS Lambda For Cognito Post Confirmation Trigger Completion

I use Cognito User Pools and for a Post Sign Up Confirmation Trigger I am writing a dotnet core lambda to do some followup work. I stared by simple logging out what I get in and do some work from there. Thing is I realised my logging out works but then when the Function should "finish" it fails to do so. It produces the following error - Code: InvalidLambdaResponseException, Message: Unrecognizable lambda output. Here is my FunctionHandler:

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
...
public async Task FunctionHandler(JObject input, ILambdaContext context)
{
    context.Logger.LogLine($"INPUT:{input}");
    //Do some work      
}

I can only find a NodeJS example for Post Confirmation Lambda Trigger:

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

    if (event.request.userAttributes.email) {
            sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function(status) {

            // Return to Amazon Cognito
            callback(null, event);
        });
    } else {
        // Nothing to do, the user's email ID is unknown
        callback(null, event);
    }
};

In this example, it's simple - You simple invoke the callback, but there is no such callback for the dotnet core handlers.

I have also tried changing the function to simply return a basic string like: return "OK";, but it also does not like that and gives me the same error.

How am I suppose to close of this process for the Lambda?

Upvotes: 0

Views: 669

Answers (2)

Niraj Trivedi
Niraj Trivedi

Reputation: 2880

When I was using JObject it was throwing the below error

PostConfirmation failed with error Error converting the Lambda event JSON payload to type Newtonsoft.Json.Linq.JObject: The JSON value could not be converted to Newtonsoft.Json.Linq.JToken. Path: $.version | LineNumber: 0 | BytePositionInLine: 14..

Previously AWS was using Amazon.Lambda.Serialization.Json.JsonSerializer which uses Newtonsoft.Json new version uses Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer which implements the recent System.Text.Json so JObject is no longer appropriate and you can replace it with JsonElement

public async Task<JsonElement> FunctionHandler(JsonElement input, ILambdaContext context)
{
    var request = input.GetProperty("request");
    var userAttributes = request.GetProperty("userAttributes");
    string email = userAttributes.GetProperty("email").GetString();

    return input;
}

Upvotes: 1

Mete
Mete

Reputation: 31

You could use;

await Task.CompletedTask;

check out the examples below: https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html

For Cognito Post Confirmation Event;

public JObject FunctionHandler(JObject input, ILambdaContext context)
    {
        context.Logger.LogLine($"input data: {JsonConvert.SerializeObject(input)}");
        return input;
    }

Upvotes: 1

Related Questions