kinesis-lambda-aggregator
Source on GitHub · All examples
A Kinesis stream triggers a Node.js Lambda that counts events by type and adds the counts to a DynamoDB table.
put-record -> event-stream --event source mapping--> event-processor --UpdateItem--> event-metricsWhat it shows
- OpenTofu (
main.tf) creates the streamevent-stream(1 shard), thenodejs20.xLambdaevent-processor(TABLE_NAME=event-metrics) and an event source mapping withTRIM_HORIZONand batch size 100. - The table
event-metrics(keyeventType) is created by alocal-execprovisioner that runs the AWS CLI, so the AWS CLI must be on the path whentofu applyruns. demo.shputs five records (clickx3,purchasex2), waits 5 s, then scans the table.- The Lambda decodes each base64 record, counts
type, and runsUpdateItemwithADD eventCount. It uses the injectedAWS_ENDPOINT_URL.
Prerequisites
- Docker, OpenTofu (
tofu), AWS CLI v2.
WARNING
The compose file mounts /var/run/docker.sock and runs Gopherstack as root so it can start the Lambda container.
NOTE
main.tf and demo.sh use http://localhost:8000, so Gopherstack must listen there. demo.sh uses --cli-binary-format raw-in-base64-out, which needs AWS CLI v2.
Run
docker compose up -d --build
docker compose ps # wait until healthy
./demo.shOn Docker Desktop, start the stack with LAMBDA_DOCKER_HOST=host.docker.internal docker compose up -d --build. The default is 172.17.0.1, the Linux Docker bridge gateway.
Expected output
After the tofu apply output and five put-record responses (ShardId, SequenceNumber):
=== Waiting for Event Source Mapping to Poll (5s) ===
=== Aggregated Metrics in DynamoDB ===
[
{"eventCount": {"N": "3"}, "eventType": {"S": "click"}},
{"eventCount": {"N": "2"}, "eventType": {"S": "purchase"}}
](The CLI prints each object over several lines.) The fixed 5 s wait includes the Lambda container start. Re-running demo.sh adds to the counts.
Files
| File | Purpose |
|---|---|
main.tf | Stream, table, role, Lambda, event source mapping |
src/index.js | Aggregating handler |
demo.sh | Deploy, send events, scan |
Clean up
tofu destroy -auto-approve
docker compose down -v