CloudWatch Metrics, Logs and Alarms

Collect metrics and logs, query them with Logs Insights, build dashboards and alert on symptoms that matter to users.

Intermediate · about 5 hours · difficulty 2/5

Learn first: AWS Lambda Functions, EC2 Instances, AMIs and Pricing Options

Amazon CloudWatch is the default monitoring service on AWS. Azure Monitor and Google Cloud Monitoring/Logging are the equivalents.

Metrics

  • AWS services publish metrics automatically (EC2 CPU, ALB request count and 5xx errors, Lambda duration and errors, RDS connections).
  • EC2 memory and disk need the CloudWatch agent.
  • Publish custom metrics (orders placed, payment failures) or use the Embedded Metric Format in logs.
  • Understand statistics (average, p95, p99) and periods. Averages hide slow requests; watch percentiles.

Logs

  • Log groups and log streams collect logs from Lambda, ECS, API Gateway and the agent.
  • Set a retention period; the default is to keep logs forever and pay for them forever.
  • Logs Insights queries logs with a simple query language: filter errors, count by status code, find slow requests.
  • Metric filters turn log patterns into metrics you can alarm on.

Alarms and dashboards

  • Alarms watch a metric and notify SNS (email, Slack via Chatbot) or trigger actions like Auto Scaling.
  • Alert on symptoms users feel (error rate, latency) rather than every CPU spike.
  • Composite alarms reduce noise; anomaly detection learns normal patterns.
  • Dashboards give one view of a service's health.

Pitfalls

  • Never-expiring log groups quietly growing your bill.
  • Alert fatigue from dozens of noisy alarms.
  • Logging secrets or personal data such as Aadhaar or phone numbers.

You are ready to move on when you can build a dashboard for your API, write a Logs Insights query that finds the slowest requests, and get an email when the error rate crosses a threshold.

Key concepts

  • Built-in service metrics
  • CloudWatch agent
  • Custom metrics and EMF
  • Percentiles vs averages
  • Log groups and retention
  • Logs Insights queries
  • Alarms and SNS notifications
  • Dashboards
  • Composite and anomaly alarms

Mini project

For your serverless notes API, set 7-day log retention, build a dashboard (requests, errors, p95 latency), write two Logs Insights queries, and create an alarm that emails you via SNS when Lambda errors exceed 5 in 5 minutes. Trigger it deliberately. A few alarms and a dashboard fit the free tier; delete them when done.

Check yourself

Why might average latency look fine while users complain the app is slow? Which statistic would you alarm on?

Free resources