简体   繁体   中英

Passing a 'Payload' to an AWS Lambda function with the AWS PHP SDK

I'm trying to use the aws php sdk to invoke a aws-lambda function and get the return value like so:

    $client = LambdaClient::factory([
        'key' => 'mykey',
        'secret' => 'mysecret',
        'region' => 'us-west-2'
    ]);

    $payload = [
        'key1' => 'value1',
        'key2' => 'value2',
        'key3' => 'value3'
    ];

    $result = $client->invoke([
        'FunctionName' => 'testFunction',
        'Payload' => json_encode($payload)
    ]);

For some reason i'm getting an ErrorException in StatusCodeVisitor.php on line 21. "Illegal string offset 'StatusCode'"

When I don't include the

'Payload' => json_encode($payload) 

Then I don't get an error, but I also don't pass any data into my lambda function which defeats the purpose.

Can anyone see anything I could be doing wrong? This seems like a trivial example.

Edit - Adding link to documentation on This function

http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.Lambda.LambdaClient.html#_invoke

I faced the same issue and for some weird reason AWS SDK did not recognize the associate array in PHP. Maybe because at the time of JSON encoding associative array in PHP becomes object and SDK expect it as Array.

Changing $payload as follows will fix this issue.

$payload = array('test1', 'test3', 'test3');

Also remember that if you made changes in $payload you need to access them in the Lambda function as follows:

exports.handler = function(event, context) {
    console.log('value1 =', event[0]);
    console.log('value2 =', event[1]);
    console.log('value3 =', event[2]);
    context.succeed(event);  // Echo back the first key value
    // context.fail('Something went wrong');
};

Update

You can pass information like this:

$payload = array(
                "key1" => array(),
                "key2" => array()
            );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM