CloudWatch — Monitoring & Logs
Metrics, logs, alarms, and dashboards — know what your infrastructure is doing, be notified when it isn't
Skills taught: aws.cloudwatch, aws.cloudwatch-logs, aws.cloudwatch-alarms, aws.cloudwatch-metrics, aws.cloudwatch-dashboards
CloudWatch — AWS's observability platform
CloudWatch is the central observability service for everything in AWS. It collects metrics (CPU utilization, request count, error rate), logs (application output, access logs, Lambda output), and events (resource state changes). Without CloudWatch, running production AWS infrastructure is flying blind — you won't know that your EC2 instance's CPU is at 100%, your Lambda is throwing errors, or your RDS disk is 90% full.
Logs — viewing and querying
# Stream logs in real time (like 'tail -f')
aws logs tail /aws/lambda/my-function --follow
aws logs tail /ecs/myapp --follow
aws logs tail /var/log/nginx/access.log --follow
# Get logs from a specific time range
aws logs filter-log-events \
--log-group-name /aws/lambda/my-function \
--start-time $(date -d '1 hour ago' +%s)000 \
--filter-pattern ERROR
# CloudWatch Insights — SQL-like query language for logs
# Use in console: CloudWatch → Log Insights
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 50
'
Metrics and alarms
# Alarm: notify when Lambda error rate exceeds 1% over 5 minutes
aws cloudwatch put-metric-alarm \
--alarm-name 'Lambda-High-Error-Rate' \
--alarm-description 'Lambda function error rate > 1%' \
--namespace AWS/Lambda \
--metric-name Errors \
--dimensions Name=FunctionName,Value=my-function \
--statistic Sum \
--period 300 \
--threshold 5 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--alarm-actions 'arn:aws:sns:ap-south-1:123456789:my-alerts-topic' \
--ok-actions 'arn:aws:sns:ap-south-1:123456789:my-alerts-topic'
# Common alarms to set up for every production system:
# EC2: CPUUtilization > 80% for 10 min
# RDS: CPUUtilization > 70%, FreeStorageSpace < 10GB
# ALB: HTTPCode_Target_5XX_Count > 10 per 5 min
# Lambda: Errors > threshold, Duration approaching timeout
# SQS: ApproximateNumberOfMessagesVisible > 1000 (queue backup)
# Create SNS topic for alerts
aws sns create-topic --name my-alerts-topic
aws sns subscribe \
--topic-arn arn:aws:sns:ap-south-1:123456789:my-alerts-topic \
--protocol email \
--notification-endpoint alerts@mycompany.com
Custom metrics from your application
import { CloudWatchClient, PutMetricDataCommand } from '@aws-sdk/client-cloudwatch';
const cloudwatch = new CloudWatchClient({ region: 'ap-south-1' });
async function recordMetric(name: string, value: number, unit: string) {
await cloudwatch.send(new PutMetricDataCommand({
Namespace: 'MyApp', // groups your metrics
MetricData: [{
MetricName: name,
Value: value,
Unit: unit,
Dimensions: [
{ Name: 'Environment', Value: process.env.NODE_ENV ?? 'production' },
{ Name: 'Service', Value: 'api' },
],
Timestamp: new Date(),
}],
}));
}
// Usage in your Express app:
app.post('/checkout', async (req, res) => {
const start = Date.now();
try {
await processOrder(req.body);
await recordMetric('OrdersCompleted', 1, 'Count');
await recordMetric('CheckoutLatency', Date.now() - start, 'Milliseconds');
res.json({ success: true });
} catch (err) {
await recordMetric('OrdersFailed', 1, 'Count');
throw err;
}
});
// Then create alarms on 'MyApp/OrdersFailed' metric
// Dashboard showing 'MyApp/OrdersCompleted' rate
Dashboards
CloudWatch Dashboards let you build a single pane of glass for all your infrastructure metrics. Create widgets for graphs (metric over time), number cards (current value), alarm status indicators, and log query results. Share dashboards with your team. Add cross-account widgets to see multiple AWS accounts in one view. Most teams create one dashboard per environment (production, staging) and one per service.
Tip: Set alarms BEFORE you go to production, not after an incident. The first time most teams discover they needed a CPUUtilization alarm is during an outage — when the server has been at 100% CPU for 20 minutes and nobody noticed. Set up basic alarms (CPU, memory, error rates, disk space) when you first deploy. SNS + email or Slack is free. CloudWatch alarms cost $0.10/alarm/month — a 10-alarm setup for a small app costs $1/month.
Frequently Asked Questions
How much does CloudWatch cost?
Logs: $0.50/GB ingested, $0.03/GB stored/month. Metrics: first 10 custom metrics free, then $0.30/metric/month. Alarms: $0.10/alarm/month. Insights queries: $0.005/GB scanned. For a typical small production app: ~$5-20/month total. This is non-negotiable — the cost of not having visibility into production failures is orders of magnitude higher.