Search code examples
aws-lambdaamazon-cognitobref

Amazon AWS Cognito Lambda Triggers Not Working With Simple PHP BREF Function


I recently started experimenting with using the bref (https://bref.sh/docs/) package to use PHP for Lambda functions on Amazon AWS. I'm able to use this package perfectly fine to upload a simple Lambda function, as simple as it can get:

<?php

require __DIR__.'/vendor/autoload.php';

lambda(function ($event) {
    return $event;
});

Then I take this Lambda function and start assigning it to Cognito triggers. It works great for the Pre sign-up trigger, no issues at all hitting it when I create a new account from the admin panel in Cognito.

However when I take this same exact trigger and add it to the Pre-authentication Cognito trigger, then test it with a simple Symfony app I have running locally to act as a simple authentication mechanism using Cognito (developed using this tutorial: https://tech.mybuilder.com/managing-authentication-in-your-symfony-project-with-aws-cognito/), it immediately returns the error:

Error executing "AdminInitiateAuth" on "https://cognito-idp.us-east-2.amazonaws.com"; AWS HTTP error: Client error: `POST https://cognito-idp.us-east-2.amazonaws.com` resulted in a `400 Bad Request` response:
{"__type":"InvalidLambdaResponseException","message":"Unrecognizable lambda output"}
InvalidLambdaResponseException (client): Unrecognizable lambda output - {"__type":"InvalidLambdaResponseException","message":"Unrecognizable lambda output"}

When I disable the above lambda it immediately starts working without error, so the issue is not with my Symfony app, it's somehow with the lambda (even though it's just returning the event that it is given and works for other triggers, as noted above).

Even when I replace this with a simple lambda that is basically the equivalent in Node, it works fine:

exports.handler = async (event) => {
    return event;
};

Does anyone have any idea why the PHP lambda doesn't seem to be working with the Cognito Pre-authentication trigger? I would prefer to do this in PHP if possible.


Solution

  • The problem is in the conversion of $event into JSON. The lambda for these two events expects:

    "response": {}
    

    and bref is returning

    "response": []
    

    Which is invalid response for AWS. So to hack it. The quickest way is to set

    $event['response'] = ['property' => 'name']
    

    to force bref to return:

    "respose": {"property":"name"}