我如何 运行 PhantomJS on AWS Lambda with NodeJS
How do I run PhantomJS on AWS Lambda with NodeJS
在互联网上其他任何地方都找不到有效答案后,我提交了这个自问自答的教程
如何从 AWS Lambda
上的 NodeJS
脚本获取简单的 PhantomJS
进程 运行ning?我的代码在我的本地机器上运行良好,但我 运行 遇到了不同的问题试图 运行 它在 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);
});
}
phantom-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();
要获得适用于亚马逊 Linux 机器的 PhantomJS 二进制文件,请转到 PhantomJS Bitbucket Page 并下载 phantomjs-1.9.8-linux-x86_64.tar.bz2
。
一个通用的解决方案是使用实际的 AWS Linux 机器来安装 npm 模块并将它们传输到您的 lambda 可执行文件。以下是步骤:
- 启动一个 EC2 实例
- ssh 进入 EC2
- 安装 Node + npm
- 安装所需的 npm 模块
- 将它们压缩
- 使用
scp
将它们提取到您的本地计算机
- 解压并复制到您的 lambda 函数的 npm_modules 文件夹中 (不要在本地安装 npm!)
- 将您的代码上传到 Lambda
这是一个教程,其中包含更多资源的链接:
Compile node module libraries for AWS Lambda
当 PhantomJS 是另一个节点模块的依赖项时,这也适用于这种情况,例如。 node-webshot 并且您对正在安装的内容的影响较小。
在互联网上其他任何地方都找不到有效答案后,我提交了这个自问自答的教程
如何从 AWS Lambda
上的 NodeJS
脚本获取简单的 PhantomJS
进程 运行ning?我的代码在我的本地机器上运行良好,但我 运行 遇到了不同的问题试图 运行 它在 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);
});
}
phantom-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();
要获得适用于亚马逊 Linux 机器的 PhantomJS 二进制文件,请转到 PhantomJS Bitbucket Page 并下载 phantomjs-1.9.8-linux-x86_64.tar.bz2
。
一个通用的解决方案是使用实际的 AWS Linux 机器来安装 npm 模块并将它们传输到您的 lambda 可执行文件。以下是步骤:
- 启动一个 EC2 实例
- ssh 进入 EC2
- 安装 Node + npm
- 安装所需的 npm 模块
- 将它们压缩
- 使用
scp
将它们提取到您的本地计算机
- 解压并复制到您的 lambda 函数的 npm_modules 文件夹中 (不要在本地安装 npm!)
- 将您的代码上传到 Lambda
这是一个教程,其中包含更多资源的链接: Compile node module libraries for AWS Lambda
当 PhantomJS 是另一个节点模块的依赖项时,这也适用于这种情况,例如。 node-webshot 并且您对正在安装的内容的影响较小。