Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWS Lambda function for forwarding SNS notifications to Slack
console.log('Loading function');
const https = require('https');
const url = require('url');
// to get the slack hook url, go into slack admin and create a new "Incoming Webhook" integration
const slack_url = 'https://hooks.slack.com/services/...';
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);
});
req.write(JSON.stringify({text: JSON.stringify(rec.Sns.Message, null, ' ')})); // for testing: , channel: '@vadim'
req.end();
}
});
};
@JeffRausch

This comment has been minimized.

Copy link

JeffRausch commented Jul 13, 2015

Thank you! This worked great!

@aalbertson

This comment has been minimized.

Copy link

aalbertson commented Nov 18, 2015

Worked well, I did modify my own a bit, to add an easy to change notify, so I can have the post trigger an @channel or @here, etc....

@aschyiel

This comment has been minimized.

Copy link

aschyiel commented Nov 23, 2015

You are a Gentleman and a Scholar; thank you! :)

@wiicode

This comment has been minimized.

Copy link

wiicode commented Aug 9, 2016

Getting a "null" message and no result. Admittedly new to Lambda. Curious if it's me or perhaps code require an update?

@vutoff

This comment has been minimized.

Copy link

vutoff commented Mar 23, 2017

Same here. Getting null all the time with no actual effect.

@monty241

This comment has been minimized.

Copy link

monty241 commented Mar 28, 2017

Thanks a lot for the base code. I've used it to integrate Slack into AWS CodeCommit (but without SNS in this case, the risk is acceptable). Ping back on http://stackoverflow.com/questions/43065388/register-an-aws-git-code-push-event-in-exact-online-erp/43065389#43065389

@ViktorHaag

This comment has been minimized.

Copy link

ViktorHaag commented Apr 19, 2017

This looks very useful; do you have a license you'd prefer to govern use of this code?

@benyanke

This comment has been minimized.

Copy link

benyanke commented Aug 30, 2017

Not sure if you'd find it useful, but I modified this to show more information about CloudWatch alerts pushed via SNS: It parses the message JSON to see if it's a cloudwatch alert, and if so, it uses slack's attachment formatting.

image

Non-CloudWatch messages are still just displayed normally.

@balexandre

This comment has been minimized.

Copy link

balexandre commented Oct 24, 2017

@benyanke good fork! I also change it for CodeCommit, might fork it to show the code as well later

for those that need a nice icon to their Slack channel, here's the new one for AWS, and crops as a square nicely in Slack Incoming Hooks settings

another trick is to use environment variables so you can actually add your slack channel in the function variables so you can use the script in another function to dump to another channels without change the code itself, like

const slack_url = process.env.SLACK_WEBHOOK_URL || 'https://hooks.slack.com/services/default/link';

and set up as:

@ssaanncchheezz

This comment has been minimized.

Copy link

ssaanncchheezz commented Aug 7, 2018

Works! Just what I needed, thank you! 😄

@midhunnemani

This comment has been minimized.

Copy link

midhunnemani commented Oct 6, 2018

How to add 2 slack urls?

@ernievd

This comment has been minimized.

Copy link

ernievd commented Jul 22, 2019

How do I update "rec.Sns.Message" to be some custom text? Can I set "rec.Sns.Message" from somewhere in the AWS dashboard?

@ernievd

This comment has been minimized.

Copy link

ernievd commented Jul 22, 2019

Newbie here - Can someone explain in little more detail on how to add this into a Lambda? I created a Lambda function, chose its runtime to be Nodejs 10.x and it opens with a default file of index.js. I assume I can not copy and paste this code into the index.js? Anything else special needed to call this from SNS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.