Guest User

Untitled

a guest
Oct 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. if (process.argv.length < 3) {
  4. console.error('get-latest-ami-id.js <role>');
  5. console.error('Simply specify a Role value as the only argument.');
  6. console.error('Output is just the AMI ID.');
  7. console.error('If none match, the exit code will be 2.');
  8. process.exit(1);
  9. }
  10.  
  11. var aws = require('aws-sdk');
  12. aws.config.update({region: process.env.AWS_REGION || 'us-east-1'});
  13. var ec2 = new aws.EC2();
  14.  
  15. var params = {
  16. Filters: [
  17. { Name: 'tag:Role', Values: process.argv.slice(2) },
  18. { Name: 'state', Values: ['available'] },
  19. ],
  20. };
  21. ec2.describeImages(params, function(err, data) {
  22. if (err) {
  23. console.error('Error listing EC2 AMIs:');
  24. console.error(err.message);
  25. return console.error();
  26. }
  27.  
  28. data.Images.map(function(image) {
  29. return {
  30. date: new Date(image.CreationDate),
  31. id: image.ImageId,
  32. };
  33. }).sort(function(a, b) {
  34. return b.date - a.date;
  35. }).slice(0, 1).forEach(function(image) {
  36. console.log(image.id);
  37. process.exit();
  38. });
  39.  
  40. // no instances, fail
  41. process.exit(2);
  42. });
Add Comment
Please, Sign In to add comment