Skip to content

Instantly share code, notes, and snippets.

@jeremypruitt
Created May 14, 2015 08:16
Show Gist options
  • Save jeremypruitt/ac1e3e0b77427d3802c6 to your computer and use it in GitHub Desktop.
Save jeremypruitt/ac1e3e0b77427d3802c6 to your computer and use it in GitHub Desktop.
AWS Lambda function to identify ec2 instances without an owner tag
console.log('Loading function');
var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
exports.handler = function(event, context) {
console.log("\n\nLoading handler\n\n");
var ec2 = new AWS.EC2();
var with_owner = {};
var without_owner = {};
ec2.describeInstances({}, function(err, data) {
if(err) {
console.error(err.toString());
} else {
for(var r=0,rlen=data.Reservations.length; r<rlen; r++) {
var reservation = data.Reservations[r];
for(var i=0,ilen=reservation.Instances.length; i<ilen; ++i) {
var instance = reservation.Instances[i];
var name = '';
for(var t=0,tlen=instance.Tags.length; t<tlen; ++t) {
if(instance.Tags[t].Key === 'Name') {
name = instance.Tags[t].Value;
}
if(instance.Tags[t].Key === 'owner') {
params = {};
params.Name = name;
params.State = instance.State.Name;
params.Owner = instance.Tags[t].Value;
var ID = instance.InstanceId;
with_owner[ID] = params;
} else {
params = {};
params.Name = name;
params.State = instance.State.Name;
var ID = instance.InstanceId;
without_owner[ID] = params;
}
}
}
}
}
console.log('WITH OWNER:')
console.log(JSON.stringify(with_owner)+'\n\n')
console.log('WITHOUT OWNER:')
console.log(JSON.stringify(without_owner)+'\n\n')
context.done(null, 'Function Finished!');
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment