Created
February 20, 2017 20:52
-
-
Save alibo/98e6176133c0a9274cc5fdc5da50ff4c to your computer and use it in GitHub Desktop.
Send S3 events to s slack channel via Lambda and Node.js
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
'use strict'; | |
/* | |
Thanks to https://gist.github.com/vgeshel/1dba698aed9e8b39a464 | |
*/ | |
console.log('Loading function'); | |
const aws = require('aws-sdk'); | |
const s3 = new aws.S3({ apiVersion: '2006-03-01' }); | |
const https = require('https'); | |
const url = require('url'); | |
const slackUrl = 'https://hooks.slack.com/services/....'; // Your incoming webhook url | |
const slackRequestOptions = url.parse(slackUrl); | |
slackRequestOptions.method = 'POST'; | |
slackRequestOptions.headers = {'Content-Type': 'application/json'}; | |
exports.handler = (event, context) => { | |
event.Records.forEach(record => { | |
let data = { | |
bucket: record.s3.bucket.name, | |
key: record.s3.object.key, | |
size: record.s3.object.size, | |
eTag: record.s3.object.eTag, | |
eventName: record.eventName, | |
time: record.eventTime, | |
requestIp: record.requestParameters.sourceIPAddress | |
} | |
if(data.key.startsWith('logs/')) { // If you enabled s3 logging | |
console.log("Ignoring, started with 'logs/': " + data.key); | |
return; | |
} | |
let req = https.request(slackRequestOptions, res => { | |
if (res.statusCode === 200) { | |
context.succeed('posted to slack'); | |
} else { | |
context.fail('status code: ' + res.statusCode); | |
} | |
}); | |
req.on('error', e => { | |
console.log('problem with request: ' + e.message); | |
context.fail(e.message); | |
}); | |
req.write(JSON.stringify({ | |
text: JSON.stringify(data, null, ' '), | |
username: "s3-notifier", | |
icon_emoji: ":ghost:", | |
channel: "#s3", | |
})); | |
req.end(); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample S3 event: