Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run PhantomJS on AWS Lambda with NodeJS

Tags:

After not finding a working answer anywhere else on the internet, I am submitting this ask-and-answer-myself tutorial

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.

like image 404
Tyler Avatar asked Jan 06 '16 23:01

Tyler


People also ask

Does AWS Lambda support node JS?

AWS Lambda now supports Node. js 16 as both a managed runtime and a container base image. Developers creating serverless applications in Lambda with Node. js 16 can take advantage of new features such as support for Apple silicon for local development, the timers promises API, and enhanced performance.

What kind of packages can you use with node js for Lambda?

You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and . zip file archives. To create the deployment package for a .


1 Answers

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.

like image 189
Tyler Avatar answered Sep 27 '22 00:09

Tyler