-
Notifications
You must be signed in to change notification settings - Fork 17
/
aws_maintenance_lambda.js
40 lines (33 loc) · 1.1 KB
/
aws_maintenance_lambda.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var Promise = require('bluebird');
var notification = require('./notifications/notification');
var simpledb = require('./simpledb');
var ec2 = require('./ec2');
var handler = function* (event, context, callback) {
try {
yield simpledb.createDomain();
var instances = yield ec2.getInstancesUnderMaintenance();
yield Promise.filter(Object.keys(instances), function(instance) {
var eventTimestamp = instances[instance].Events[0].NotBefore;
return simpledb.isInstanceToBeProcessed(instance, eventTimestamp);
}).map(function(instance) {
return notification.sendMessage(instances[instance])
.then(function() {
var eventTimestamp = instances[instance].Events[0].NotBefore;
return simpledb.markInstanceAsProcessed(instance, eventTimestamp);
});
})
callback(null, 'Finished');
} catch(ex) {
callback(ex);
}
};
exports.handler = Promise.coroutine(handler);
//Uncomment below to test locally
// exports.handler(null, null, function(e, s) {
// if(e) {
// console.log('[ERROR] ' + e);
// return;
// }
//
// console.log(s);
// });