# bin() in CloudWatch Logs Insights

Logs Insights queries · Updated 16 November 2026

**Short answer:** bin(period) rounds each event's @timestamp down to the start of a time bucket, so stats count(*) by bin(5m) produces one row per five-minute window. Queries that aggregate by bin() can be shown as a line or bar chart on the Visualization tab and added to a CloudWatch dashboard.

## The query

```
filter @type = "REPORT"
| stats count(*) as invocations,
        avg(@duration) as avgMs,
        pct(@duration, 95) as p95Ms
  by bin(1m)
```

Pointed at a Lambda function's log group, this gives invocations, average duration and p95 duration per minute: three lines on one chart, all from logs, with no custom metrics needed.

## How it works

1. `filter @type = "REPORT"` keeps one line per Lambda invocation, the platform summary line, so counts and durations aren't distorted by application logging.
2. `stats … by bin(1m)` assigns each event to the one-minute bucket its timestamp falls into, then evaluates every aggregate per bucket.
3. Each aggregate becomes a column, and on the Visualization tab, a series. Name them with `as` so the legend is readable.

Buckets with no events produce no row, which shows up as a gap or a straight line between points rather than a zero. Keep that in mind when reading quiet periods.

## Choosing a bin size

| Time range | Sensible bin |
|---|---|
| 15–60 minutes | `bin(30s)` or `bin(1m)` |
| A few hours | `bin(5m)` |
| A day | `bin(15m)` to `bin(1h)` |
| A week or more | `bin(1h)` to `bin(1d)` |

Units include `s`, `m`, `h` and `d`. Aim for somewhere between about 50 and 300 points across the range: fewer hides short spikes, more makes the line jagged and the table long. When you compare two incidents, use the same bin for both, otherwise a spike that looks twice as tall may just be a bucket twice as wide.

## Variations

**Errors over time** from application logs:

```
filter level = "ERROR"
| stats count(*) as errors by bin(5m)
```

**Daily totals** for a weekly report:

```
stats sum(bytes) / 1024 / 1024 / 1024 as GiB by bin(1d)
```

**Several series from one field.** Grouping by a bin and another field returns one row per bucket and value, which reads fine as a table. For a clean multi-line chart, give each series its own aggregate column instead, for example by counting matches with `strcontains`:

```
stats sum(strcontains(@message, "WARN")) as warns,
      sum(strcontains(@message, "ERROR")) as errors
  by bin(5m)
```

## From query to dashboard

On the Visualization tab, choose line, stacked area or bar, then **Add to dashboard**. The widget re-runs the query whenever the dashboard loads or refreshes, and each run is billed for the data scanned, so a widget over many large log groups on an auto-refreshing dashboard can add up. For something you'll watch constantly or alarm on, publish a metric with a metric filter or embedded metric format and chart that instead; keep `bin()` queries for investigation and occasional reporting.

## Questions

### What does bin() do in CloudWatch Logs Insights?

It rounds @timestamp down to the start of a bucket of the size you give, such as 5m, so stats by bin(5m) aggregates per five-minute window.

### Why doesn't my Logs Insights query show a chart?

Visualisation needs an aggregation with stats, usually grouped by bin(). Plain fields or filter queries only produce a table.

### Can I use bin with other group-by fields?

Yes, stats count(*) by bin(5m), status works and returns one row per bucket and status. For a cleaner chart, compute separate aggregate columns instead.

## When this happens away from your desk

A time series from logs is what you want to glance at when an alarm goes off. CloudConsole, a phone app for AWS, is built to show alarms and the related logs on your phone.

[About the CloudConsole app](https://cloudconsole.dev/aws-console-app) · [Download on the App Store](https://apps.apple.com/app/cloudconsole-for-aws/id6835419710)

## Sources

- [CloudWatch Logs Insights query syntax](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html)
- [Visualize log data in graphs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_Insights-Visualizing-Log-Data.html)

## Related

- [Count errors over time with CloudWatch Logs Insights](https://cloudconsole.dev/logs-insights/count-errors-over-time)
- [Group by in CloudWatch Logs Insights](https://cloudconsole.dev/logs-insights/group-by)
- [Percentile latency with CloudWatch Logs Insights](https://cloudconsole.dev/logs-insights/percentile-latency)
- [Find the slowest Lambda invocations with Logs Insights](https://cloudconsole.dev/logs-insights/lambda-slowest-invocations)
- [CloudWatch Logs Insights syntax cheat sheet](https://cloudconsole.dev/logs-insights/syntax-cheat-sheet)
- [API Gateway 5xx errors with Logs Insights](https://cloudconsole.dev/logs-insights/api-gateway-5xx-errors)

---

Source: https://cloudconsole.dev/logs-insights/bin-time-series

CloudConsole is an independent iPhone and Android app for Amazon Web Services by Cloudconcur Ltd. It is not affiliated with Amazon Web Services.
