Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get aws lambda function response in PHP client

I am trying to call a simple Aws Lambda function using PHP as Instructed in the documentation, But I am not getting the desired response.

PHP Lambda client

require './aws/aws-autoloader.php';
use Aws\Lambda\LambdaClient;

$client = LambdaClient::factory(array(
            'version' => "latest",
            'credentials' => array(
                'key' => '*******',
                'secret' => '*******'
            ),
            'region' => '*******'
        ));

$response = $client->invoke([
    'FunctionName' => 'myLambda', // REQUIRED
    'InvocationType' => 'RequestResponse',
    'Payload' => '{"key":"value"}',
        ]);

echo "<pre>";
print_r($response);
print_r($response->data);

?>

Node.js Lambda function This has nothing but this simple code that returns "success" on successful execution of the Lambda function. Its working find in the Amazon Lambda console.

exports.handler = function(event, context){

    context.succeed("success");
};

Response from Amazon I am getting a Private data object, that I cannot access. And according to the documentation, Payload is supposed to be the response from the function. But, I am getting an Object, which again I cannot access, because the parent object data is private.

Aws\Result Object
(
    [data:Aws\Result:private] => Array
        (
            [Payload] => GuzzleHttp\Psr7\Stream Object
                (
                    [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #6
                    [size:GuzzleHttp\Psr7\Stream:private] => 
                    [seekable:GuzzleHttp\Psr7\Stream:private] => 1
                    [readable:GuzzleHttp\Psr7\Stream:private] => 1
                    [writable:GuzzleHttp\Psr7\Stream:private] => 1
                    [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
                    [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                        (
                        )

                )

            [StatusCode] => 200
            [FunctionError] => 
            [LogResult] => 
            [@metadata] => Array
                (
                    [statusCode] => 200
                    [effectiveUri] => https://lambda.*********.amazonaws.com/2015-03-31/functions/myLambda/invocations
                    [headers] => Array
                        (
                            [content-type] => application/json
                            [date] => Wed, 06 Apr 2016 12:33:05 GMT
                            [x-amzn-remapped-content-length] => 0
                            [x-amzn-requestid] => ******-*****-*****-****-*******************
                            [content-length] => 9
                            [connection] => keep-alive
                        )

                    [transferStats] => Array
                        (
                            [http] => Array
                                (
                                    [0] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

So, how do I access the Response from the Lambda function? What might be the issue here?

UPDATE

I am able to access the Payload by using print_r($response['Payload']); But, Still its useless because the Payload is not coming as expected.

like image 940
Parthapratim Neog Avatar asked Apr 06 '16 12:04

Parthapratim Neog


People also ask

Can I use AWS Lambda with PHP?

Previously, we saw how to use AWS Lambda as web hosting for complete web applications. But we can also run event-driven PHP functions on AWS Lambda. Writing functions is very useful to process events and data from other AWS services.

What is the most likely issue with the Lambda functions timeout?

There are many reasons why a function might time out, but the most likely is that it was waiting on an IO operation to complete. Maybe it was waiting on another service (such as DynamoDB or Stripe) to respond. Within a Lambda invocation, the function might perform multiple IO operations.

Can I call Lambda directly without API gateway?

If you set up the API without using the API Gateway console, such as when importing an API from an OpenAPI file , you must explicitly create, if necessary, and set up an invocation role and policy for API Gateway to invoke the Lambda functions.


2 Answers

Oh! well, I found the answer. You need to call the __toString() method of the GuzzleHttp\Psr7\Stream Object that is inside the Payload.

So, doing a print_r($response['Payload']->__toString()); prints "Success" which is the desired response of the Lambda function, and the one that I was looking for.

Hope this helps someone in the future.

like image 60
Parthapratim Neog Avatar answered Nov 09 '22 23:11

Parthapratim Neog


Another way is to call, getContents() of the stream object as following:

$result = $client->invoke(array(
          // FunctionName is required
          'FunctionName' => 'myService-beta-hello',
          'InvocationType' => 'RequestResponse',
            'LogType' => 'Tail',
            'Payload' => '{"key1":"value1", "key2":"value2","key3":"value3"}',
            //'Qualifier' => 'string',
                ));
print "<pre>";
print_r($result);
print_r($result['Payload']->getContents());
print "</pre>";
like image 35
Mostafa Shaikh Avatar answered Nov 10 '22 01:11

Mostafa Shaikh