Skip to main content

Calculate Lambda function invocation duration from logs

Problem / Use case​

You want to measure how long each AWS Lambda function 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_function, cx_metadata.faas_execution as invocation_id agg
formatTimestamp(min($m.timestamp), 'timestamp_milli') as start,
formatTimestamp(max($m.timestamp), 'timestamp_milli') as end
| create duration_ms from end:number - start:number

Expected output​

lambda_functioninvocation_idstartendduration_ms
cll-lambda-nodejs<UUID>17511773803611751177381308947
cll-lambda-nodejs<UUID>17511772699531751177270800847
cll-lambda-nodejs<UUID>17511777403171751177741183866
cll-lambda-python<UUID>175117696734117511769711413800
cll-lambda-python-2<UUID>175117716124117511771627661525
cll-lambda-python-2<UUID>175117714852817511771500031475
continuousdataflowfunction-faas<UUID>1751177722174175117778258960415
databaserelatederrorsfunction-faas<UUID>175117724239517511772436851290
...

Variations​

Aggregate durations per Lambda function​

Once you’ve calculated the duration of each Lambda function invocation, you might want to summarize performance across all invocations of a given function. This helps answer questions like:

  • Which Lambda function 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_function aggregate
sum(duration_ms).round(2) as total_duration_ms,
avg(duration_ms).round(2) as avg_duration_ms,
percentile(0.95, duration_ms).round(2) as p95_duration_ms
lambda_functiontotal_duration_msavg_duration_msp95_duration_ms
cll-lambda-nodejs34936919.371162
cll-lambda-python573233821.534350.5
cll-lambda-python-272706131611.35192047.73
continuousdataflowfunction-faas78691056207.8660589.4
databaserelatederrorsfunction-faas3700264.29306
...

TL;DR​

Use diffTime on grouped timestamps to calculate Lambda function execution duration. Optionally roll up by function name for summaries.

Was this page helpful?