forked from varshneyjayant/S3ToLoggly
-
Notifications
You must be signed in to change notification settings - Fork 3
/
S3ToLoggly.js
118 lines (99 loc) · 4.1 KB
/
S3ToLoggly.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//AWS Lambda Script to send S3 logs to Loggly
var aws = require('aws-sdk')
var s3 = new aws.S3({apiVersion: '2006-03-01'})
var _ = require('lodash')
, async = require('async')
, request = require('request')
, Transform = require('stream').Transform
, csv = require('csv-streamify')
, JSONStream = require('JSONStream')
// Set the tag 'loggly-customer-token'to set Loggly customer token on the S3 bucket.
// Set the tag 'loggly-tag' to set Loggly tag on the S3 bucket.
LOGGLY_URL_BASE = 'https://logs-01.loggly.com/bulk/'
BUCKET_LOGGLY_TOKEN_NAME = 'loggly-customer-token'
BUCKET_LOGGLY_TAG_NAME = 'loggly-tag'
// Used if no S3 bucket tag doesn't contain customer token.
// Note: You either need to specify a cutomer token in this script or via the S3 bucket tag else an error is logged.
DEFAULT_LOGGLY_URL = null
if ( typeof LOGGLY_TOKEN !== 'undefined' ) {
DEFAULT_LOGGLY_URL = LOGGLY_URL_BASE + LOGGLY_TOKEN;
if ( typeof LOGGLY_TAG !== 'undefined' ) {
DEFAULT_LOGGLY_URL += '/tag/' + LOGGLY_TAG;
}
}
if ( DEFAULT_LOGGLY_URL ) {
console.log('Loading S3ToLoggly, default Loggly endpoint: ' + DEFAULT_LOGGLY_URL);
}
else {
console.log('Loading S3ToLoggly, NO default Loggly endpoint, must be set in bucket tag ' + BUCKET_LOGGLY_TOKEN_NAME );
}
exports.handler = function(event, context) {
// console.log('Received event');
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
var size = event.Records[0].s3.object.size;
if ( size == 0 ) {
console.log('S3ToLoggly skipping object of size zero')
}
else {
// Download the logfile from S3, and upload to loggly.
async.waterfall([
function buckettags(next) {
var params = {
Bucket: bucket /* required */
};
s3.getBucketTagging(params, function(err, data) {
if (err) {
next(err); console.log(err, err.stack);
} // an error occurred
else {
var s3tag = _.zipObject(_.map(data['TagSet'], 'Key'),
_.map(data['TagSet'], 'Value'));
if (s3tag[BUCKET_LOGGLY_TOKEN_NAME]) {
LOGGLY_URL = LOGGLY_URL_BASE + s3tag[BUCKET_LOGGLY_TOKEN_NAME];
if ( s3tag[BUCKET_LOGGLY_TAG_NAME] ) {
LOGGLY_URL += '/tag/' + s3tag[BUCKET_LOGGLY_TAG_NAME];
}
}
else {
LOGGLY_URL = DEFAULT_LOGGLY_URL
}
}
if ( LOGGLY_URL ) next();
else next('No Loggly customer token. Set S3 bucket tag ' + BUCKET_LOGGLY_TOKEN_NAME)
});
},
function download(next) {
// Download the image from S3 into a buffer.
s3.getObject({
Bucket: bucket,
Key: key
}, next);
},
function upload(data, next) {
// Stream the logfile to loggly.
var bufferStream = new Transform();
bufferStream.push(data.Body)
bufferStream.end()
console.log( 'Using Loggly endpoint: ' + LOGGLY_URL )
bufferStream.pipe(request.post(LOGGLY_URL)).on('error', function(err) {next(err)}).on('end', function() {next()})
}
],
function (err) {
if (err) {
console.error(
'Unable to read ' + bucket + '/' + key +
' and upload to loggly' +
' due to an error: ' + err
);
} else {
console.log(
'Successfully uploaded ' + bucket + '/' + key +
' to ' + LOGGLY_URL
);
}
context.done();
});
}
};