Export database snapshots manually to S3 & export S3 content in Glue Data Catalog using Crawler for tables fetched in Athena
Tags: aws, awsglue, awsathena, security
Published: 2021-08-06 12:56:53

“Challenges faced to find the solution of how to reduce the cost of exporting rds data to S3 without giving input as rds username and password and other details”. First and foremost I have checked the different methods of how I am able to export the rds data in S3. I got some solutions such as using Data Pipeline, Glue and doing tasks manually with database snapshot. In data pipeline, problem faced is of region as i have to do the task in mumbai region but data pipeline service is not available in mumbai region. Next I checked to do the task with AWS Glue but in Glue for exporting data to S3 i need to input rds username, password and other details in crawler. Next I have only 1 option: export the db snapshot manually to S3 and then i have calculated the cost which is charged as per GB basics. So I have chosen to do it manually.

“Challenges faced to find the solution for running the crawler in Glue to get the data in Glue data catalog”. When I have exported the data to S3 manually I have not input any KMS key arn during the configuration process and as my RDS database is encrypted for security purposes. Due to which crawler configured for getting S3 data to Glue data catalog is not running properly and not adding any tables in catalog. So to resolve this problem, I have again done the process manually to export the db snapshot data to S3 using KMS key and then after configuration of crawler in Glue I have assigned the Glue role configured to KMS as a user.

AWS Glue is a serverless data integration service that makes it easy to discover, prepare, and combine data for analytics, machine learning, and application development. AWS Glue provides all of the capabilities needed for data integration so that you can start analyzing your data and putting it to use in minutes instead of months. AWS Glue provides both visual and code-based interfaces to make data integration easier. Users can easily find and access data using the AWS Glue Data Catalog. To learn more, read the [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html). 

AWS Athena Amazon Athena is an interactive query service that makes it easy to analyze data in Amazon S3 using standard SQL. Athena is serverless, so there is no infrastructure to manage, and you pay only for the queries that you run. Athena is easy to use. Simply point to your data in Amazon S3, define the schema, and start querying using standard SQL. Most results are delivered within seconds. 

With Athena, there’s no need for complex ETL jobs to prepare your data for analysis. This makes it easy for anyone with SQL skills to quickly analyze large-scale datasets. Athena is out-of-the-box integrated with AWS Glue Data Catalog, allowing you to create a unified metadata repository across various services, crawl data sources to discover schemas and populate your Catalog with new and modified table and partition definitions, and maintain schema versioning. To learn more, read the [AWS Athena](https://docs.aws.amazon.com/athena/latest/ug/what-is.html). 

In this post, you will get to know how to export database snapshots manually to S3 and export S3 content in Glue Data Catalog using crawler to get the tables fetched in Athena. Here I have taken a centos ec2 server and mysql rds database. And created a database snapshot which I need to export to S3 for getting S3 data in Glue by which I am able to run the queries in athena and store the queries output in S3 bucket from athena for ease of access by users.

#Prerequisites
You’ll need an Amazon EC2 Server for this post. [Getting started with amazon EC2](https://aws.amazon.com/ec2/getting-started/) provides instructions on how to launch an EC2 Server.

You’ll also need an Amazon RDS Database. [Getting started with RDS](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.html) provides instructions on how to create a RDS. For this blog, I assume that I have an ec2 server and a rds database with a latest database snapshot which contains the current databases and tables of rds.

#Architecture Overview    
Diagram 1                                                                                                              
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhtcf2d84cmnr88dvh6h.png)
Diagram 2
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgq2k458bkcnknziindl.png)
Diagram 3
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r19kx32ekgbw6be3re3h.png)

The architectural diagrams show the overall deployment architecture with data flow, RDS DB Snapshot, S3, AWS Glue and AWS Athena.

#Solution overview
The blog post consists of the following phases:
1. Export of RDS db snapshot to S3.
2. Create and run Crawler in AWS Glue to export S3 data in Glue Data Catalog.
3. In Athena, run of queries and store of queries output in S3 bucket.

