Skip to content

Instantly share code, notes, and snippets.

@marinbek
Created April 15, 2015 06:43
Show Gist options
  • Save marinbek/a223450fff22a68359f0 to your computer and use it in GitHub Desktop.
Save marinbek/a223450fff22a68359f0 to your computer and use it in GitHub Desktop.
AWS ip range parser
var https = require('https'),
regions = {
"ap-northeast-1": "Tokyo, JP",
"ap-southeast-1": "SG",
"ap-southeast-2": "Sydney, AU",
"eu-central-1": "Frankfurt, DE",
"eu-west-1": "IE",
"sa-east-1": "Sao Paulo, BR",
"us-east-1": "VA, US",
"us-west-1": "CA, US",
"us-west-2": "OR, UA",
"GLOBAL": "Global",
"us-gov-west-1": "GovCloud west",
"cn-north-1": "Bejing, CN"
};
var parse_ips = function(ips) {
ips.prefixes.forEach(function(prefix) {
var region = regions[prefix.region];
if (!region) {
console.log('Missing region definition for', prefix.region)
region = prefix.region;
}
console.log(prefix.ip_prefix + ',' + region);
});
};
https.get("https://ip-ranges.amazonaws.com/ip-ranges.json", function(res) {
if (res.statusCode != 200) {
console.error('Invalid response');
return;
}
var payload = '';
res.on('data', function(chunk) {
payload += chunk;
}).on('end', function(e) {
parse_ips(JSON.parse(payload));
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment