7

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.

3 Answers 3

18

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.

Sign up to request clarification or add additional context in comments.

Comments

12

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>";

Comments

0
require_once 'aws/aws-autoloader.php';
use Aws\Lambda\LambdaClient;
$client = LambdaClient::factory([
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => [
        'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
        'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
     ]
]);

$result = $client->invoke([
    // The name your created Lamda function
    'FunctionName' => 'calculation',
]);

var_dump((string)$result->get('Payload'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.