I have a ec2 server and a rds database with latest db snapshot as below →
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0xzwrdvzrbzql2yhcn9.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9sa42lyv4py8m49y6lbc.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nzoo8jnsbcurp5wtnybr.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48i24vhhtn0cdl3n6oqu.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gq2ns5nf26cmaiejpmcr.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8r6vsji7altgz5rwovg9.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xanm83gxiyvcyq8wu4f0.png)

##Phase 1: Export of RDS db snapshot to S3
1. Open the AWS RDS Console and goto snapshots option. Choose the snapshot and click on actions drop down and choose option export to amazon S3.  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxh7ezi78ijl8lxajp66.png)
2. Give the export identifier name as test-demo-export-1.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otuebi5bgk827mt21u3t.png)
3. You can choose a partial option for export of data as any database or table.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scdw4qzjo20eg3p11110.png)
4. I have chosen the option as ALL as I need to export full data to S3.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvhmhgszzwnf9q1eu8hj.png)
5. Created bucket named as test-demo-export with default settings. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90dvjbycia3hf0lu5cpn.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivy35in325ne34tsj6h3.png)
6. Selected S3 bucket “test-demo-export” and Created new IAM role named as test-demo-export in export db snapshot configurations. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eughdiu1827bz60pq0cn.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsbo6oxrdlmj3biu1l7q.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80dhbjcmkt0m7hgkqvra.png)
7. Created KMS key as “export-rds-snapshot-to-S3” in Key Management Service with default settings and copied the arn of key created to export of db snapshot configuration.  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6h5ln2vtrtuxr6jdv01d.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztixf0cwlr5tpz94l6pk.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/seusoof9mvabwlr5dutm.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tmk6laxsrdr9hykk2dg.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frkycbym8dd4q0j6v5jj.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxdx1v1gbfaxybszgatt.png)
8. Click on export to amazon S3 button and then the process gets started.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tj5ztbb3j2ysuzsvm7yd.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9et2gxpaol3rmuxl5wyw.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ac0y3toqmjgssthp5cov.png)
9. After completion of the export task to S3 bucket, I am able to see the databases and tables in the bucket named as test-demo-export. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd06gm7ub1w420u48sq8.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfrysirdkffb833ixshu.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z67tb149t88a4k4p7jol.png)

##Phase 2: Create and run Crawler in AWS Glue to export S3 data in Glue Data Catalog
1. In AWS Glue Console, Goto crawler option and click on the add crawler button. Then give the crawler name as test-demo and click next. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpdr6va3a28udx7eupoj.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tu7g4rt3r3u8boc570uh.png)
2. In Specify crawler source type, choose data stores option and crawl all folders option and then click next.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/guif61jmc6rr9ec1dhwx.png)
3. Choose S3 as a source data store from the drop down list. And choose the specified path in my account option and then give the path of s3 data location as s3://test-demo-export/test-demo-export-1/ and click on next.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gj2d6rnkh0ij5ph7kaif.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1aiwhc41icscultxkml2.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dul5o96c20rlcya6o6d6.png)
4. Choose No in add another data store and click next.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb09cbm5onm3aq0wxuiq.png)
5. Create a new role as test-demo and click next.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hlwe1r5o5pv68d8k0smp.png)
6. Choose a schedule for crawler as a Run on demand option as you need to run the crawler once and then click next.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oj2fnpmgkzyam0sa6cel.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mixbpbcobfylbysu04pi.png)
7. Click on add database and give name as test-demo and leave other option default and click on create button. As prefix added to tables, grouping behavior for s3 data and configuration options are optional so leave it default and click on next as do not need to add it. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd3crj6yx3pcdlgzbu4m.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyycundm5bj1fheqwctw.png)
8. Review the crawler information and click on the finish button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u00n92qq5c0r7f37sym6.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpan2l5emngenry8znil.png)
9. Crawler is created and ready to run.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lq9a305qqhh7qvkdbfmm.png)
10. Before running the crawler, goto customer managed keys and add a key user as glue services role created while configuration of crawler named as AWSGlueServiceRole-test-demo. Note that without this step crawler won't add tables in Data Catalog. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tuw90pphbxo560fcmyko.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7knrjw2h23f1q407k6m8.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le6yhfw1zrizw2ydd0j8.png)
11. Choose the crawler and click on the run crawler option.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4goiya5m1smuv6mo2je.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asrb0du5b5mszxjgcx1b.png)
12. Once the crawler starts and stops, again it will be in a ready state. And we can see the number of tables and logs in cloudwatch.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gqpjkoxb8kyhwode8cb.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stots41z5re41dlwevbm.png)
13. Click on the crawler name and can see the configuration of crawler. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l93ynyl8fjv1ae9h0ak1.png)
14. Click on the tables option to see the number of tables added in the test-demo database.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tik8cfgkqux3raql6qkr.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3lvv1tzqg5fwwp0j3g8.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gcybgjywpxmaf5we6nc.png)

