Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Created March 5, 2018 17:19
Show Gist options
  • Save niraj-shah/e4c33d57312d9ea46251fb04dfe22b1c to your computer and use it in GitHub Desktop.
Save niraj-shah/e4c33d57312d9ea46251fb04dfe22b1c to your computer and use it in GitHub Desktop.
AWS Lambda IP Example
// include the libraries we need
var request = require('request');
var cheerio = require('cheerio');
// set some defaults
req = request.defaults({
jar: true, // save cookies to jar
rejectUnauthorized: false,
followAllRedirects: true // allow redirections
});
exports.getIP = function( event, context ) {
// scrape the page
req.get({
url: "http://www.whatsmyipaddress.net/",
headers: {
'User-Agent': 'Super Cool Browser' // optional headers
}
}, function(err, resp, body){
var $ = cheerio.load(body);
// get the data and output to console
var data = {};
data.ip = $('.inner_cntent:nth-child(1) span').text();
data.host = $('.inner_cntent:nth-child(2) span').text();
data.ua = $('.browser span').text();
context.done( null, JSON.stringify( data ) );
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment