How do I run PhantomJS on AWS Lambda with NodeJS

A generic solution is to use an actual AWS Linux machine to install the npm modules and transfer them to your lambda executable. Here are the steps:

  1. spin up an EC2 instance
  2. ssh into EC2
  3. install Node + npm
  4. install the required npm modules
  5. zip them up
  6. fetch them to your local machine with scp
  7. unzip and copy to the npm_modules folder of your lambda function (don't npm install locally!)
  8. upload your code to Lambda

Here is a tutorial with links to further resources: Compile node module libraries for AWS Lambda

This also works in such cases when PhantomJS is a dependency of another node module, eg. node-webshot and you have less influence on what is being installed.


EDIT: This no longer works. This is an apparent solution.


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

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

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.