Calculate Lambda invocation duration from logs
Problem / use case
You want to measure how long each AWS Lambda invocation took by calculating the time difference between the first and last log entry per invocation. This is useful for debugging, performance tuning, or anomaly detection.
Query
source logs
| filter cx_metadata.faas_name != null
| groupby cx_metadata.faas_name as lambda, cx_metadata.faas_execution as invocation_id agg
min($m.timestamp) as start,
max($m.timestamp) as end
| create duration from end.diffTime(start)
Expected output
Lambda | execution_id | Logs | Start | End | Duration |
---|---|---|---|---|---|
MalfunctioningDatabaseFunction-faas | 01362d6d-3ec2-43ae-9bfb-366b7bc84242 | 5 | 09/06/2025 18:39:22.443 | 09/06/2025 18:39:22.671 | 228000000 |
MalfunctioningDatabaseFunction-faas | 4d905e36-4d26-40b5-8c78-40ddd0237839 | 4 | 09/06/2025 18:40:22.236 | 09/06/2025 18:40:22.451 | 214000000 |
OverloadSpecificTableFunction-faas | b8819ae0-eb17-423b-80db-5fd57bbabda0 | 13 | 09/06/2025 18:39:22.384 | 09/06/2025 18:39:27.832 | 5448000000 |
OverloadSpecificTableFunction-faas | 2706b0fb-4620-4582-9c2d-3ab5f21281ba | 13 | 09/06/2025 18:40:22.208 | 09/06/2025 18:40:27.680 | 5471000000 |
cll-lambda-nodejs | f657c430-1d6e-4b8c-8b16-b48e42f1f26a | 12 | 09/06/2025 18:39:49.915 | 09/06/2025 18:39:50.760 | 846000000 |
cll-lambda-nodejs | f5e9129d-3d05-4a36-beeb-7f8123fa8ddf | 12 | 09/06/2025 18:40:40.256 | 09/06/2025 18:40:41.323 | 1067000000 |
FetchCustomerHistoryFromRDS-faas | 6c378aa1-74c5-5a4c-8f3a-ea60752621e3 | 7 | 09/06/2025 18:37:22.816 | 09/06/2025 18:37:25.494 | 2678000000 |
Variations
Aggregate durations per Lambda function
Once you’ve calculated the duration of each Lambda invocation, you might want to summarize performance across all invocations of a given function. This helps answer questions like:
- Which Lambda runs the longest on average?
- What’s the total compute time per function?
- Are there outliers or performance spikes?
Add this to the end of the query:
| groupby lambda aggregate
sum(duration) as total_duration,
avg(duration) as avg_duration,
percentile(0.95, duration) as p95_duration
Lambda | Sum_Exec | Avg_Exec | P95 |
---|---|---|---|
cll-lambda-python-2 | 3d20h51m24s364ms | 22s116ms67us | 3m1s908ms781us |
cll-lambda-python | 10h37m37s194ms | 3s796ms863us | 4s558ms248us |
cll-lambda-nodejs-2 | 6d22h53m35s245ms | 1m59s233ms31us | 2s403ms508us |
cll-lambda-nodejs | 5d8h23m58s417ms | 18s349ms347us | 1s338ms689us |
OverloadSpecificTableFunction-faas | 15h20m52s575ms | 5s483ms582us | 5s536ms666us |
NotifyProcessingAndTriggerWorkflow-faasavion | 14h36m38s885ms | 425ms282us | 566ms672us |
NotifyProcessingAndTriggerWorkflow-faas | 38m3s257ms | 369ms160us | 482ms76us |
TL;DR
Use diffTime
on grouped timestamps to calculate Lambda execution duration. Optionally roll up by function name for summaries.
Theme
Light