-
Notifications
You must be signed in to change notification settings - Fork 4
/
elb-instance-reboot.js
43 lines (38 loc) · 1.37 KB
/
elb-instance-reboot.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
41
42
43
const AWS = require('aws-sdk');
const waterfall = require('async-waterfall');
exports.handler = function(event, context, callback) {
const message = JSON.parse(event.Records[0].Sns.Message);
const elb = (message.Trigger && message.Trigger.Dimensions) ? message.Trigger.Dimensions[0] : null;
if (!elb) return console.log('No elb value found in message', message);
const elbApi = new AWS.ELB();
const ec2Api = new AWS.EC2();
waterfall([
function(next){
const params = {
LoadBalancerNames: [elb.value]
};
elbApi.describeLoadBalancers(params, next);
},
function(data, next){
const params = {
LoadBalancerName: elb.value,
Instances: data.LoadBalancerDescriptions[0].Instances
};
elbApi.describeInstanceHealth(params, next);
},
function(data, next){
const unhealthyNodes = data.InstanceStates
.filter(instance => instance.State === 'OutOfService')
.map(instance => instance.InstanceId);
if (unhealthyNodes.length) {
console.log('Rebooting unhealthy nodes', unhealthyNodes);
ec2Api.rebootInstances({InstanceIds: unhealthyNodes}, next);
} else {
next(null, 'All nodes InService, no reboots necessary');
}
}
], function (err, result) {
console.log('Process complete', result);
callback(err, (err) ? 'Fail' : 'Success');
});
};