Skip to content

Commit 0f5667b

Browse files
Init commit
1 parent 53ab39d commit 0f5667b

19 files changed

+5048
-7
lines changed

.gitignore

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
.aws-sam/
2+
3+
# Created by https://www.toptal.com/developers/gitignore/api/pycharm+all,visualstudiocode,vuejs,macos,windows
4+
# Edit at https://www.toptal.com/developers/gitignore?templates=pycharm+all,visualstudiocode,vuejs,macos,windows
5+
6+
### macOS ###
7+
# General
8+
.DS_Store
9+
.AppleDouble
10+
.LSOverride
11+
12+
# Icon must end with two \r
13+
Icon
14+
15+
16+
# Thumbnails
17+
._*
18+
19+
# Files that might appear in the root of a volume
20+
.DocumentRevisions-V100
21+
.fseventsd
22+
.Spotlight-V100
23+
.TemporaryItems
24+
.Trashes
25+
.VolumeIcon.icns
26+
.com.apple.timemachine.donotpresent
27+
28+
# Directories potentially created on remote AFP share
29+
.AppleDB
30+
.AppleDesktop
31+
Network Trash Folder
32+
Temporary Items
33+
.apdisk
34+
35+
### macOS Patch ###
36+
# iCloud generated files
37+
*.icloud
38+
39+
### PyCharm+all ###
40+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
41+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
42+
43+
# User-specific stuff
44+
.idea/**/workspace.xml
45+
.idea/**/tasks.xml
46+
.idea/**/usage.statistics.xml
47+
.idea/**/dictionaries
48+
.idea/**/shelf
49+
50+
# AWS User-specific
51+
.idea/**/aws.xml
52+
53+
# Generated files
54+
.idea/**/contentModel.xml
55+
56+
# Sensitive or high-churn files
57+
.idea/**/dataSources/
58+
.idea/**/dataSources.ids
59+
.idea/**/dataSources.local.xml
60+
.idea/**/sqlDataSources.xml
61+
.idea/**/dynamic.xml
62+
.idea/**/uiDesigner.xml
63+
.idea/**/dbnavigator.xml
64+
65+
# Gradle
66+
.idea/**/gradle.xml
67+
.idea/**/libraries
68+
69+
# Gradle and Maven with auto-import
70+
# When using Gradle or Maven with auto-import, you should exclude module files,
71+
# since they will be recreated, and may cause churn. Uncomment if using
72+
# auto-import.
73+
# .idea/artifacts
74+
# .idea/compiler.xml
75+
# .idea/jarRepositories.xml
76+
# .idea/modules.xml
77+
# .idea/*.iml
78+
# .idea/modules
79+
# *.iml
80+
# *.ipr
81+
82+
# CMake
83+
cmake-build-*/
84+
85+
# Mongo Explorer plugin
86+
.idea/**/mongoSettings.xml
87+
88+
# File-based project format
89+
*.iws
90+
91+
# IntelliJ
92+
out/
93+
94+
# mpeltonen/sbt-idea plugin
95+
.idea_modules/
96+
97+
# JIRA plugin
98+
atlassian-ide-plugin.xml
99+
100+
# Cursive Clojure plugin
101+
.idea/replstate.xml
102+
103+
# SonarLint plugin
104+
.idea/sonarlint/
105+
106+
# Crashlytics plugin (for Android Studio and IntelliJ)
107+
com_crashlytics_export_strings.xml
108+
crashlytics.properties
109+
crashlytics-build.properties
110+
fabric.properties
111+
112+
# Editor-based Rest Client
113+
.idea/httpRequests
114+
115+
# Android studio 3.1+ serialized cache file
116+
.idea/caches/build_file_checksums.ser
117+
118+
### PyCharm+all Patch ###
119+
# Ignore everything but code style settings and run configurations
120+
# that are supposed to be shared within teams.
121+
122+
.idea/*
123+
124+
!.idea/codeStyles
125+
!.idea/runConfigurations
126+
127+
### VisualStudioCode ###
128+
.vscode/*
129+
!.vscode/settings.json
130+
!.vscode/tasks.json
131+
!.vscode/launch.json
132+
!.vscode/extensions.json
133+
!.vscode/*.code-snippets
134+
135+
# Local History for Visual Studio Code
136+
.history/
137+
138+
# Built Visual Studio Code Extensions
139+
*.vsix
140+
141+
### VisualStudioCode Patch ###
142+
# Ignore all local history of files
143+
.history
144+
.ionide
145+
146+
# Support for Project snippet scope
147+
.vscode/*.code-snippets
148+
149+
# Ignore code-workspaces
150+
*.code-workspace
151+
152+
### Vuejs ###
153+
# Recommended template: Node.gitignore
154+
155+
node_modules/
156+
dist/
157+
npm-debug.log
158+
yarn-error.log
159+
160+
### Windows ###
161+
# Windows thumbnail cache files
162+
Thumbs.db
163+
Thumbs.db:encryptable
164+
ehthumbs.db
165+
ehthumbs_vista.db
166+
167+
# Dump file
168+
*.stackdump
169+
170+
# Folder config file
171+
[Dd]esktop.ini
172+
173+
# Recycle Bin used on file shares
174+
$RECYCLE.BIN/
175+
176+
# Windows Installer files
177+
*.cab
178+
*.msi
179+
*.msix
180+
*.msm
181+
*.msp
182+
183+
# Windows shortcuts
184+
*.lnk
185+
186+
# End of https://www.toptal.com/developers/gitignore/api/pycharm+all,visualstudiocode,vuejs,macos,windows

ParameterizedQueriesDiagram.png

186 KB
Loading

README.md

Lines changed: 168 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,178 @@
1-
## My Project
1+
## Athena Parameterized Queries Blog
22

3-
TODO: Fill this README out!
3+
This is a repository for [Use Amazon Athena parameterized queries to provide data as a service](https://aws.amazon.com/blogs/big-data/use-amazon-athena-parameterized-queries-to-provide-data-as-a-service/) blog post. It contains a CloudFormation template for provisioning accompanying resources and assets for deploying a sample web application to demonstrate new ability to pass executions parameters to Amazon Athena [StartQueryExecution API](https://docs.aws.amazon.com/athena/latest/APIReference/API_StartQueryExecution.html).
44

5-
Be sure to:
5+
### Prerequisites:
66

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
7+
This sample application uses the following AWS services to demonstrate a Data-as-a-Service (DaaS) architecture pattern that uses Amazon Athena to query the Amazon.com customer reviews dataset.
8+
9+
- [AWS Amplify](https://docs.aws.amazon.com/amplify/latest/userguide/welcome.html)
10+
- [Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html)
11+
- [Amazon Athena](https://docs.aws.amazon.com/athena/latest/ug/what-is.html)
12+
- [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)
13+
- [Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html)
14+
- [AWS Glue](https://aws.amazon.com/glue) and AWS Glue Data Catalog
15+
- [AWS Identity and Access Management](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) (IAM)
16+
- [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)
17+
- [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)
18+
19+
The instructions assume you have:
20+
21+
- An AWS Account. For instructions, refer to [Creating an AWS account](https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-creating.html).
22+
- A CloudTrail trail created. For instructions, refer to [Creating a trail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-create-a-trail-using-the-console-first-time.html).
23+
- Amazon S3 Bucket created. For instructions, refer to [Creating a bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html).
24+
- [Node.js](https://nodejs.org/) version 16+ installed on your device.
25+
26+
### Deploy the CloudFormation Stack
27+
28+
In this section, you deploy a CloudFormation template ([athena-parameterized-queries.yaml](athena-parameterized-queries.yaml)) that creates the following resources:
29+
30+
- AWS Glue Data Catalog database
31+
- AWS Glue Data Catalog table
32+
- Amazon Athena workgroup
33+
- Three Athena prepared statements
34+
- Three Athena named queries
35+
- The API Gateway HTTP API
36+
- The Lambda execution role for Athena queries
37+
- The Lambda execution role for API Gateway HTTP API authorization
38+
- Five Lambda functions:
39+
- Update the AWS Glue Data Catalog
40+
- Authorize API Gateway requests
41+
- Submit Athena queries
42+
- List Athena prepared statements
43+
- List Athena named queries
44+
45+
![ParameterizedQueriesDiagram.png](ParameterizedQueriesDiagram.png)
46+
47+
![screenshot_1.jpg](screenshot_1.jpg)
48+
49+
To deploy the CloudFormation stack template, follow these steps:
50+
51+
1. Navigate to this [GitHub repository](https://github.com/aws-samples/amazon-athena-execution-parameters-blog).
52+
2. Clone the repository or copy the CloudFormation template `athena-parameterized-queries.yaml`.
53+
3. On the [AWS CloudFormation](https://console.aws.amazon.com/cloudformation) console, choose **Create stack**.
54+
4. Select **Upload a template file** and choose **Choose file**.
55+
5. Upload `athena-parameterized-queries.yaml`, then choose **Next**.
56+
6. On the **Specify stack details** page, enter the stack name `athena-parameterized-queries`.
57+
7. On the same page, there are two parameters:
58+
1. For **S3QueryResultsBucketName**, enter the S3 bucket name in your AWS Account and in the same AWS Region as where you're running your CloudFormation stack. (For this post, we use the bucket name value, like `my-bucket`).
59+
2. For **APIPassphrase**, enter a passphrase to authenticate API requests. You use this later.
60+
8. Choose **Next**.
61+
9. On the **Configure stack options** page, choose **Next**.
62+
10. On the **Review** page, choose **I acknowledge that AWS CloudFormation might create IAM resources with custom names**, and choose **Create stack**.
63+
64+
The script takes less than two minutes to run and change to a `CREATE_COMPLETE` state. If you deploy the stack twice in the same AWS account and Region, some resources may already exist, and the process fails with a message indicating the resource already exists in another template.
65+
66+
11. On the **Outputs** tab, copy the `APIEndpoint` value to use later.
67+
68+
Refer to [Use Amazon Athena parameterized queries to provide data as a service](https://aws.amazon.com/blogs/big-data/use-amazon-athena-parameterized-queries-to-provide-data-as-a-service/) blog for guidance on least-privilege authorization for deployment of the CloudFormation template.
69+
70+
### Deploy the Amplify application
71+
72+
In this section, you deploy your Amplify application.
73+
74+
1. In the cloned repository, open `web-application/.env` in a text editor.
75+
2. Set `AWS_API_ENDPOINT` as the `APIEndpoint` value from the CloudFormation stack **Outputs**. For example: `AWS_API_ENDPOINT="https://123456abcd.execute-api.your-region.amazonaws.com"`.
76+
3. Set `API_AUTH_CODE` as the value you input as the CloudFormation stack's `APIPassphrase` parameter argument. For example: `API_AUTH_CODE="YOUR_PASSPHRASE"`.
77+
4. Navigate to the `web-application/` directory and run `npm install`.
78+
5. Run `npm run build` to compile distribution assets.
79+
6. On the [Amplify](https://console.aws.amazon.com/amplify) console, choose **All apps**.
80+
7. Choose **New app**.
81+
8. Select **Host web app**, select **Deploy without Git provider**, then choose **Continue**.
82+
9. For **App name**, enter `Athena Parameterized Queries App`.
83+
10. For **Environment name**, you don't need to enter a value.
84+
11. Select **Drag and Drop**.
85+
12. Locate `dist/` directory inside `web-application/`, drag it into the window and drop it. Ensure you drag the entire directory, not the files within it.
86+
87+
![screenshot_2.jpg](screenshot_2.jpg)
88+
89+
13. Choose **Save and deploy** to deploy the web application on Amplify.
90+
91+
This step will take less than a minute to complete.
92+
93+
14. Under **App settings**, choose **Access control**, then choose **Manage access**
94+
15. Select **Apply a global password**, then enter values for **Username** and **Password**.
95+
96+
You will use these credentials to access your Amplify application.
97+
98+
##### Troubleshooting:
99+
100+
- If the Amplify app page shows as blank, refer back to Step 10 and make sure to drag and drop the entire **dist/** directory instead of individual files inside of it
101+
- If you see **Network Error**, verify that **web-application/.env** file has the correct API Endpoint URL (refer back to Step 12 in **Instruction to deploy the API**)
102+
- If you see error **status code 403**, verify that **web-application/.env** file has the correct Passphrase (refer back to Step 1)
103+
104+
### Access your Amplify application and run queries
105+
106+
In this section, you use the Amplify application to run Athena parameterized queries against the Amazon.com customer reviews dataset. The left side of the application shows how you can run parameterized queries using Athena prepared statements. The right side of the application shows how you can run parameterized queries without prepared statements, such as if the queries are written in your code; this sample uses named queries within the Athena workgroup. For more information about named queries, refer to [NamedQuery](https://docs.aws.amazon.com/athena/latest/APIReference/API_NamedQuery.html) in the [Amazon Athena API Reference](https://docs.aws.amazon.com/athena/latest/APIReference/Welcome.html).
107+
108+
1. Open the Amplify web application link located under **Domain**. For example: `https://dev123.abcd12345xyz.amplifyapp.com/`.
109+
2. In the **Sign in** prompt, enter the user name and password your provided as the Amplify application global password.
110+
3. For **Workgroup Name**, choose `ParameterizedStatementsWG` workgroup.
111+
4. Choose a statement example on the **Prepared Statement** or **SQL Statement** drop-down menu.
112+
113+
Selecting a statement displays a description about the query, including examples of parameters you can try with this statement, and the original SQL query string. SQL parameters of type string must be surrounded by single quotes, for example: `'your_string_value'`.
114+
115+
5. Enter your query parameters.
116+
117+
The following figure depicts an example of the parameters to input for the `product_helpful_reviews` prepared statement.
118+
119+
![screenshot_3.jpg](screenshot_3.jpg)
120+
121+
6. Choose **Run Query** to send the query request to the API endpoint.
122+
123+
After the query runs, the results will be returned as a table. The complete query workflow is depicted in the previous architecture diagram.
124+
125+
#### Using execution parameters with the AWS SDK for Python (Boto3)
126+
127+
In this section, you inspect the Lambda function code for using the `StartQueryExecution` API with and without prepared statements.
128+
129+
1. On the [Lambda](https://console.aws.amazon.com/lambda) console, choose **Functions**.
130+
2. Navigate to `LambdaAthenaFunction-athena-parameterized-queries` Lambda function.
131+
3. Choose the **Code Source** window.
132+
133+
Examples of passing parameters to the Athena `StartQueryExecution` API using AWS SDK for Python (Boto3) begin on lines 39 and 49. Note the new `ExecutionParameters` option on lines 45 and 55.
134+
135+
The following code uses execution parameters with Athena prepared statements:
136+
137+
```
138+
response = athena.start_query_execution(
139+
QueryString=f'EXECUTE {statement}', # Example: "EXECUTE prepared_statement_name"
140+
WorkGroup=workgroup,
141+
QueryExecutionContext={
142+
'Database': 'athena_prepared_statements'
143+
},
144+
ExecutionParameters=input_parameters
145+
)
146+
```
147+
148+
The following code uses execution parameters without Athena prepared statements:
149+
150+
```
151+
response = athena.start_query_execution(
152+
QueryString=statement, # Example: "SELECT \* FROM TABLE WHERE parameter_name = ?"
153+
WorkGroup=workgroup,
154+
QueryExecutionContext={
155+
'Database': 'athena_prepared_statements'
156+
},
157+
ExecutionParameters=input_parameters
158+
)
159+
```
160+
161+
### Clean up
162+
163+
In this post, you created several components, which generate cost. To avoid incurring future charges, remove the resources with the following steps:
164+
165+
1. [Delete the S3 bucket’s results prefix](https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-folders.html#delete-folders) created after you ran a query on your workgroup.
166+
167+
With the default template, the prefix is named `<S3QueryResultsBucketName>/athena-results`. Use caution in this step. Unless you are [using versioning on your S3 bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Versioning.html), deleting S3 objects cannot be undone.
168+
169+
2. On the Amplify console, select the app to delete and on the **Actions** menu, choose **Delete app**, then confirm.
170+
3. On the AWS CloudFormation console, select the stack to delete, choose **Delete**, and confirm.
9171

10172
## Security
11173

12174
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
13175

14176
## License
15177

16-
This library is licensed under the MIT-0 License. See the LICENSE file.
17-
178+
This project is licensed under the MIT-0 License. See the LICENSE file.

0 commit comments

Comments
 (0)