|
| 1 | +## lambdium |
| 2 | +### serverless application repository quickstart |
| 3 | + |
| 4 | +Lambdium allows you to run a Selenium Webdriver script written in Javascript inside of an AWS Lambda function bundled with a special version of Google Chrome (headless chrome). |
| 5 | + |
| 6 | +For example, this script uses Selenium to automate visiting the google.com homepage and print the title to the console: |
| 7 | + |
| 8 | +``` |
| 9 | +console.log('About to visit google.com...'); |
| 10 | +$browser.get('http://www.google.com/ncr'); |
| 11 | +$browser.wait($driver.until.titleIs('Google'), 1000); |
| 12 | +$browser.getTitle().then(function(title) { |
| 13 | + console.log("title is: " + title); |
| 14 | +}); |
| 15 | +console.log('Finished running script!'); |
| 16 | +``` |
| 17 | + |
| 18 | +After running this script inside of of the AWS Lambda function running lambdium, you can look at Cloudwatch logs to read the output of the script: |
| 19 | + |
| 20 | +``` |
| 21 | +23:37:332018-03-12T23:37:33.682Z 567babb5-264e-11e8-ae20-e37b20048a93 About to visit google.com... |
| 22 | +23:37:33 2018-03-12T23:37:33.682Z 567babb5-264e-11e8-ae20-e37b20048a93 Finished running script! |
| 23 | +23:37:36 2018-03-12T23:37:36.860Z 567babb5-264e-11e8-ae20-e37b20048a93 title is: Google |
| 24 | +``` |
| 25 | + |
| 26 | +### running your first selenium script |
| 27 | + |
| 28 | +lambdium requires you to invoke it using a special event trigger: a JSON document with a single property called `Base64Script`, which is a Base64-encoded selenium script written in Javascript. The example which visits google.com looks like this: |
| 29 | + |
| 30 | +``` |
| 31 | +{ |
| 32 | + "Base64Script": "Ly8gU2FtcGxlIHNlbGVuaW11bS13ZWJkcml2ZXIgc2NyaXB0IHRoYXQgdmlzaXRzIGdvb2dsZS5jb20KLy8gVGhpcyB1c2VzIHRoZSBzZWxlbml1bS13ZWJkcml2ZXIgMy40IHBhY2thZ2UuCi8vIERvY3M6IGh0dHBzOi8vc2VsZW5pdW1ocS5naXRodWIuaW8vc2VsZW5pdW0vZG9jcy9hcGkvamF2YXNjcmlwdC9tb2R1bGUvc2VsZW5pdW0td2ViZHJpdmVyL2luZGV4Lmh0bWwKLy8gJGJyb3dzZXIgPSB3ZWJkcml2ZXIgc2Vzc2lvbgovLyAkZHJpdmVyID0gZHJpdmVyIGxpYnJhcmllcwovLyBjb25zb2xlLmxvZyB3aWxsIG91dHB1dCB0byBBV1MgTGFtYmRhIGxvZ3MgKHZpYSBDbG91ZHdhdGNoKQoKY29uc29sZS5sb2coJ0Fib3V0IHRvIHZpc2l0IGdvb2dsZS5jb20uLi4nKTsKJGJyb3dzZXIuZ2V0KCdodHRwOi8vd3d3Lmdvb2dsZS5jb20vbmNyJyk7CiRicm93c2VyLmZpbmRFbGVtZW50KCRkcml2ZXIuQnkubmFtZSgnYnRuSycpKS5jbGljaygpOwokYnJvd3Nlci53YWl0KCRkcml2ZXIudW50aWwudGl0bGVJcygnR29vZ2xlJyksIDEwMDApOwokYnJvd3Nlci5nZXRUaXRsZSgpLnRoZW4oZnVuY3Rpb24odGl0bGUpIHsKICAgIGNvbnNvbGUubG9nKCJ0aXRsZSBpczogIiArIHRpdGxlKTsKfSk7CmNvbnNvbGUubG9nKCdGaW5pc2hlZCBydW5uaW5nIHNjcmlwdCEnKTs=" |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +To encode another script, you can paste the contents of a script in an online converter like [https://www.base64encode.org/](https://www.base64encode.org/) or using the following commands, which are also available as a simple shell script in `scripts/invoke.sh`: |
| 37 | + |
| 38 | +``` |
| 39 | +SELENIUM_SCRIPT=/path/to/your/script |
| 40 | +BASE64_ENCODED=`cat $SELENIUM_SCRIPT | openssl base64` |
| 41 | +PAYLOAD_STRING='{"Base64Script": "'$BASE64_ENCODED'"}' |
| 42 | +echo $PAYLOAD_STRING |
| 43 | +``` |
| 44 | + |
| 45 | +With the payload constructed, you can then invoke the function directly in the AWS Console by defining a test event or using the AWS CLI with the payload written to a file defined in the $PAYLOAD_FILE enviornment variable. Replace the function name with the name of the function deployed by the serverless application repository (it should be `lambdium` by default). |
| 46 | + |
| 47 | +``` |
| 48 | +aws lambda invoke --invocation-type RequestResponse --function-name lambdium --payload file://$PAYLOAD_FILE --log-type Tail $OUTPUT_FILE |
| 49 | +``` |
| 50 | + |
| 51 | +### troubleshooting |
| 52 | + |
| 53 | +Errors and failures will be written to Cloudwatch. The `DEBUG_ENV` environment variable outputs additional information about the filesystem and inputted Base64 script. |
| 54 | + |
| 55 | +The maximum payload size is 4kb and the default timeout is 10 seconds, so this won't work with very large scripts or scripts that run a long time. |
| 56 | + |
| 57 | +More details are available on the official Github page, https://github.com/smithclay/lambdium. |
0 commit comments