AWS Lambda上的PhantomJS总是超时

我尝试在AWS Lambda上创build一个从PhantomJS创buildPDF文件的任务,然后再上传到AWS S3。

现在,我尝试在Lambda上运行它,但总是超时。

我的Lambda有128MB的内存。 运行时是node.js 4.4.3。

这是我从Lambda获得的错误

"errorMessage": "2017-03-01T08:05:56.255Z dfd4cfe8-fe55-11e6-bf24-e7edf412e037 Task timed out after 10.00 seconds" 

这些也是日志输出

 REPORT RequestId: dfd4cfe8-fe55-11e6-bf24-e7edf412e037 Duration: 10000.08 ms Billed Duration: 10000 ms Memory Size: 128 MB Max Memory Used: 29 MB 2017-03-01T08:05:56.255Z dfd4cfe8-fe55-11e6-bf24-e7edf412e037 Task timed out after 10.00 seconds 

这是我的代码。

Index.js

 var childProcess = require('child_process'); var path = require('path'); exports.handler = function(event, context, callback) { // Set the path as described here: https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/ process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT']; // Set the path to the phantomjs binary var phantomPath = path.join(__dirname, 'phantomjs_linux-x86_64'); // Arguments for the phantom script var processArgs = [ path.join(__dirname, 'phantom-script.js'), event.url ]; // Launc the child process childProcess.execFile(phantomPath, processArgs, function(error, stdout, stderr) { if (error) { context.fail(error); return; } if (stderr) { context.fail(error); return; } context.succeed(stdout); }); } 

幻像的script.js

  var system = require('system'); var args = system.args; // Example of how to get arguments passed from node script // args[0] would be this file's name: phantom-script.js const url = "https://google.com"; system.stdout.write('hello from phantom!'); console.log("task start, target url = " + url); console.time("execute time"); phantom.create().then(function(ph) { console.time("create Page"); ph.createPage().then(function(page) { console.timeEnd("create Page"); console.time("open website"); page.open(url).then(function(status) { console.timeEnd("open website"); console.time("render as pdf"); page.render('google.pdf').then(function() { console.timeEnd("render as pdf"); console.log('Page Rendered'); ph.exit(); // send to s3 console.timeEnd("execute time"); }); }); }); }); // Send some info node's childProcess' stdout system.stdout.write('hello from phantom!') phantom.exit(); 

我尝试按照这个答案做我的工作,但它不工作。

我没有从我的phantom-script.js得到任何日志,因为它不是触发器,但我的任务总是时间。

在我花了很多时间之后。 我发现包名称Phantomjs-Prebuilt ,你可以通过npm来安装它。 你必须在亚马逊linux实例或者具有节点版本4.x(使用节点版本4.3的lambda)的docker amazon linux上执行npm install 。 否则,它不会在lambda上工作。

然后,我更新了我的代码。

Index.js

 var phantomjs = require('phantomjs-prebuilt') exports.handler = function(event, context, callback) { var sourceUrl = "https://example.com" var program = phantomjs.exec('phantom-script.js', sourceUrl) program.stdout.pipe(process.stdout) program.stderr.pipe(process.stderr) program.on('exit', code => { // do something here after you phantomjs finish. return }) } 

幻像的script.js

 var system = require('system') var args = system.args // Example of how to get arguments passed from node script // args[0] would be this file's name: phantom-script.js var url = args[1] // received sourceUrl value // Send some info node's childProcess' stdout system.stdout.write('phantomJS running\r\n') var webpage = require('webpage').create() webpage.paperSize = { format: 'A4', orientation: 'landscape' } webpage.open(url, function(status) { system.stdout.write('start open page\r\n') webpage.render('/tmp/web.pdf', { format: 'pdf', quality: '100' }) system.stdout.write('finish render page\r\n') phantom.exit() }) 

在lambda上,你可以写一个文件的地方是/tmp文件夹,为什么我在那里保存了文件。

我通过lambda192mb的RAM运行这个。 这工作真的很好。 我可以创build一个包含500个图像的网页截图。 最重要的是确保你的lambda能够连接互联网。

仅供参考,我意识到,当phantom-script.js(我写的幻影脚本文件)有一个错误,你的lambda会冻结,直到超时。 这就是为什么我总是得到这个响应从lambda Task timed out after 10.00 seconds