Skip to content
VirtualLP Virtual Learning Platform

Observability with CloudWatch: Metrics, Logs, Alarms and Insights

13 min read Topic 21 of 30 1 hands-on lab

You cannot fix what you cannot see. Amazon CloudWatch collects metrics, logs and events from AWS resources and your applications, raises alarms and shows dashboards.

flowchart LR
    S["AWS services<br/>EC2 · Lambda · API GW · DynamoDB · Bedrock"] -->|metrics| M[(CloudWatch Metrics)]
    APP[Your code: console.log / logger] -->|logs| L[(CloudWatch Logs)]
    L -->|metric filter| M
    M --> A{Alarm<br/>Errors > 5 for 5 min}
    A -->|ALARM| SNS[SNS → e-mail / Slack / pager]
    A -->|action| ASG[Auto Scaling]
    M --> D[Dashboard]
    L --> Q[Logs Insights queries]

Metrics

Concept Example
Namespace AWS/Lambda, AWS/EC2, AWS/ApiGateway, custom Shop/Orders
Metric Invocations, Errors, Duration, CPUUtilization
Dimension FunctionName=products-api
Statistic Sum, Average, Minimum, Maximum, p90/p99
Period 1 minute, 5 minutes…

Publish your own business metrics with PutMetricData or the Embedded Metric Format in logs.

Alarms

An alarm watches one metric (or a math expression) and changes state OK → ALARM → INSUFFICIENT_DATA.

  • Threshold: e.g. Errors >= 5 for 3 of 3 datapoints of 1 minute.
  • Actions: notify an SNS topic, scale an ASG, stop/reboot an instance, open an incident.
  • Composite alarms combine alarms to reduce noise.

Logs

Concept Meaning
Log group E.g. /aws/lambda/products-api; set retention (logs never expire by default – cost!)
Log stream Sequence from one source, e.g. one Lambda execution environment
Metric filter Turn log patterns (ERROR) into metrics you can alarm on
Subscription filter Stream logs to Lambda, Kinesis or OpenSearch
Logs Insights Query language for searching and aggregating
fields @timestamp, @message
| filter @message like /ERROR/
| sort @timestamp desc
| limit 20
filter @type = "REPORT"
| stats avg(@duration), max(@duration), count(*) by bin(5m)

Lambda’s REPORT line

REPORT RequestId: … Duration: 12.4 ms Billed Duration: 13 ms Memory Size: 128 MB Max Memory Used: 71 MB Init Duration: 160.2 ms – duration, cost and cold starts in one line.

Beyond metrics and logs

  • AWS X-Ray / CloudWatch Application Signals – distributed traces across API Gateway, Lambda and DynamoDB to find slow calls.
  • Synthetics canaries – scripted checks of your endpoints.
  • Container Insights, Lambda Insights – deeper runtime metrics.
Key point

Alarm on symptoms users feel (errors, latency p99, throttles), send alarms to SNS, and always set a log retention period.

Practise it

Virtual lab · Amazon Web Services · 30 min

Build, test and debug your first Lambda function

Write a Node.js Lambda function that really runs: test it with JSON events, configure environment variables, hit a real timeout, fix it and read the logs in CloudWatch.

Open lab

Quick revision

  1. Metrics = namespace + name + dimensions; use percentiles for latency.
  2. Alarms → SNS / scaling actions.
  3. Logs: groups, streams, retention, metric filters.
  4. Logs Insights: fields, filter, stats, sort, limit.
  5. X-Ray traces requests across services.

Practise this topic

Hands-on virtual lab

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.