CloudWatch Contributor Insights lets you define a rule over log groups instead of running an ad-hoc Logs Insights query. What does such a rule actually produce, and when would you build one rather than just querying?
answer
- rule watches events as ingested
- key defines the contributor ranked
- filters decide what counts
- no backfill before rule creation
- high cardinality is the sweet spot
basics
~20 sA Contributor Insights rule evaluates log events as they arrive and produces a continuously updated ranking of top contributors by a key you choose, plus graphable metrics you can alarm on — unlike a query, which reads history once.
solid answer
~60 sA rule is a small JSON definition: which log groups it watches, whether they are JSON or Common Log Format, which field or fields form the contributor key, an optional value to sum, and filters that decide which events count. CloudWatch matches it against events **as they are ingested** and maintains a top-N ranking over time, along with metrics such as the number of unique contributors and the value of the top one, which can be graphed and alarmed on. You reach for a rule when the question is recurring and shaped as "who is the biggest talker" — which tenant is hammering the API, which DynamoDB partition key is hot, which client IP is generating the 429s — and you want the answer standing by rather than reconstructed from a fresh scan every time. You reach for a Logs Insights query instead when the question is one-off or exploratory, or when it is about the past: a rule has no backfill, so it can only tell you about events ingested since you created it.
code
json · 12 lines{
"Schema": { "Name": "CloudWatchLogRule", "Version": 1 },
"LogGroupNames": ["/aws/lambda/api"],
"LogFormat": "JSON",
"AggregateOn": "Count",
"Contribution": {
"Keys": ["$.tenantId"],
"Filters": [
{ "Match": "$.status", "GreaterThan": 399 }
]
}
}go deeper
Know that Contributor Insights answers who the top talkers are — by tenant, IP or key — continuously, whereas a Logs Insights query reads stored history once.
Describe the parts of a rule: log groups, log format, contributor keys, filters, and counting versus summing a value. Know that JSON keys are selected by path.
Argue the choice honestly — query for exploration and history, rule for a recurring high-cardinality ranking you want to alarm on — and name the no-backfill limitation as the decider.
Treat rules as a small standing budget: each costs per matched event, so decide which recurring attribution questions deserve a permanent rule and which stay ad-hoc, and keep the set from sprawling.
## The shape of a rule A CloudWatch Contributor Insights rule for log groups is a JSON document. The essential parts are the log groups it watches, the log format, and a `Contribution` block naming the key: ```json { "Schema": { "Name": "CloudWatchLogRule", "Version": 1 }, "LogGroupNames": ["/aws/lambda/api"], "LogFormat": "JSON", "AggregateOn": "Count", "Contribution": { "Keys": ["$.tenantId"], "Filters": [ { "Match": "$.status", "GreaterThan": 399 } ] } } ``` - **`Keys`** define the contributor — the thing being ranked. A small number of keys may be combined, producing a composite contributor such as tenant plus endpoint. - **`Filters`** decide which events participate at all, so a rule can count only errors, only one route, only requests over a latency threshold. - **`AggregateOn`** chooses between counting matching events and summing a numeric field named by `ValueOf` — the difference between "who made the most calls" and "who consumed the most capacity units". - **`LogFormat`** is JSON or Common Log Format; JSON keys are addressed with JSON-path style selectors. AWS also publishes built-in rules for some services, so common cases (for example, top contributors in VPC flow logs) do not have to be authored by hand. ## What it produces Unlike a query, which reads stored history once and returns a table, a rule is **continuous**. CloudWatch evaluates it against log events as they are ingested and maintains a time series of the top contributors, which the console renders as the familiar stacked "top N" graph. Alongside the ranking it exposes aggregate measures — how many unique contributors there were in a period, and the value attributed to the leading one — and those are ordinary CloudWatch measures, so they can be graphed on a dashboard and alarmed on. That is the real payoff: "alert me when one tenant exceeds a third of all requests" is expressible with a rule and is not expressible with an ad-hoc query. ## The decision: rule or query Prefer a **rule** when all of these hold: the question recurs; it is shaped as a ranking over a high-cardinality key; and you want an answer available immediately rather than after a scan. Canonical cases are the hot partition key in a DynamoDB workload, the client IP or API key behind a throttling spike, the noisiest source of 5xx responses, and the tenant driving a cost anomaly. High cardinality is precisely where a rule beats the alternatives: you cannot put a tenant id in a metric dimension without a cardinality explosion, but a rule ranks tenants happily because it keeps only the top contributors rather than a series per tenant. Prefer a **query** when the question is exploratory or one-off, when you need the raw log lines rather than a ranking, or when the window of interest is in the past. This is the limitation that decides most arguments: a rule only sees events ingested after it exists. It cannot answer "who caused last Tuesday's spike" — for that, Logs Insights scanning history is the only option. The mature pattern is therefore sequential: use Logs Insights to work out during the first incident which key mattered, then create a rule so the second incident answers itself. ## Costs and limits worth knowing Rules are priced per rule and by the volume of matching log events, so a rule with no filters over a firehose of debug logs is a standing charge you did not intend. Filter narrowly and point rules at specific log groups. Rules can also be disabled rather than deleted, which is the right move for something you only want during a campaign. There is a per-account limit on how many rules may exist, which is a mild forcing function towards keeping the ones that earn their place. ## The trap The misconception to avoid in an interview is treating Contributor Insights as "a saved Logs Insights query". It is not: a saved query is text you re-run, still scanning history and still billed per scan; a rule is a stream processor over ingestion that produces measures. They answer different questions, and the honest answer to "which should we use" is usually "the query first, the rule once we know what to watch".
- Why not just put the contributor key in a CloudWatch metric dimension instead?Because dimensions are cardinality-bounded: a tenant id or client IP would create a separate metric per value, which is expensive and unusable. A Contributor Insights rule keeps only the top contributors and the aggregate shape of the distribution, which is exactly the trade you want for a high-cardinality key you only ever look at as a ranking.
- Can a rule tell you who caused a spike that happened last week?Only if the rule already existed then. Rules evaluate events at ingestion and do not backfill, so a rule created today knows nothing about last week. For historical attribution you have to run a Logs Insights query over the stored logs — which is the usual sequence: query to discover the key that matters, then create a rule so the next occurrence is answered live.
- How does a rule end up driving an alert?A rule exposes measures such as the unique-contributor count and the top contributor's value, and those behave like other CloudWatch measures, so a dashboard can graph them and an alarm can watch them. That is how you get "page me when one client exceeds a share of total traffic", which no ad-hoc query can do.
saying these in an interview costs you the question
- Calls it just a saved Logs Insights query
- Expects a new rule to show historical contributors
- Puts the contributor key in a metric dimension instead
- Creates an unfiltered rule over a high-volume log group
- Thinks it returns raw log lines rather than a ranking