##Phase 3: In Athena, run of queries and store of queries output in S3 bucket
1. Goto AWS Athena console, Select the database from drop down list and can see the tables of it.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mxf5fv6p164c6noso3u6.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77jbjc91gae0na4z2wad.png)
2. Create the folder named as “athena-test-demo-output” in the same bucket to save the output of queries run on athena.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/um0yqtqdjkzyzll8mlv0.png)
3. In Athena console, goto settings option and give the query result location as path of output folder as s3://test-demo-export/athena-test-demo-output/ and then click on save option.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4e7u62gfjf128q69ga7.png)
4. Choose any table and can preview it by clicking on the three dots option. And then click on the run query button to get the results of it.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1kboddfotui8oxtfcg3.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvv9rddmw8glserq1apj.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmhg7hoi7fxm1i7dyr3i.png)
5. Check the output folder to see the results of the run query. And can download the results from the bucket by choosing the file.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6kofe0mrbh44tlwm9qw.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmb1vtke1hbqtof30cnj.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldstazyaa7ysz36bysjh.png)
6. For creating the table manually without running the crawler, just goto create table option and then give the database name, path of s3 bucket, data format type, column name, column type and partition details and then click on create table button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvk4sfcap1chso4zw6c5.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y30i50ex18b863uci0s.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7stsc15y2l7b98ar8re7.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vltshu23dowhodyu7wki.png)
7. You can view the history of the run query and can download the results.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ucuhqe4osfftkp7go4qe.png)

#Clean-up
Delete the environment as: EC2, RDS, AWS Glue, AWS Athena, KMS, IAM, S3 and Cloudwatch log group.

#Pricing
I review the pricing and estimated cost of this example. 

For every db snapshot export to S3 depends on total storage size of data →
Cost of per GB = $0.01
For S3 storage →
Cost of per GB = $0.025
For AWS Glue Crawler depends on crawler run time →
Cost of per Data Processing Unit-Hour = $0.44
Your glue storage cost is $0, as the storage for the first million tables is free. Your first million glue requests are also free. You will be billed for one million requests above the free tier, which is $1.
For AWS Athena →
Cost of 1 TB = $5
For Key Management Service → 
Cost of customer managed KMS key version = $1

You can learn more by visiting the [AWS Glue pricing](https://aws.amazon.com/glue/pricing/) and [AWS Athena pricing](https://aws.amazon.com/athena/pricing/) service details page.

In my case, costing of using AWS glue and AWS Athena service: 
For every db snapshot export to S3 depends on total storage size of data →
Cost of 20 GB = $0.20
For S3 storage →
Cost of 2 GB = $0.05
For AWS Glue Crawler depends on crawler run time →
Cost of 0.074 DPU-Hour = $0.03
For AWS Athena →
Cost of 0.000060 Terabytes = $0.00
For Key Management Service,
Cost of 0.022 keys = $0.02 
Total Cost = $(0.20+0.05+0.03+0.00+0.02) = $0.30

#Summary
In this post, I had shown you how to export database snapshots manually to S3 and export S3 content in Glue Data Catalog using crawler to get the tables fetched in Athena.
For more details on glue and athena service, Checkout Get started with AWS Glue and AWS Athena, open the [AWS Glue console](https://ap-south-1.console.aws.amazon.com/glue/home?region=ap-south-1#) and [AWS Athena console](https://ap-south-1.console.aws.amazon.com/athena/home?region=ap-south-1#query). To learn more, read the [AWS Glue documentation](https://docs.aws.amazon.com/glue/?id=docs_gateway) and [AWS Athena documentation](https://docs.aws.amazon.com/athena/?id=docs_gateway). 

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)