Skip to content

Instantly share code, notes, and snippets.

@Kazuma
Created November 1, 2015 05:03
Show Gist options
  • Save Kazuma/b2b15b9f44a5a10abf2c to your computer and use it in GitHub Desktop.
Save Kazuma/b2b15b9f44a5a10abf2c to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk');
AWS.config.region = 'us-west-2';
const ec2 = new AWS.EC2();
var webhook = '<Slack WebHook URL>';
exports.handler = function(event, context) {
console.log('Start');
var list = [];
var params = {
DryRun: false,
MaxResults: 10,
Filters: [{
Name: 'instance-state-name',
Values: ['running']
}]
};
ec2.describeInstances(params, function(err, data) {
if (err) {
consle.log(err, err.stack);
} else {
for (i = 0; i < data.Reservations.length; i++) {
instance_id = "> Instance ID: " + data.Reservations[i].Instances[0].InstanceId;
list.push(instance_id);
}
if (list.length === 0) {
context.succeed("No Running Instance.");
} else {
var text = "Please check the running instances in us-west-2 (Oregon)";
var pray = ":pray: :pray: :pray:";
text = text + "\\n" + list.join('\\n') + "\\n" + pray;
var exec = require('child_process').exec;
var cmd = 'curl -X POST --data-urlencode \'payload={"text": "' + text + '"}\' ' + webhook;
exec(cmd, function(err, stdout, stderr) {
context.succeed(stdout);
});
}
}
});
console.log('Finish');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment