【发布时间】:2016-04-11 06:02:56
【问题描述】:
在互联网上其他任何地方都没有找到有效的答案后,我提交了这个自问自答教程
如何从AWS Lambda 上的NodeJS 脚本运行一个简单的PhantomJS 进程?我的代码在本地机器上运行良好,但尝试在 Lambda 上运行时遇到了不同的问题。
【问题讨论】:
标签: node.js amazon-web-services phantomjs aws-lambda
在互联网上其他任何地方都没有找到有效的答案后,我提交了这个自问自答教程
如何从AWS Lambda 上的NodeJS 脚本运行一个简单的PhantomJS 进程?我的代码在本地机器上运行良好,但尝试在 Lambda 上运行时遇到了不同的问题。
【问题讨论】:
标签: node.js amazon-web-services phantomjs aws-lambda
编辑:这不再有效。 This is an apparent solution.
这是一个简单的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);
});
}
幻影脚本.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();
要获取适用于亚马逊 Linux 机器的 PhantomJS 二进制文件,请访问 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 并且您对正在安装的内容的影响较小。
【讨论】: