Created
April 16, 2016 13:30
-
-
Save chroju/43abb861045c2c866a0bc7a01804b852 to your computer and use it in GitHub Desktop.
aws to slack lambda function (node.js)
This file contains hidden or 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/xxxxx'; | |
| 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) { | |
| 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); | |
| }); | |
| // この辺で適当にslack postするテキストを調整する. | |
| var message = JSON.parse(rec.Sns.Message); | |
| var status = message.NewStateValue; | |
| if (status === "ALARM") { | |
| status = ":exclamation: " + status; | |
| } | |
| if (status === "OK") { | |
| status = ":+1: " + status; | |
| } | |
| var str = "*" + | |
| status + | |
| ": " + | |
| message.AlarmDescription + | |
| "*" + | |
| "\n" + | |
| message.NewStateReason; | |
| req.write(JSON.stringify({text: str})); | |
| req.end(); | |
| } | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment