Skip to content

Instantly share code, notes, and snippets.

@taufikobet
Last active February 20, 2021 11:47
Show Gist options
  • Save taufikobet/1be57c9f6fcddaeae51d to your computer and use it in GitHub Desktop.
Save taufikobet/1be57c9f6fcddaeae51d to your computer and use it in GitHub Desktop.
AWS Lambda: SNS Topic Notification to Slack (useful for tracking SNS push notification errors via Slack)
https = require('https');
url = require('url');
util = require('util');
slack_url = 'https://hooks.slack.com/services/${insert}/${your-token}/${here}';
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) {
var message = event.Records[0].Sns.Message;
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);
});
var attachment_payload = {
attachments: [
{
fallback: "fallback",
text: "Topic: push-notification-failure",
pretext: "SNS Event",
"color": "#d1372d",
fields: [
{
title: "Delivery Attempts",
value: message.DeliveryAttempts,
short: message.DeliveryAttempts
},
{
title: "Endpoint ARN",
value: message.EndpointArn,
short: message.EndpointArn
},
{
title: "Event Type",
value: message.EventType,
short: message.EventType
},
{
title: "Failure Message",
value: message.FailureMessage,
short: message.FailureMessage
},
{
title: "Failure Type",
value: message.FailureType,
short: message.FailureType
},
{
title: "Message ID",
value: message.MessageId,
short: message.MessageId
},
{
title: "Resource",
value: message.Resource,
short: message.Resource
},
{
title: "Service",
value: message.Service,
short: message.Service
},
{
title: "Time",
value: message.Time,
short: message.Time
}
]
}
]
};
payload_string = util.format("%j", attachment_payload);
req.write(payload_string);
req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment