3
votes

I have Amazon Connect using an AWS Lambda Function to retrieve data from a PHP server. I've read these docs on Using AWS Lambda Functions with Amazon Connect

I have Lambda set up like this:

exports.handler = (event, context, callback) => {
    https.get(url, (httpsResult) => {
        httpsResult.setEncoding('utf8');
        httpsResult.on('data', function gotResult (body) {
            console.log("PHP RETURNED= "+body);
            callback(null, body);
        });
    });
};

PHP returns to lambda a test JSON object:

$testArray=array("test"=>"test string value");
echo json_encode($testArray); exit;

Lambda Cloudwatch logs show PHP RETURNED= {"test": "test string value"}

But when body is returned to AWS Connect, it only logs this:

{
    "Results": "The Lambda Function Returned An Error.",
    "ContactFlowModuleType": "InvokeExternalResource",
    ...
}

I've tried several ways in Lambda to build the JSON object response to Connect. When incorrect, the only error logged is as above,

The lambda function returned an error

but not what the error is. But there are no errors in lambda, only in connect.

The only way I seem to pass data successfully is when I ignore the PHP data and set Lambda to callback(null, {"test": "test string value"});

So what is going wrong with my passing of the JSON object from PHP through Lambda?

1

1 Answers

0
votes

Turns out, you are not supposed to pass it a JSON object because although the docs don't mention it, the callback function must be doing the JSON encoding itself. Usually you would see evidence of this where the log would show it being double escaped, but for whatever reason that didn't happen here.

I was using Node.js in Lambda so the callback needs to receive a regular Javascript Object.

A closer look at the examples helped reveal the difference:

var resultMap = { Name:'CustomerName', Address:'1234 Main Road', CallerType:'Patient' }

callback(null, resultMap);

The output returned from the function must be a flat object of key/value pairs, with values that include only alphanumeric, dash, and underscore characters. Nested and complex objects are not supported. The size of the returned data must be less than 32 Kb of UTF-8 data.

The following example shows the JSON output from these Lambda functions:

{ "Name": "CustomerName", "Address": "1234 Main Road", "CallerType": "Patient" }