Why?
The data of a DynamoDb table is not so easy to analyze as a RDS with e.g., the pgAdmin. It will be somehow possible with scan operation but it’s in the most cases not recommented.
Another possibility is the export to S3 functionylity.
In this post, it’s described to use streams. Since 11/2020, it is also possible to use kinesis data streams for such a case.
That also allows to analyze changes and use it for audits.
A example with DynamoDb streams are here:
Architecture
The lambda is sending fake person data to DynamoDb. The integration of the Kinesis Data Stream into the DynamoDb is connected to the Kinesis Firehose, which sends the changes partitioned to the S3 bucket.
The Glue crawler will recognize the data structure and create a table, which can be accessed from Athena to analyze the data.
Let’s see the certain building blocks
Lambda (for data creation)
The Lambda is created with a module from serverless.tf.
The source code is here
The number of created persons depends on the test event.
DynamoDb and Kinesis Data Stream
This is the creation of the DynamoDb with the Kinesis Data Stream.
That adds to the DynamoDb, a Kinesis Data Stream, and connects it to the DynamoDb.
![kinesis data stream]({{ site.baseurl }}/img/2021-08-27-aws_example_ddb_analytics/kinesis_data_stream.png)
![kinesis data stream ddb]({{ site.baseurl }}/img/2021-08-27-aws_example_ddb_analytics/kinesis_data_stream_ddb.png)
Kinesis Data Firehose and S3 Bucket
Kinesis Data Firehose is the connection between the Kinesis Data Stream to the S3 Bucket.
Unfortunately, Firehose stores the JSONs without a linefeed. Therefore it’s a lambda for conversion is necessary.
More about that is described in this post
Besides policy configuration, it looks like this.
Details are here
The delivery of the data to the S3 bucket is buffered. Here are the default values.
![firehose-buffer]({{ site.baseurl }}/img/2021-08-27-aws_example_ddb_analytics/firehose_buffer.png)
Glue crawler
Athena needs a structured table for the SQL queries. The Glue crawler creates this from the data in the S3 bucket.
Details here
For test purposes, it’s enough to run the crawler before any analysis. Scheduling is also possible.
![glue-run-crawler]({{ site.baseurl }}/img/2021-08-27-aws_example_ddb_analytics/glue_run_crawler.png)
That creates this table, which is accessible by Athena.
![glue-table]({{ site.baseurl }}/img/2021-08-27-aws_example_ddb_analytics/glue_table.png)
Athena
For Athena it needs an S3 bucket for the query results and, for better isolation to other projects, a workgroup.
Details here
Analysis
First select the new workgroup.
![athena-workgroup]({{ site.baseurl }}/img/2021-08-27-aws_example_ddb_analytics/athena_workgroup.png)
And than the new Database.
![athena-database]({{ site.baseurl }}/img/2021-08-27-aws_example_ddb_analytics/athena_database.png)
Query example
DynamoDb sends the changes of an item as INSERT, MODIFY or REMOVE. To the current data of the table, this Query will work.
![athena-ddb]({{ site.baseurl }}/img/2021-08-27-aws_example_ddb_analytics/athena_ddb.png)
Cost Alert 💰
⚠️ Don’t forget to destroy after testing. Kinesis Data Streams has costs per hour
Code
https://github.com/JohannesKonings/test-aws-dynamodb-athena-tf