Stop 14 · Monitoring, Security & Cost OptimizationTopics · 2/10

CloudWatch Logs & Logs Insights

Part of the AWS Roadmap

In short

When a customer says 'payment failed at 7:42 p.m.', logs are how you find out why. CloudWatch Logs collects them from across AWS, and Logs Insights lets you search millions of lines in seconds.

CloudWatch Logs stores logs from Lambda, ECS, EC2 and other sources in log groups. Logs Insights lets you query them with a purpose-built query language to find errors and slow requests quickly.

The jaadu in a station master's cap flips quickly through a thick logbook on a platform desk, holding a magnifying glass over one line.

Think of it like this

The railway station logbook

Every station master at a busy junction like Itarsi writes each train's arrival, delay and platform in a logbook. One logbook per platform, kept for a fixed number of months. When the zonal office asks 'which trains were late last Tuesday?', a clerk with a good index finds them in minutes. Log groups are the logbooks, retention is how long they're kept, and Logs Insights is the clerk with the index.

Watch it happen

How a Logs Insights query runs

Pick log groups
Time range
filter
stats
Results

1/5Choose /aws/lambda/checkout and /ecs/shop-api.

The big ideas

What you need to know

Groups and streams

A log group per app or function, such as /aws/lambda/checkout. Each instance, task or container writes its own stream inside it.

Set retention

New log groups keep logs forever by default. Set retention, such as 30 days, so storage costs don't keep growing.

Log JSON

Structured JSON logs let Logs Insights find fields like orderId and status automatically, which makes queries much easier.

Metric filters

Turn a log pattern like 'PaymentFailed' into a CloudWatch metric, then alarm on it.

See it in code

Walk through the code

text
1fields @timestamp, @message2| filter @message like /ERROR/3| sort @timestamp desc4| limit 205 6# Lambda duration per 5 minutes, from the REPORT lines7filter @type = "REPORT"8| stats avg(@duration) as avgMs,9        max(@duration) as maxMs,10        pct(@duration, 95) as p95Ms11  by bin(5m)

1/3The latest 20 lines containing ERROR, newest first.

Two CloudWatch Logs Insights queries. Run them in the console, or with aws logs start-query from the CLI. In the console, run each query separately.

Save yourself the pain

Common mistakes

Oops

Leaving every log group on 'Never expire'.

Do this instead

Set retention per group, for example 14 days for dev and 90 days for prod, and export to S3 for long-term needs.

Oops

Logging full request bodies with card numbers, phone numbers or passwords.

Do this instead

Mask or drop personal and secret data before logging. Logs are read by many people and kept a long time.

Your mission

Find the slow requests

~25 min

Quick check

You created a Lambda function last year and never touched its logs. How long are they kept?

Go deeper

  1. 1Find the log group for one of your Lambda functions and read recent logs.
  2. 2Write a Logs Insights query that counts errors per 5 minutes.
  3. 3Set a retention period on log groups so old logs do not pile up costs.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge