Skip to content

Instantly share code, notes, and snippets.

@yhahn
Last active August 29, 2015 14:06
Show Gist options
  • Save yhahn/c3ae8a603b376e5c0bf6 to your computer and use it in GitHub Desktop.
Save yhahn/c3ae8a603b376e5c0bf6 to your computer and use it in GitHub Desktop.
Get latest trusty AMIs
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