Skip to content

Commit 55c1d0a

Browse files
committed
Version 1.4.0
Support for dynamic routing: * refactored codebase to be modules wrt transformers and routers * created constants to be imported into modules * added support for mocha tests
1 parent 606198a commit 55c1d0a

8 files changed

+696
-452
lines changed

README.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ eventName
128128

129129
For more information on DynamoDB Update Streams, please read http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html.
130130

131-
By default, the function will append a newline character to received data so that files delivered to S3 are nicely formatted, and easy to load into Amazon Redshift. However, the function also provides a framework to write your own transformers. If you would like to modify the data after it's read from the Stream, but before it's forwarded to Firehose, then you can implement and register a new Javascript function with the following interface:
131+
By default, the function will append a newline character to received data so that files delivered to S3 are nicely formatted, and easy to load into Amazon Redshift. However, the function also provides a framework to write your own transformers. If you would like to modify the data after it's read from the Stream, but before it's forwarded to Firehose, then you can implement and register a new Javascript function with the following interface (ideally in `transformer.js`:
132132

133133
```
134134
function(inputData, callback(err,outputData));
@@ -139,10 +139,11 @@ callback: function to be invoked once transformation is completed, with argument
139139
outputData: Buffer instance (typically 'ascii' encoded) which will be forwarded to Firehose
140140
```
141141

142-
You then register this transformer function by assigning an instance of it to the exported ```transformer``` instance:
142+
You then register this transformer function by assigning an instance of it to the exported ```transformer``` instance in the header of `index.js`:
143143

144144
```
145-
var transformer = myTransformerFunction.bind(undefined, <internal setup args>);
145+
// var useTransformer = transform.addNewlineTransformer.bind(undefined);
146+
var useTransformer = myTransformerFunction.bind(undefined, <internal setup args>);
146147
```
147148

148149
You can also take advantage of a built in regex-to-csv transformer, which can be used by un-commenting and configuring the following entry in the function:
@@ -152,6 +153,26 @@ You can also take advantage of a built in regex-to-csv transformer, which can be
152153

153154
Where ```/(myregex) (.*)/``` is the regular expression that uses character classes to capture data from the input stream to export to the CSV, and ```"|"``` is the delimiter.
154155

156+
# Delivery Stream Routing
157+
As stated previously, data will be forwarded on the basis of a Kinesis tag named `ForwardToFirehoseStream`, and if this isn't found, then it will fall back to a default delivery stream. DynamoDB update streams are always routed to the delivery stream with the same name as the base table.
158+
159+
In version 1.4.0, we added the ability to do dynamic routing. For example, you might want to route to different destinations on S3 or Redshift on the basis of the actual data being received. You can now use this by overriding the default routing, and providing a map of who records should be routed. You do this by changing the `router.defaultRouting` method to be `router.routeByAttributeMapping`. When done, you need to previde an 'attribute delivery map' which tells the router which fields to look at in your data, and how to route based on their values. You do this with a configuration object - for example to route by the value of an attribute `binaryValue` that can only be `true` or `false`:
160+
161+
```
162+
var attributeMap = {
163+
"binaryValue" : {
164+
"true" : "TestRouting-route-A",
165+
"false" : "TestRouting-route-B"
166+
}
167+
};
168+
```
169+
170+
this attribute map is then used to configure the router instance:
171+
172+
```
173+
var useRouter = router.routeByAttributeMapping.bind(undefined, attributeMap);
174+
```
175+
155176
# Confirming Successful Execution
156177

157178
When successfully configured, writes to your Stream will be automatically forwarded to the Firehose Delivery Stream, and you'll see data arriving in Amazon S3 and optionally Amazon Redshift. You can also view [CloudWatch Logs](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html) for this Lambda function as it forwards stream data to Firehose

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ npm install
2626

2727
rm $filename 2>&1 >> /dev/null
2828

29-
zip -r $filename index.js package.json node_modules/ README.md LICENSE && mv -f $filename dist/$filename
29+
zip -x \*node_modules/protobufjs/tests/\* -r $filename index.js router.js transformer.js constants.js lambda.json package.json node_modules/ README.md LICENSE NOTICE.txt && mv -f $filename dist/$filename
3030

3131
if [ "$1" = "true" ]; then
3232
aws lambda update-function-code --function-name $functionName --zip-file fileb://dist/$filename --region $region

constants.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
OK = 'OK';
2+
ERROR = 'ERROR';
3+
FORWARD_TO_FIREHOSE_STREAM = "ForwardToFirehoseStream";
4+
DDB_SERVICE_NAME = "aws:dynamodb";
5+
KINESIS_SERVICE_NAME = "aws:kinesis";
6+
FIREHOSE_MAX_BATCH_COUNT = 500;
7+
// firehose max PutRecordBatch size 4MB
8+
FIREHOSE_MAX_BATCH_BYTES = 4 * 1024 * 1024;
9+
targetEncoding = "utf8";

dist/LambdaStreamToFirehose-1.4.0.zip

2.5 MB
Binary file not shown.

0 commit comments

Comments
 (0)