Visualising your Amazon DynamoDB data with Amazon QuickSight
Tags: aws, quicksight, dynamodb
Published: 2022-03-22 15:25:57

Please noted that this content is translated from Thai language on this [Blog](https://dev.to/awscommunity-asean/amazon-dynamodb-amazon-quicksight-4mcb) because it has high demand for implementing this solution in real life.

## Table of Content
1. [Introduction](#intro)
2. [Architecture Diagram](#diagram)
3. [Cost Estimation](#cost)
4. [Step 1: Prepare AWS Services](#step1)
5. [Step 2: Create Amazon Athena data source](#step2)
6. [Step 3: Test query on your data in DynamoDB via Amazon Athena](#step3)
7. [Step 4: Connect Amazon QuickSight and Amazon Athena with Federated Query](#step4)
8. [Step 5: Create QuickSight dataset of DynamoDB via Athena](#step5)
9. [Step 6: Create QuickSight analyse from DynamoDB dataset](#step6)
10. [Conclusion](#summary)

<a name="intro"></a>
## Introduction
In modern application, NoSQL databases are used widely in the organisation, including Amazon DynamoDB, Amazon DocumentDB, Apache HBase, and so on, which each of them has their own query language. However, there is a demand that want to visualise those data in form of graphs for business purpose.

AWS has Amazon QuickSight, a cloud-native business intelligent tool that help you to create a dashboard, visualise data, find insights, and has build-in Machine Learning capability. Amazon QuickSight support many [native build-in data source](https://docs.aws.amazon.com/quicksight/latest/user/supported-data-sources.html), such as Amazon RDS, Amazon Aurora, Self-Managed MySQL, or 3rd party software.

But NoSQL databases, which mostly do not natively support SQL language, need to have data pipeline or data processing to store that data into somewhere else, for example Amazon S3, before visualising that data in the business intelligent tool.

In this blog, I will show you how to visualise data on Amazon DynamoDB, one of AWS NoSQL database, without moving the data out, and natively update the data in near-real time on Amazon QuickSight.
<a name="diagram"></a>
## Architecture Diagram
![Architecture Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r95r7hnbyynmf4j2vyud.png)

<a name="cost"></a>
## Cost Estimation
* Pricing for Storage and Read Capacity Unit (RCU) of [Amazon DynamoDB](https://aws.amazon.com/dynamodb/pricing/)
* Pricing for Data Scan of [Amazon Athena](https://aws.amazon.com/th/athena/pricing/)
* Pricing for User and SPICE of [Amazon QuickSight](https://aws.amazon.com/quicksight/pricing/)
* Pricing for Storage of [Amazon S3](https://aws.amazon.com/s3/pricing)

> :information_source: Info: It might has a bit additional price for AWS Lambda and Data Transfer.

<a name="step1"></a>
## Step 1: Prepare AWS Services
* Get started at Amazon Athena console, the engine version 2 is required, and prepare S3 Result Data bucket (e.g. nutchanon-athena-query-results).
![AthenaS3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye01n9o87zsf59t6s2zn.png)
* Create an Amazon S3 bucket for Spill Bucket Data of Athena.
![Spill](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oal0s3mw6nlnevb02cqc.png)
* Go to Amazon QuickSight console - [Setting Up for Amazon QuickSight](https://docs.aws.amazon.com/quicksight/latest/user/setting-up.html).
![QuickSightStart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsirfu1rctrua2x9xwzn.png)
* Prepare Amazon DynamoDB table with the sample data (e.g.quicksight-ddb).
![DDBData](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ld0ayp6c54bua1qjttfv.png)

> :information_source: Info: if you do not have the data in DynamoDB for testing, please check [วิธีการนำข้อมูล CSV จาก Amazon S3 เข้า Amazon DynamoDB](https://dev.to/awscommunity-asean/csv-amazon-s3-amazon-dynamodb-3i2f) (English translation is required)

<a name="step2"></a>
## Step 2: Create Amazon Athena data source
* Create new data source from "Connect data source".
![AthenaData](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkvfsggc8bajzan6qxkn.png)
* Choose "Query a data source" and "Amazon DynamoDB".
![AtDataSrc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u8bs46kvkvbrnrvg3ad.png)
* Choose "Configure new AWS Lambda function" for creating Lambda Connector.
![Lambda Connector](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6ubq2qqcol4e8218xvt.png)
* Choose "SpillBucket", "AthenaCatalogName" and confirm "Custom IAM Roles", and then click "Deploy". The system will automatically create AWS CloudFormation for Lambda Connector.
![LambdaConfig](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2wk9f5jbpzyu03e4axk.png)
* Go back to choose Lambda Connector that you just created (might need a refresh), then provide the name of the catalog.
![Lambda Connector2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aejudjaod01k4z78alqu.png)

<a name="step3"></a>
## Step 3: Test query on your data in DynamoDB via Amazon Athena
* Choose "Data Source" to be your newly created catalog, then the table of DynamoDB will be able to selected. Let's try to query via DynamoDB data via Athena.
![AthenaDDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymuno0t7uvrdjm8towj4.png)

<a name="step4"></a>
## Step 4: Connect Amazon QuickSight and Amazon Athena with Federated Query
* Change QuickSight region to N.Virginia for edit configuration. Then go to "Manage QuickSight".
![QSRegion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yd8b95u33l13d0f2kvh3.png)
* Allocate SPICE capacity. (What QuickSight SPICE is? Please see [Managing SPICE Capacity](https://docs.aws.amazon.com/quicksight/latest/user/managing-spice-capacity.html)
![QSSPICEAllocate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g46tuju17simz020gvsi.png)
* For "Security & Permission" choose "Add or Remove".
![QSAdminSec](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zfz0knssdxgr19bu5cx.png)
* Choose Amazon Athena (If it already has chosen, please click twice times), then you will see the window of permission for Amazon S3. Next, choose Athena Spill and Result Data Bucket with write permission.
![QSPermission](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mal9fmynwzu4ehnh03nb.png)
![QSS3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dplbglzzqggqu31xuaqp.png)
* Change QuickSight region to Singapore (or your preferred region) for creating the dataset.
* Go back to AWS Management Console and go to "IAM" Service. Then go to IAM Role.
* Add policy at IAM Role "aws-quicksight-s3-consumers-role-v0", which the system automated generated when you grant S3 permission in QuickSight.
![QSIAMRole](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/as7jsb2eyhe84ynmecgw.png)
Add "inline policy" name "InvokeAthenaFedereted" by follow JSON document as below.

> :warning:  Please change "aws_account_id" and Lambda Function name that you created in step 2 ("AthenaCatalogName").
```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "InvokeAthenaFedereted",
            "Effect": "Allow",
            "Action": "lambda:InvokeFunction",
            "Resource": "arn:aws:lambda:ap-southeast-1:<aws_account_id>:function:dynamocatalog"
        }
    ]
}
```

<a name="step5"></a>
## Step 5: Create QuickSight dataset of DynamoDB via Athena
* Create dataset from Athena Data Source with the connection name, e.g."athena-dynamodb".
![QSAthenaDDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zl43y44pat7g4udhebj6.png)
* Choose catalog name that you created in step 2, and the table.
![QSCatalog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo5j1bxs9rk24wx7i3gd.png)
* Choose to create from SPICE.
![QSSPICE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25fu73ywfcogbr4ffb52.png)
* Wait until the data is imported into SPICE.
![QSSPICE/](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mzvhrxsashep79rspig.png)
* Change data type of each column appropriately for the data calculation and filtering in the future.
![QSDataType](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oee5q3kdtklry6m2ozap.png)

<a name="step6"></a>
## Step 6: Create QuickSight analyse from DynamoDB dataset
* You can create analyse and dashboard directly from DynamoDB data.
![QSAnalyse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en839wel301qbs9w7bc0.png)

> :information_source:  Info: Changing the data type of dataset directly affect how to do calculation on graphs.

<a name="summary"></a>
## Conclusion
Amazon QuickSight can connect to non-directly support data source via Amazon Athena Federeted Query, which allow you to expand the functionality of building graph on many data source. By using Amazon Athena Federeted Query, you can even expand to NoSQL databases on AWS, such as Amazon DynamoDB, Amazon DocumentDB, or Amazon OpenSearch Service.

This solution will help to you easily build dashboard or visualise data on Amazon QuickSight without data movement or need to build data pipeline platform. Also, the data that show in the dashboard will be refreshed in near real-time based-on the source of data as well.

![CastleArm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3pp3cc9du87hqg20ugb.jpg)

### Reference
[1] https://aws.amazon.com/blogs/big-data/accessing-and-visualizing-data-from-multiple-data-sources-with-amazon-athena-and-amazon-quicksight/
[2] https://aws.amazon.com/blogs/big-data/query-any-data-source-with-amazon-athenas-new-federated-query/