Last active
November 8, 2015 03:50
-
-
Save bageljp/060161db49586335218e to your computer and use it in GitHub Desktop.
AWS_Lambda_source_AutoScaling_SNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Loading function.'); | |
const https = require('https'); | |
const url = require('url'); | |
const slack_url = 'https://hooks.slack.com/services/XXXXX'; // slack url | |
const slack_req_opts = url.parse(slack_url); | |
slack_req_opts.method = 'POST'; | |
slack_req_opts.headers = {'Content-Type': 'application/json'}; | |
exports.handler = function(event, context) { | |
(event.Records || []).forEach(function (rec) { | |
// debug | |
console.log("Records: %j", rec); | |
if (rec.Sns) { | |
var req = https.request(slack_req_opts, function (res) { | |
if (res.statusCode === 200) { | |
context.succeed('posted to slack.'); | |
} else { | |
context.fail('status code: ' + res.statusCode); | |
} | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
context.fail(e.message); | |
}); | |
// Matching launched/terminated EC2 instance by AutoScaling | |
var message = rec.Sns.Message.match(/i-[a-z0-9]{8}/); | |
var status = ":exclamation: "; | |
var str = "*" + | |
status + | |
rec.Sns.Subject + | |
"*" + | |
"\n" + | |
"@channel EnstanceId: " + message[0]; | |
req.write(JSON.stringify({text: str, link_names: 1})); | |
req.end(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AutoScalingによりEC2が新しく起動/削除された際にSNSで通知した内容をslackへ送る。
AutoScalingからSNSへの通知設定を行い、LambdaのsourceにSNSを設定する。