Skip to content

Instantly share code, notes, and snippets.

@alibo
Created February 20, 2017 20:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alibo/98e6176133c0a9274cc5fdc5da50ff4c to your computer and use it in GitHub Desktop.
Save alibo/98e6176133c0a9274cc5fdc5da50ff4c to your computer and use it in GitHub Desktop.
Send S3 events to s slack channel via Lambda and Node.js
'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();
})
};
@alibo
Copy link
Author

alibo commented Feb 20, 2017

Sample S3 event:

{
  "Records": [
    {
      "eventVersion": "2.0",
      "eventTime": "1970-01-01T00:00:00.000Z",
      "requestParameters": {
        "sourceIPAddress": "127.0.0.1"
      },
      "s3": {
        "configurationId": "testConfigRule",
        "object": {
          "eTag": "0123456789abcdef0123456789abcdef",
          "sequencer": "0A1B2C3D4E5F678901",
          "key": "HappyFace.jpg",
          "size": 1024
        },
        "bucket": {
          "arn": "arn:aws:s3:::mybucket",
          "name": "sourcebucket",
          "ownerIdentity": {
            "principalId": "EXAMPLE"
          }
        },
        "s3SchemaVersion": "1.0"
      },
      "responseElements": {
        "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
        "x-amz-request-id": "EXAMPLE123456789"
      },
      "awsRegion": "us-east-1",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "EXAMPLE"
      },
      "eventSource": "aws:s3"
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment