在互联网上没有找到工作的答案其他地方后,我提出这个问一答,自己教程如何在AWS的λ运行PhantomJS用的NodeJS
我怎样才能得到一个简单的PhantomJS
过程从NodeJS
脚本AWS Lambda
运行?我的代码在我的本地机器上工作正常,但遇到了试图在Lambda上运行它的不同问题。
在互联网上没有找到工作的答案其他地方后,我提出这个问一答,自己教程如何在AWS的λ运行PhantomJS用的NodeJS
我怎样才能得到一个简单的PhantomJS
过程从NodeJS
脚本AWS Lambda
运行?我的代码在我的本地机器上工作正常,但遇到了试图在Lambda上运行它的不同问题。
这是一个简单的PhantomJS
进程的完整代码示例,该进程作为NodeJS
child_process
启动。 It is also available on github。
index.js
var childProcess = require('child_process');
var path = require('path');
exports.handler = function(event, context) {
// 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'),
'my arg'
];
// 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
var unusedArg = args[1];
// Send some info node's childProcess' stdout
system.stdout.write('hello from phantom!')
phantom.exit();
要获得PhantomJS二进制与亚马逊的Linux机器的工作,去了PhantomJS Bitbucket Page并下载phantomjs-1.9.8-linux-x86_64.tar.bz2
。
一个通用的解决方案是使用实际的AWS Linux机器安装npm模块并将它们传输到您的lambda可执行文件。以下是具体步骤:
scp
这里是一个环节,进一步资源教程: Compile node module libraries for AWS Lambda
这也适用于这种情况下,当PhantomJS是另一个节点模块,例如依赖。 node-webshot,并且您对安装的内容影响较小。
谢谢。比公认的答案更有帮助。 – geotheory
梦幻般的答案@泰勒。 –
@Tyler在同样的我执行我的代码。一切都很顺利,只是它展示了我在完成前退出的过程。我可以知道是什么造成这个问题? – Vikas