How to deploy phantomjs node app on AWS Lambda?

I combined a small Lambda function to crawl the site using SpookyJS, CasperJS and PhantomJS binding for a silent view. The task is quite simple, and at some point a few months ago she worked in Lambda. Recently, I had to change something, and again I wanted to work on the project, but I got to work and I could not start Lambda without errors in any capacity. My question is how can I run phantomjs in Lambda ?

Example code that I run:

spooky.start('http://en.wikipedia.org/wiki/Spooky_the_Tuff_Little_Ghost');
spooky.then(function () {
    this.emit('hello', 'Hello, from ' + this.evaluate(function () {
        return document.title;
    }));
});
spooky.run();

The error I get in Lambda:

{ [Error: Child terminated with non-zero exit code 1] details: { code: 1, signal: null } }

I followed various procedures so that everything could work on Lambda. The following is a long list of things I was trying to diagnose:

  • node index.js ,
  • package.json js EC2 Amazon Linux , npm,
  • npm install ec2 node index.js,
  • AWS, cli

.json:

{
  "name": "lambda-spooky-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "casperjs": "^1.1.3",
    "phantomjs-prebuilt": "^2.1.10",
    "spooky": "^0.2.5"
  }
}

( AWS EC2, ​​Lambda:

  • phantom
  • casperjs phantomjs process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/.bin'; console.log( 'PATH: ' + process.env.PATH );
  • spawn child_process .spawn() :

    { '0': 'casperjs',
      '1': 
       [ '/var/task/node_modules/spooky/lib/bootstrap.js',
         '--transport=http',
         '--command=casperjs',
         '--port=8081',
         '--spooky_lib=/var/task/node_modules/spooky/lib/../',
         '--spawnOptions=[object Object]' ],
      '2': {} }
    
  • .exec('casperjs') .exec('phantomjs --version') , , EC2, Lambda. :

    `require('child_process').exec('casperjs', (error, stdout, stderr) => {
    if (error) { console.error('error: ' + error); }
       console.log('out: ' + stdout);
       console.log('err: ' + stderr);
    });
    

:

err: Error: Command failed: /bin/sh -c casperjs
module.js:327
    throw err;
    ^

Error: Cannot find module '/var/task/node_modules/lib/phantomjs'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/var/task/node_modules/.bin/phantomjs:16:15)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)

2016-08-07T15:36:37.349Z    b9a1b509-5cb4-11e6-ae82-256a0a2817b9    sout: 
2016-08-07T15:36:37.349Z    b9a1b509-5cb4-11e6-ae82-256a0a2817b9    serr: module.js:327
    throw err;
    ^

Error: Cannot find module '/var/task/node_modules/lib/phantomjs'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/var/task/node_modules/.bin/phantomjs:16:15)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
+4
1

, , node_modules/.bin , ec2-, /bin . , . :

[ec2-user@ip-172-31-32-87 .bin]$ ls -lrt
total 0
lrwxrwxrwx 1 ec2-user ec2-user 35 Aug  7 00:52 phantomjs -> ../phantomjs-prebuilt/bin/phantomjs
lrwxrwxrwx 1 ec2-user ec2-user 24 Aug  7 00:52 casperjs -> ../casperjs/bin/casperjs

, - Lambda:

process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] 
        + ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/phantomjs-prebuilt/bin'
        + ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/casperjs/bin';

phantom, casper spooky Lambda.

+3

Source: https://habr.com/ru/post/1650465/


All Articles