Skip to content

Instantly share code, notes, and snippets.

@ijin
Created April 17, 2015 01:17
Show Gist options
  • Save ijin/52033eb3b9b02c1fe975 to your computer and use it in GitHub Desktop.
Save ijin/52033eb3b9b02c1fe975 to your computer and use it in GitHub Desktop.
makeASUnhealty
var aws = require('aws-sdk');
aws.config.update({region: 'ap-northeast-1'});
var s3 = new aws.S3({apiVersion: '2006-03-01'});
var autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'});
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
var message = JSON.parse(event.Records[0].Sns.Message);
if (message.NewStateValue === 'ALARM') {
var instance_id = message.Trigger.Dimensions[0].value;
console.log('Changing instance health for: ' + instance_id);
var params = {
HealthStatus: 'Unhealthy', /* required */
InstanceId: instance_id, /* required */
ShouldRespectGracePeriod: false
};
autoscaling.setInstanceHealth(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
context.done('error','error: '+err);
}
else {
console.log(data); // successful response
context.done(null,'');
}
});
}
else {
console.log('No Records');
context.done(null,'');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment