Amazon Athena - Hands On
Walking through the Amazon Athena console and querying raw S3 access logs using SQL is where the power of serverless data lakes really hits. π
Stephaneβs hands-on lab breaks down the mandatory setup sequence and real-world log analytics queries. Let's summarize the step-by-step workflow, mandatory syntax rules.
Hands Onβ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ATHENA S3 ACCESS LOG WORKFLOW β
βββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββ
βΌ βΌ βΌ
ποΈ 1. QUERY RESULT LOCATION π 2. DATABASE & TABLE SCHEMA π 3. SQL LOG ANALYTICS
β’ Create dedicated S3 results bucket β’ `CREATE DATABASE s3_access_logs_db` β’ Preview table (`LIMIT 10`)
β’ Configure in Athena Settings (`s3://.../`) β’ Execute AWS DDL SerDe schema query β’ Run aggregations (`COUNT(*) BY status`)
β’ Prevents execution errors β’ Target S3 location with trailing slash (`/`) β’ Filter 403 errors for unauthorized checks
Step 1: Configure S3 Query Result Output Locationβ
- Before running any query in Athena, you must specify an S3 output directory (e.g.,
s3://aws-athena-results-bucket/). - Athena automatically dumps query output CSV files and execution metadata into this designated bucket.

Step 2: Create Database & DDL External Table Schemaβ
- Execute
CREATE DATABASE s3_access_logs_db;in the query editor.

- Create the external schema using Hive Regex SerDe:
CREATE EXTERNAL TABLE IF NOT EXISTS s3_access_logs_db.mybucket_logs(
BucketOwner STRING,
Bucket STRING,
RequestDateTime STRING,
RemoteIP STRING,
Requester STRING,
RequestID STRING,
Operation STRING,
Key STRING,
RequestURI_operation STRING,
RequestURI_key STRING,
RequestURI_httpProtoversion STRING,
HTTPstatus STRING,
ErrorCode STRING,
BytesSent BIGINT,
ObjectSize BIGINT,
TotalTime STRING,
TurnAroundTime STRING,
Referrer STRING,
UserAgent STRING,
VersionId STRING,
HostId STRING,
SigV STRING,
CipherSuite STRING,
AuthType STRING,
EndPoint STRING,
TLSVersion STRING
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
WITH SERDEPROPERTIES (
'serialization.format' = '1', 'input.regex' = '([^ ]*) ([^ ]*) \\[(.*?)\\] ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) \\\"([^ ]*) ([^ ]*) (- |[^ ]*)\\\" (-|[0-9]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) (\"[^\"]*\") ([^ ]*)(?: ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*))?.*$' )
LOCATION 's3://target-bucket-name/prefix/';
tip
You can reuse the access log bucket from previous labs (S3 Access Logs Lab) or create a new S3 bucket and enable access logging on it.

Step 3: Run SQL Analytical Queriesβ
- Status Aggregation (Error Breakdown):
SELECT requesturi_operation, httpstatus, count(*) FROM "s3_access_logs_db"."mybucket_logs"
GROUP BY requesturi_operation, httpstatus;

- Security & Access Analysis (Searching for 403 Forbidden Errors):
SELECT * FROM "s3_access_logs_db"."mybucket_logs"
where httpstatus='403';

Exam Tipsβ
- The Missing Query Location Trap π¨: If a developer executes an Athena query via the AWS Management Console or AWS CLI (
StartQueryExecution) and gets aMissing query result locationerrorβthe fix is setting the S3 output location under Athena Settings or workgroup configuration.

CREATE EXTERNAL TABLESyntax: Notice that Athena usesCREATE EXTERNAL TABLE. The wordEXTERNALtells Athena that the underlying dataset lives outside Athena inside S3, meaning deleting or dropping the table inside Athena does NOT delete the raw source files inside S3!