PhantomJS on AWS Lambda Always Timeout

I am trying to create a task on AWS Lambda that creates a PDF file from PhantomJS and then downloads it to AWS S3 later.

Now I'm trying to run it on Lambda, but it is always Timeout.

My Lambda has 128 MB of ram. Node.js runtime 4.4.3.

This is the error I received from Lambda

"errorMessage": "2017-03-01T08:05:56.255Z dfd4cfe8-fe55-11e6-bf24-e7edf412e037 Task timed out after 10.00 seconds"

This is also the output of the magazine.

REPORT RequestId: dfd4cfe8-fe55-11e6-bf24-e7edf412e037  Duration: 10000.08 ms   Billed Duration: 10000 ms   Memory Size: 128 MB Max Memory Used: 29 MB


2017-03-01T08:05:56.255Z dfd4cfe8-fe55-11e6-bf24-e7edf412e037 Task timed out after 10.00 seconds

This is my code.

index.js

var childProcess = require('child_process');
var path = require('path');

exports.handler = function(event, context, callback) {

     // 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'),
         event.url
    ];

    // 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 name: phantom-script.js

 const url = "https://google.com";

 system.stdout.write('hello from phantom!');

 console.log("task start, target url = " + url);

 console.time("execute time");
 phantom.create().then(function(ph) {
     console.time("create Page");
     ph.createPage().then(function(page) {
         console.timeEnd("create Page");
         console.time("open website");
         page.open(url).then(function(status) {
             console.timeEnd("open website");
             console.time("render as pdf");
             page.render('google.pdf').then(function() {
                 console.timeEnd("render as pdf");
                 console.log('Page Rendered');
                 ph.exit();

                 // send to s3
                 console.timeEnd("execute time");
             });
         });
     });
 });


 // Send some info node childProcess' stdout
 system.stdout.write('hello from phantom!')

 phantom.exit();

I try to do my job after this answer , but it does not work.

I didn’t receive a single magazine from mine phantom-script.js, as if it was not a trigger, but my task is always time.

+4
source share
1 answer

, . Phantomjs-Prebuilt, npm. npm install amazon linux amazon linux node 4.x(lambda node 4.3). .

.

index.js

var phantomjs = require('phantomjs-prebuilt')

exports.handler = function(event, context, callback) {
    var sourceUrl = "https://example.com"
    var program = phantomjs.exec('phantom-script.js', sourceUrl)
    program.stdout.pipe(process.stdout)
    program.stderr.pipe(process.stderr)
    program.on('exit', code => {
        // do something here after you phantomjs finish.
        return
    })
}

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 name: phantom-script.js

var url = args[1] // received sourceUrl value

// Send some info node childProcess' stdout
system.stdout.write('phantomJS running\r\n')

var webpage = require('webpage').create()

webpage.paperSize = {
    format: 'A4',
    orientation: 'landscape'
}

webpage.open(url, function(status) {
    system.stdout.write('start open page\r\n')
    webpage.render('/tmp/web.pdf', {
        format: 'pdf',
        quality: '100'
    })
    system.stdout.write('finish render page\r\n')
    phantom.exit()
})

, , - /tmp, .

192mb RAM. . - 500 . - , .

FYI, , phantom - script.js(, phantom script in.), , . Task timed out after 10.00 seconds.

+2

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


All Articles