---
title: Calculate Lambda function invocation duration
description: Calculate AWS Lambda invocation duration by grouping logs per invocation and subtracting the earliest timestamp from the latest.
url: https://coralogix.com/docs/dataprime/cookbook/lambda_invocation/
product: dataengine
feature: DataPrime Language
feature_id: dataprime-language
feature_identifier: dataengine/dataprime-language
last_updated: '2026-10-30'
---

# 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\_function                     | invocation\_id | start           | end             | duration\_ms |
| ------------------------------------ | -------------- | --------------- | --------------- | ------------ |
| `cll-lambda-nodejs`                  | `<UUID>`       | `1751177380361` | `1751177381308` | `947`        |
| `cll-lambda-nodejs`                  | `<UUID>`       | `1751177269953` | `1751177270800` | `847`        |
| `cll-lambda-nodejs`                  | `<UUID>`       | `1751177740317` | `1751177741183` | `866`        |
| `cll-lambda-python`                  | `<UUID>`       | `1751176967341` | `1751176971141` | `3800`       |
| `cll-lambda-python-2`                | `<UUID>`       | `1751177161241` | `1751177162766` | `1525`       |
| `cll-lambda-python-2`                | `<UUID>`       | `1751177148528` | `1751177150003` | `1475`       |
| `continuousdataflowfunction-faas`    | `<UUID>`       | `1751177722174` | `1751177782589` | `60415`      |
| `databaserelatederrorsfunction-faas` | `<UUID>`       | `1751177242395` | `1751177243685` | `1290`       |
| ...                                  |                |                 |                 |              |

## 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\_function                     | total\_duration\_ms | avg\_duration\_ms | p95\_duration\_ms |
| ------------------------------------ | ------------------- | ----------------- | ----------------- |
| `cll-lambda-nodejs`                  | `34936`             | `919.37`          | `1162`            |
| `cll-lambda-python`                  | `57323`             | `3821.53`         | `4350.5`          |
| `cll-lambda-python-2`                | `727061`            | `31611.35`        | `192047.73`       |
| `continuousdataflowfunction-faas`    | `786910`            | `56207.86`        | `60589.4`         |
| `databaserelatederrorsfunction-faas` | `3700`              | `264.29`          | `306`             |
| ...                                  |                     |                   |                   |

## TL;DR

Group logs by function and invocation, then subtract the earliest timestamp from the latest to get each invocation's duration. Optionally roll up by function name for summaries.
