RootedGlobal/node-casperjs-aws-lambda

Working local and on Lambda, but code breaks inside exports.handler

tdave00 opened this issue ยท 10 comments

I need to invoke the function from the API Gateway - to send data to/from the Lambda function, but when I put my code inside exports.handler it breaks. I do this with many other scripts.

Here's a link to the working code and the code that breaks inside exports.handler: Stack Overflow Question

Can you give a little direction on how to accomplish this? Thanks.

I've figured out how to return context.succeed and context.fail via a callback, but how can I pass the data in event to my script? Can that be set in runCasper, or do I need to set it in index.js and retrieve it from my script? If I need to retrieve it from my script, how do I do that?

From my modified index.js

// Entry Point
exports.handler = function(event, context) {
	//testing if event variables are /*set
	//works: outputs variables set in Lambda
	if (event) {console.log(JSON.stringify(event));}
    // Execute the casperJS call and exit
    runCasper('get-id-follow-working.js', /*context.done*/ function(err, data) {
		if (err) {context.fail(err);}
		else if (data) {
			var len = data.length; 
			console.log('Length: '+ len);
			len = len -1;
			for (k in data) {console.log(data[k]);}
                        //last one contains my data
			context.succeed(data[len]);
		}
	});
};

Hi, You can pass your data to scrapper script via casper args:

make some changes in runCasper function in index.js file like this:

var childArgs = [
        path.join(__dirname, scriptName),
       '--email=hello@hello.com',
        '--password=12345'
    ];

and in your casper-script you can get the parameters data like this below:

var utils = require('utils');

utils.dump(casper.cli.get('email'));
utils.dump(casper.cli.get('password'));
// OR
// utils.dump(casper.cli.raw.get('email'));
// utils.dump(casper.cli.raw.get('password'));

And for running the app correctly use these commands instead of npm run ...:

$ ./node_modules/.bin/node-lambda setup
$ ./node_modules/.bin/node-lambda run
$ ./node_modules/.bin/node-lambda deploy

Hope this helps..

Thanks for your feedback.. So, Should we close the issue now?

utils.dump would stop my script - only returned my username/password and would never run my casper script. What ended up working for me was not requiring/using utils at all:

var login = {email: casper.cli.get('email'), pw: casper.cli.get('pw')}

For anyone else that may need this, I also made these changes to index.js to make it portable.
Inside runCasper()

function runCasper(scriptName, /*event object*/ creds, callback) {
    .....
    var childArgs = [
        path.join(__dirname, scriptName)
    ];
    if (creds) {
        for (var k in creds) {
            if (creds.hasOwnProperty(k)) {childArgs.push('--'+ k + '=' + creds[k]);}
	}
    }
    .....

Inside exports.handler

.....
runCasper('yourscript.js',  event, function(err, data) {
....

@tdave00 Thanks!! pushed these arguments/parameters support on master

Cheers,

I also noticed while retrieving the script arguments/variables in my casper scripts that if I tried like this it would only return the variable(s) never running the casper script:
var login = casper.cli.get('id');

But for some reason if you set the variable first, and then define it it will work as expected:

var login;
login = casper.cli.get('id');

Same behavior with an object/array. You have to first set the variable (at least in my environment), and then define.

var login = {};
login.email = casper.cli.get('email');
login.pw = casper.cli.get('pw');

I don't think it will cause any problems if you either first declare then define OR direct set..

In the example available if you the run the script you'll see it's direct defining and setting args into variable and script is also running fine..

I think problem should be something else..