Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- if (process.argv.length < 3) {
- console.error('get-latest-ami-id.js <role>');
- console.error('Simply specify a Role value as the only argument.');
- console.error('Output is just the AMI ID.');
- console.error('If none match, the exit code will be 2.');
- process.exit(1);
- }
- var aws = require('aws-sdk');
- aws.config.update({region: process.env.AWS_REGION || 'us-east-1'});
- var ec2 = new aws.EC2();
- var params = {
- Filters: [
- { Name: 'tag:Role', Values: process.argv.slice(2) },
- { Name: 'state', Values: ['available'] },
- ],
- };
- ec2.describeImages(params, function(err, data) {
- if (err) {
- console.error('Error listing EC2 AMIs:');
- console.error(err.message);
- return console.error();
- }
- data.Images.map(function(image) {
- return {
- date: new Date(image.CreationDate),
- id: image.ImageId,
- };
- }).sort(function(a, b) {
- return b.date - a.date;
- }).slice(0, 1).forEach(function(image) {
- console.log(image.id);
- process.exit();
- });
- // no instances, fail
- process.exit(2);
- });
Add Comment
Please, Sign In to add comment