Skip to content

Instantly share code, notes, and snippets.

@ijin
Last active June 21, 2017 04:06
Show Gist options
  • Save ijin/7bcba3354814d3ca704d to your computer and use it in GitHub Desktop.
Save ijin/7bcba3354814d3ca704d to your computer and use it in GitHub Desktop.
AWS Lambda function to set an Auto Scaling instance to be Unhealthy in response to a SNS message
console.log('Loading event');
var aws = require('aws-sdk');
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:');
console.log(event);
//console.log(typeof event.Subject);
if (event.hasOwnProperty('Message')) {
var msg = event.Message.replace(/!!/g,'"');
var instance_id = JSON.parse(msg).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 message');
context.done(null,'');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment