In [this]({{ site.baseurl }}/aws/2021/08/27/aws_example_ddb_analytics/) post is a example of a Athena query to get the the current data of the DynamoDb table.
This post explains how to provide this query with CDK as a saved query in Athena to have the query stored “nearby” the editor and that the query fits to the current deployment regarding the naming of the DB and table. Another advantage is to have the query under source control.
This is a extension of [this]({{ site.baseurl }}/aws/2021/10/26/aws_example_ddb_analytics_cdk/) post.
The saved queries are stored here in the console:
![athena save queries]({{ site.baseurl }}/img/2022-08-22-aws_example_ddb_analytics_athena_saved_queries_cdk/athena_saved_queries.png)
The SQL command
This SQL command will be provided as a saved query in Athena. For easier maintenance, the command is in an extra SQL file. The DB and the table name are placeholders in the file and will be replaced during the deployment process. That ensures that this SQL command refers to the correct resources, which are also deployed.
Saved Query Construct
Like the SQL file, the CDK definition of the saved query is in an extra file as a CDK construct.
Currently, there is no L2 Construct for a named query in CDK, so it is defined as a L1 CFN Resource (CfnNamedQuery
).
This needs the query as a string. The function getSqlString
converts and transforms the SQL file content to that needed string.
So that the placeholders are replaced with the resources, which are deployed.
The Athena table name will be created during the glue crawler process. The convention is that the table name configured prefix of the S3 bucket with an underscore (”_”) instead of dashes (”-”).
The database name and the workgroup name are passed from the stack to the construct.
Insert the saved query construct in the stack
For the deployment the saved queries construct will be inserted into the stack
The needed information are passed as props to the construct.
The saved queries are depending on the workgroup. That has to be defined in CDK. Otherwise, the deployment will fail.
Result
After the deployment, the new query is listed here and can be chosen for query in the editor.
![athena save query deployed]({{ site.baseurl }}/img/2022-08-22-aws_example_ddb_analytics_athena_saved_queries_cdk/athena_saved_query_deployed.png)
![athena save queries]({{ site.baseurl }}/img/2022-08-22-aws_example_ddb_analytics_athena_saved_queries_cdk/athena_saved_query_editor.png)
Code
https://github.com/JohannesKonings/test-aws-dynamodb-athena-cdk