Logs, Metrics & Alarms

CloudWatch collects logs and metrics and alerts you when something goes wrong.

A deploy is not done when the code is live — it is done when you can see it running well. CloudWatch is AWS's observability service.

Three pillars

  • Logs — application and container logs stream into log groups you can search.
  • Metrics — CPU, memory, request count, and custom app metrics over time.
  • Alarms — thresholds that notify you (via SNS/email/Slack) or trigger auto-rollback and auto-scaling.

Wire your app's stdout to CloudWatch Logs (ECS and Lambda do this automatically) and set alarms on error rate and latency so problems page you before users complain.

Example

Example · bash
# Alarm when the service's 5xx error rate is too high
aws cloudwatch put-metric-alarm \
  --alarm-name myapp-prod-5xx \
  --namespace AWS/ApplicationELB \
  --metric-name HTTPCode_Target_5XX_Count \
  --statistic Sum --period 60 --threshold 10 \
  --comparison-operator GreaterThanThreshold \
  --evaluation-periods 2 \
  --alarm-actions arn:aws:sns:us-east-1:123456789012:oncall

When to use it

  • A team streams ECS container stdout logs to a CloudWatch log group so they can search for errors without SSH-ing into containers.
  • An ops engineer creates a CloudWatch alarm on CPU utilization above 80% that fires an SNS notification to the on-call engineer's phone.
  • A developer queries CloudWatch Logs Insights to count 5xx errors per minute in the 30 minutes after a deploy to verify the release is clean.

More examples

Create a CloudWatch log group

Creates a log group for ECS container logs and sets a 30-day retention policy to control storage costs.

Example · bash
aws logs create-log-group \
  --log-group-name /ecs/myapp

aws logs put-retention-policy \
  --log-group-name /ecs/myapp \
  --retention-in-days 30

Create CPU utilisation alarm

Creates an alarm that notifies the ops SNS topic when ECS service CPU stays above 80% for 3 consecutive minutes.

Example · bash
aws cloudwatch put-metric-alarm \
  --alarm-name myapp-high-cpu \
  --namespace AWS/ECS \
  --metric-name CPUUtilization \
  --dimensions Name=ClusterName,Value=prod Name=ServiceName,Value=myapp \
  --statistic Average \
  --period 60 \
  --threshold 80 \
  --comparison-operator GreaterThanThreshold \
  --evaluation-periods 3 \
  --alarm-actions arn:aws:sns:us-east-1:123456789012:ops-alerts

Query logs with Logs Insights

Runs a Logs Insights query to find the 20 most recent ERROR lines from the past hour for post-deploy verification.

Example · bash
aws logs start-query \
  --log-group-name /ecs/myapp \
  --start-time $(date -d '1 hour ago' +%s) \
  --end-time $(date +%s) \
  --query-string 'fields @timestamp, @message | filter @message like /ERROR/ | sort @timestamp desc | limit 20'

Discussion

  • Be the first to comment on this lesson.
Logs, Metrics & Alarms — AWS Deployment | SoundsCode