Skip to content

Block 3 — Athena Data Freshness Query

Duration: 60 min


Steps

  1. Navigate to Athena → workgroup workshop-shared
  2. All slots share a single Glue database workshop_telemetry pre-created by the platform stack — no DDL needed. Confirm the table exists by running in the query editor:

    SHOW TABLES IN workshop_telemetry;
    
    AWS CLI equivalent

    aws glue get-tables \
      --database-name workshop_telemetry \
      --query 'TableList[].Name' --output table
    

  3. Run the data freshness query in the Athena query editor:

    SELECT
      thing_name,
      from_unixtime(MAX(ingest_ts) / 1000)  AS latest_edge_ts,
      current_timestamp                      AS query_ts,
      date_diff('second',
        from_unixtime(MAX(ingest_ts) / 1000),
        current_timestamp)                   AS freshness_seconds
    FROM workshop_telemetry.telemetry
    WHERE deployment_id = 'ws-slot00'
    GROUP BY thing_name
    ORDER BY freshness_seconds ASC;
    
    AWS CLI equivalent

    QUERY_ID=$(aws athena start-query-execution \
      --work-group workshop-shared \
      --query-string "SELECT thing_name, from_unixtime(MAX(ingest_ts)/1000) AS latest_edge_ts, current_timestamp AS query_ts, date_diff('second', from_unixtime(MAX(ingest_ts)/1000), current_timestamp) AS freshness_seconds FROM workshop_telemetry.telemetry WHERE deployment_id='ws-slot00' GROUP BY thing_name ORDER BY freshness_seconds ASC" \
      --query QueryExecutionId --output text)
    
    # Wait for completion
    for _i in $(seq 1 20); do
      STATE=$(aws athena get-query-execution \
        --query-execution-id "$QUERY_ID" \
        --query 'QueryExecution.Status.State' --output text)
      if [ "$STATE" = "SUCCEEDED" ] || [ "$STATE" = "FAILED" ] || [ "$STATE" = "CANCELLED" ]; then
        break
      fi
      sleep 3
    done
    echo "$STATE"
    
    # Fetch results
    aws athena get-query-results \
      --query-execution-id "$QUERY_ID" \
      --query 'ResultSet.Rows[*].Data[*].VarCharValue' --output table
    

  4. Observe that freshness is typically tens of seconds up to ~300 seconds — because data flows IoT Rule → Amazon Data Firehose → Iceberg table in S3. Firehose commits Parquet files into the Iceberg table when its buffer fills (128 MB) or the buffering interval (300 s) elapses, whichever comes first, rather than writing one object per MQTT message. At workshop-scale throughput the 300 s interval is the dominant factor, so it acts as a rough ceiling — but commits can land considerably sooner (as low as ~30 s in practice).

Why not a direct IoT Rule → S3 path?

A direct IoT Rule → S3 action (one S3 object per message) would yield lower latency but creates millions of tiny files that make Athena scans expensive. The IoT Rule → Firehose → Iceberg path batches writes into time-partitioned Parquet files, which trades a bit of freshness for dramatically lower scan cost.


Why Can't We Wire This to a Live Dashboard?

Walk through the chain of problems:

1. Every Athena query is a full S3 scan.
Each query scans all Parquet files under the Iceberg table's data files. Iceberg's partitioning and file pruning via manifest metadata reduce file count compared to per-message writes, but there is still no row-level indexing and no pushdown beyond partition filtering.

2. Athena startup overhead is irreducible.
Even simple queries incur ~2–5 seconds of planning and DPU startup before the first byte returns. At a 5-second dashboard refresh cadence, you'd be starting a new query before the previous one finishes — and paying per query.

3. S3 is not a streaming source.
Firehose commits files on a buffering interval; there is no change-notification mechanism a browser can subscribe to. You'd have to poll Athena — which amplifies both latency and cost.

This is the archive tier

Appropriate for compliance, ML training, and historical analysis. Not for operational dashboards. Sessions 3–4 introduce the higher-frequency tiers.


Wrap-Up

Recap the full Session 1 data path:

EC2 (IoT Device Client)
  → MQTT publish → IoT Core
  → IoT Rules Engine → Firehose action
  → Amazon Data Firehose (Iceberg destination) → S3
  → Athena (Glue catalog)

Preview Session 2: Next you'll use IoT Jobs to push a script update to all 3 devices simultaneously — changing telemetry frequency from 0.2 Hz to 1 Hz and adding network I/O metrics. After the job runs you'll use Fleet Indexing to query device state across your whole deployment.


Reference