Last active
August 29, 2015 14:06
-
-
Save yhahn/c3ae8a603b376e5c0bf6 to your computer and use it in GitHub Desktop.
Get latest trusty AMIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require('https'); | |
var body = ''; | |
https.get('https://cloud-images.ubuntu.com/locator/ec2/releasesTable', function(res) { | |
res.on('data', function(chunk) { body += chunk; }); | |
res.on('end', finish); | |
}); | |
function finish() { | |
// Bad JSON -- has trailing comma. | |
body = body.replace(/,(\s*)]/, '$1]'); | |
var rows = JSON.parse(body).aaData; | |
rows = rows.filter(function(r) { | |
return r[1] === 'trusty' && | |
r[2] === '14.04 LTS' && | |
r[3] === 'amd64' && | |
(r[4] === 'instance-store' || r[4] === 'hvm:instance-store'); | |
}); | |
rows = rows.reduce(function(memo, r) { | |
var region = r[0]; | |
var type = r[4]; | |
var ami = r[6].match(/ami-[0-f]{8}/)[0]; | |
memo[region] = memo[region] || {}; | |
if (type === 'instance-store') { | |
memo[region].regular = ami; | |
} else if (type === 'hvm:instance-store') { | |
memo[region].hvm = ami; | |
} | |
return memo; | |
}, {}); | |
var sorted = {}; | |
var regions = Object.keys(rows); | |
regions.sort(); | |
regions.forEach(function(r) { sorted[r] = rows[r]; }); | |
console.log(JSON.stringify(sorted, null, 2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment