配置AWS Lambda以使用PHP + MySQL?

时间:2017-02-27 19:23:03

标签: php mysql amazon-web-services lambda aws-lambda

我正在尝试使用AWS Lambda函数运行PHP代码和MySQL

为此,我关注this tutorial并设法让PHP正常工作,但当我尝试添加MySQL代码时,我收到了错误消息。这个错误并不多见,它是这样的:

"errorMessage": "RequestId: 14a46e73-xxx-xxx-b810-11db18dxxx0 Process exited before completing request"

日志就像

START RequestId: 14a46e73-xxx-xxx-b810-11db18dxxx0 Version: $LATEST
2017-02-27T19:08:12.544Z    14a46e73-xxx-xxx-b810-11db18dxxx0   SyntaxError: Unexpected token F
    at Object.parse (native)
    at ChildProcess.<anonymous> (/var/task/php.js:29:34)
    at emitTwo (events.js:87:13)
    at ChildProcess.emit (events.js:172:7)
    at maybeClose (internal/child_process.js:821:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
END RequestId: 14a46e73-xxx-xxx-b810-11db18dxxx0
REPORT RequestId: 14a46e73-xxx-xxx-b810-11db18dxxx0 Duration: 293.96 ms Billed Duration: 300 ms     Memory Size: 128 MB Max Memory Used: 12 MB  
RequestId: 14a46e73-xxx-xxx-b810-11db18dxxx0 Process exited before completing request

看起来好像在抱怨Unexpected token F,但是我无法找到可能导致我的代码出现问题的任何内容,这很简单,因为我只是在检查是否可以连接到MySQL

我的PHP代码如下

$servername = "xx";
$username = "xx";
$password = "xx";
$dbname = "xx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    //die("Connection failed: " . $conn->connect_error);
}


//If I remove everything above this line it works

$data = stream_get_contents(STDIN);

$json = json_decode($data, true);

$result = json_encode(array('result' => count($json)));

echo $result;

您可以看到我刚刚在教程中的代码中添加了一个简单的MySQL连接。当我删除该部分时,代码运行正常。

此时,我开始怀疑这是否甚至可能。 任何正确方向的提示都将受到赞赏。

php.js

process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];

const spawn = require('child_process').spawn;

exports.handler = function(event, context) {

    //var php = spawn('php',['helloLambda.php']); //local debug only
    var php = spawn('php-7-bin/bin/php',['hello.php']);
    var output = "";

    //send the input event json as string via STDIN to php process
    php.stdin.write(JSON.stringify(event));

    //close the php stream to unblock php process
    php.stdin.end();

    //dynamically collect php output
    php.stdout.on('data', function(data) {
          output+=data;
    });

    //react to potential errors
    php.stderr.on('data', function(data) {
            console.log("STDERR: "+data);
    });

    //finalize when php process is done.
    php.on('close', function(code) {
            context.succeed(JSON.parse(output));
    });
}

0 个答案:

没有答案