By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(For AWS Solutions Architect – Associate)
You’re a cloud engineer at a mid-sized e-commerce company. Your data team dumps terabytes of raw logs, CSV files, and JSON exports into S3 every day. Marketing wants to run ad-hoc queries on customer behavior. Finance needs to reconcile transactions. The BI team is screaming because their SQL queries take hours to run on Redshift.
This is where S3 Select, Athena, and Glue come in.
Why this matters in production:- Cost: Athena charges $5 per TB scanned (vs. Redshift’s $0.25–$1.50/hour for a cluster).- Speed: S3 Select can filter a 10GB file in seconds (vs. downloading the whole thing).- Flexibility: No need to pre-load data into a database—query it where it lives.- Scalability: Athena auto-scales to petabytes of data (no manual sharding).
Real-world scenario:You inherit a legacy data lake in S3 with millions of unstructured JSON and CSV files. Your boss says:
"We need to run analytics on this data, but we can’t afford to move it into Redshift. Also, we need results by EOD."
Your options:1. Write a custom Python script to download, parse, and query (slow, error-prone, expensive).2. Use Athena + Glue to query the data in place (fast, serverless, cost-effective).
This guide will show you how to do #2—the right way.
HIVE_METASTORE_ERROR
s3://bucket/logs/year=2023/month=01/day=15/
WHERE year=2023 AND month=01
s3://bucket/logs/year=2023/month=01/
marketing
finance
aws-athena-query-results-<account-id>-<region>
✅ AWS account with admin IAM permissions (or at least AthenaFullAccess, GlueFullAccess, S3FullAccess).✅ A sample dataset in S3 (we’ll use a public dataset: s3://aws-athena-query-results-us-east-1/samples/parquet/).✅ AWS CLI installed and configured (aws configure).
AthenaFullAccess
GlueFullAccess
S3FullAccess
s3://aws-athena-query-results-us-east-1/samples/parquet/
aws configure
Athena requires a bucket to store query results. If you don’t set one, queries will fail.
# Create a bucket (replace <your-account-id> and <region>) aws s3 mb s3://athena-query-results-<your-account-id>-<region> --region <region> # Example: aws s3 mb s3://athena-query-results-123456789012-us-east-1 --region us-east-1
Verify:
aws s3 ls s3://athena-query-results-<your-account-id>-<region>/
Glue needs to know your data’s schema before Athena can query it.
athena-sample-crawler
AWSGlueServiceRole-AthenaSample
athena_samples
Verify:- Go to Glue Data Catalog → Databases → athena_samples → Tables.- You should see a table named parquet (or similar).
parquet
Now that Glue has inferred the schema, you can query the data in Athena.
sql SELECT * FROM "athena_samples"."parquet" LIMIT 10;
s3://athena-query-results-<your-account-id>-<region>/
Expected output:A table with 10 rows from the sample dataset.
Let’s simulate a real-world scenario: You have 100GB of CSV logs in S3, and queries are slow/costly.
s3://logs/year=2023/month=01/day=15/
bash aws s3 cp yellow_tripdata_2023-01.csv s3://your-bucket/raw/csv/
csv-to-parquet
Script: ```python import sys from awsglue.transforms import * from awsglue.utils import getResolvedOptions from pyspark.context import SparkContext from awsglue.context import GlueContext from awsglue.job import Job
sc = SparkContext() glueContext = GlueContext(sc) spark = glueContext.spark_session job = Job(glueContext)
# Read CSV df = spark.read.csv("s3://your-bucket/raw/csv/", header=True, inferSchema=True)
# Write as Parquet, partitioned by pickup date df.write.partitionBy("tpep_pickup_datetime").parquet("s3://your-bucket/processed/parquet/")
job.commit() 4. Run the job (takes ~5–10 minutes for 1GB of data).5. Create a new Glue crawler to infer the schema of the Parquet data.6. Query in Athena:sql -- Only scans the partition for Jan 1, 2023 SELECT * FROM "processed"."parquet" WHERE tpep_pickup_datetime = '2023-01-01' LIMIT 10; ```
4. Run the job (takes ~5–10 minutes for 1GB of data).5. Create a new Glue crawler to infer the schema of the Parquet data.6. Query in Athena:
Result:- Before: Query scans 100GB of CSV → $50 cost.- After: Query scans 1GB of Parquet → $0.50 cost.
athena:StartQueryExecution
athena:GetQueryResults
s3:*
year/month/day
s3://<company>-<env>-<purpose>/<data-type>/<partition-key>=<value>/
s3://acme-prod-logs/clickstream/year=2023/month=01/day=15/
Environment=prod
CostCenter=marketing
--enable-continuation
Athena.QueryFailed
Athena.QueryQueueTime
Athena.QueryExecutionTime
Athena.DataScanned
Glue.JobRunTime
Glue.CrawlerFailed
Query result location
NULL
SELECT *
WHERE
❌ EMR (requires a cluster).
"How can you reduce Athena query costs by 90%?"
❌ Increase Redshift cluster size (irrelevant).
"What’s the cheapest way to filter a 10GB CSV file in S3?"
❌ Download and filter locally (slow, expensive).
"You need to run ETL on S3 data before querying with Athena. Which service?"
"You need to analyze 50TB of log data stored in S3. The data is rarely accessed, and queries are ad-hoc. Which solution is the most cost-effective?"
Options:A) Load into Redshift and run queries.B) Use Athena with Parquet and partitioning.C) Spin up an EMR cluster and run Spark jobs.D) Download the data and analyze locally.
Correct Answer: B- Why? - Athena is serverless (no cluster costs). - Parquet + partitioning minimizes data scanned (low cost). - Redshift (A) is expensive for infrequent queries. - EMR (C) is overkill for ad-hoc SQL. - Downloading (D) is slow and impractical for 50TB.
You have a 1GB CSV file in S3 (s3://your-bucket/data/sales.csv). It has columns: order_id, customer_id, amount, order_date.
s3://your-bucket/data/sales.csv
order_id
customer_id
amount
order_date
Task:1. Query only the top 10 highest-value orders (amount) using S3 Select.2. Then, use Athena to find the total sales per customer (group by customer_id).
Constraints:- Do not download the file.- Use least privilege IAM roles.
aws s3api select-object-content \ --bucket your-bucket \ --key data/sales.csv \ --expression "SELECT * FROM s3object s ORDER BY s.amount DESC LIMIT 10" \ --expression-type "SQL" \ --input-serialization '{"CSV": {"FileHeaderInfo": "USE", "FieldDelimiter": ","}}' \ --output-serialization '{"CSV": {}}' \ "output.csv"
Why it works:- S3 Select streams only the filtered rows (no full download).- ORDER BY amount DESC LIMIT 10 returns the top 10 highest-value orders.
ORDER BY amount DESC LIMIT 10
sql SELECT customer_id, SUM(amount) as total_sales FROM "your_database"."sales" GROUP BY customer_id ORDER BY total_sales DESC;
SUM(amount)
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.