Skip to content

Instantly share code, notes, and snippets.

@vanga
Forked from terranware/snsToSlack.js
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save vanga/3a1b089f565036850038 to your computer and use it in GitHub Desktop.

Select an option

Save vanga/3a1b089f565036850038 to your computer and use it in GitHub Desktop.

Revisions

  1. @terranware terranware revised this gist May 17, 2015. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions snsToSlack.js
    Original file line number Diff line number Diff line change
    @@ -43,13 +43,16 @@ exports.handler = function(event, context) {
    path: '/services/your-slack-webhook-url-info-goes-here'
    };

    var req = https.request(options)
    .on('error', function(e) {
    console.log('problem with request: ' + e.message);
    })
    .on('end', function(e) {
    context.done(e);
    var req = https.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
    context.done(null);
    });
    });

    req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
    });

    req.write(util.format("%j", postData));
    req.end();
  2. @terranware terranware revised this gist May 13, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion snsToSlack.js
    Original file line number Diff line number Diff line change
    @@ -18,12 +18,14 @@ exports.handler = function(event, context) {
    var stateYellow = message.indexOf(" to YELLOW");
    var noPermission = message.indexOf("You do not have permission");
    var failedDeploy = message.indexOf("Failed to deploy application");
    var removedInstance = message.indexOf("Removed instance ");
    var addingInstance = message.indexOf("Adding instance ");
    var color = "good";

    if (stateRed != -1 || butWithErrors != -1 || noPermission != -1 || failedDeploy != -1) {
    color = "danger";
    }
    if (stateYellow != -1) {
    if (stateYellow != -1 || removedInstance != -1 || addingInstance != -1) {
    color = "warning";
    }

  3. @terranware terranware created this gist May 12, 2015.
    54 changes: 54 additions & 0 deletions snsToSlack.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    var https = require('https');
    var util = require('util');

    exports.handler = function(event, context) {
    console.log(JSON.stringify(event, null, 2));
    console.log('From SNS:', event.Records[0].Sns.Message);

    var postData = {
    "channel": "#aws-sns",
    "username": "AWS SNS via Lamda :: DevQa Cloud",
    "text": "*" + event.Records[0].Sns.Subject + "*",
    "icon_emoji": ":aws:"
    };

    var message = event.Records[0].Sns.Message;
    var butWithErrors = message.indexOf(" but with errors");
    var stateRed = message.indexOf(" to RED");
    var stateYellow = message.indexOf(" to YELLOW");
    var noPermission = message.indexOf("You do not have permission");
    var failedDeploy = message.indexOf("Failed to deploy application");
    var color = "good";

    if (stateRed != -1 || butWithErrors != -1 || noPermission != -1 || failedDeploy != -1) {
    color = "danger";
    }
    if (stateYellow != -1) {
    color = "warning";
    }

    postData.attachments = [
    {
    "color": color,
    "text": message
    }
    ];

    var options = {
    method: 'POST',
    hostname: 'hooks.slack.com',
    port: 443,
    path: '/services/your-slack-webhook-url-info-goes-here'
    };

    var req = https.request(options)
    .on('error', function(e) {
    console.log('problem with request: ' + e.message);
    })
    .on('end', function(e) {
    context.done(e);
    });

    req.write(util.format("%j", postData));
    req.end();
    };