如何使用NodeJS在AWS Lambda上运行PhantomJS [英] How do I run PhantomJS on AWS Lambda with NodeJS

查看:164
本文介绍了如何使用NodeJS在AWS Lambda上运行PhantomJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在互联网上其他任何地方都找不到有效的答案之后,我正在提交此问与答"自己的教程

如何从AWS Lambda上的NodeJS脚本运行一个简单的PhantomJS进程?我的代码在本地计算机上运行良好,但是尝试在Lambda上运行时遇到了其他问题.

How can I get a simple PhantomJS process running from a NodeJS script on AWS Lambda? My code works fine on my local machine, but I run into different problems trying to run it on Lambda.

推荐答案

这不再起作用. 这是显而易见的解决方案.

这是一个简单的PhantomJS过程的完整代码示例,该过程以NodeJS child_process形式启动. 在github上也可以使用.

Here is a complete code sample of a simple PhantomJS process, which is launched as a 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


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();


要获取适用于Amazon Linux计算机的PhantomJS二进制文件,请转到 PhantomJS Bitbucket页面并下载phantomjs-1.9.8-linux-x86_64.tar.bz2.


To get a PhantomJS binary that works with Amazon's Linux machine's, go to the PhantomJS Bitbucket Page and download phantomjs-1.9.8-linux-x86_64.tar.bz2.

这篇关于如何使用NodeJS在AWS Lambda上运行PhantomJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