Created
October 29, 2021 08:08
-
-
Save lukaszbudnik/8c7f560a6f91d771c80fb78fcb644879 to your computer and use it in GitHub Desktop.
Kinesis Firehose Data Transformation Lambda
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
const redact = function (str) { | |
return str[0] + "*".repeat(str.length - 2) + str.slice(-1); | |
} | |
const transform = (data) => { | |
// decode Kinesis Data Firehose raw data and convert it to JSON object | |
var parsedData = JSON.parse(new Buffer.from(data,'base64').toString('utf8')); | |
// you can now transform the JSON object | |
// 1. extract properties to top level | |
parsedData.status = parsedData.detail.status; | |
// 2. redact fields | |
parsedData.email = redact(parsedData.email) | |
// 3. or even delete fields | |
delete parsedData.detail; | |
// convert back to base64 encoded string | |
return new Buffer.from(JSON.stringify(parsedData) + '\n').toString('base64'); | |
} | |
exports.handler = async (event, context) => { | |
const output = event.records.map((record) => ({ | |
recordId: record.recordId, | |
result: 'Ok', | |
data: transform(record.data), | |
})); | |
return { records: output }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment