CloudWatch Monitoring & Logs
CloudWatch collects metrics, logs and alarms so you can see what your resources are doing and react to problems.
Amazon CloudWatch is the observability service for AWS. It gathers metrics, stores logs, and fires alarms when something crosses a threshold.
Metrics
Most services publish metrics automatically — EC2 CPU utilization, ALB request counts, RDS connections. You can also push your own custom metrics.
Logs
CloudWatch Logs centralizes log output. Install the CloudWatch agent on EC2, or Lambda and many services send logs automatically. Use Logs Insights to query them.
Alarms
An alarm watches a metric and takes an action when breached — send an SNS notification, trigger Auto Scaling, or reboot an instance.
Dashboards
Combine metrics into dashboards for an at-a-glance view of system health.
Example
# Alarm when average CPU exceeds 80% for two consecutive 5-minute periods
aws cloudwatch put-metric-alarm \
--alarm-name high-cpu-web-01 \
--namespace AWS/EC2 --metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-0123456789abcdef0 \
--statistic Average --period 300 --evaluation-periods 2 \
--threshold 80 --comparison-operator GreaterThanThreshold \
--alarm-actions arn:aws:sns:us-east-1:123456789012:ops-alertsWhen to use it
- A production API publishes custom latency metrics to CloudWatch and triggers a PagerDuty alert when p99 latency exceeds 500ms for 5 minutes.
- A Lambda function emits structured JSON logs to CloudWatch Logs Insights, allowing engineers to query for error patterns across millions of log events.
- An operations team creates a CloudWatch dashboard with EC2 CPU, RDS connections, and ALB request count widgets to monitor the system at a glance.
More examples
Put a Custom Metric
Publishes a custom latency metric to a custom namespace, enabling application-specific monitoring beyond built-in AWS metrics.
aws cloudwatch put-metric-data \
--namespace MyApp/API \
--metric-name RequestLatency \
--value 145 \
--unit Milliseconds \
--dimensions Method=POST,Endpoint=/checkoutCreate a CPU Alarm on EC2
Creates an alarm that fires an SNS notification when EC2 CPU stays above 80% for two consecutive 5-minute periods.
aws cloudwatch put-metric-alarm \
--alarm-name high-cpu-alarm \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--dimensions Name=InstanceId,Value=i-0abc123 \
--statistic Average \
--period 300 \
--threshold 80 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:ops-alertsQuery Logs with Logs Insights
Runs a CloudWatch Logs Insights query to find the 20 most recent error log lines from a Lambda function in the last hour.
aws logs start-query \
--log-group-name /aws/lambda/my-function \
--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