Once an application runs on AWS, you need to know when something goes wrong, ideally before your users do. Amazon CloudWatch collects metrics and logs from AWS services and your apps, and raises alarms when numbers cross a limit.
Metrics
Metrics are numbers over time. Many arrive automatically: EC2 CPU usage, RDS free storage, Lambda errors and duration, load balancer request counts and 5xx errors. Note that EC2 memory and disk usage are not collected by default; install the CloudWatch agent for those.
Logs
CloudWatch Logs stores text logs in log groups. Lambda functions send logs automatically; EC2 servers and containers send them through the agent or the container’s log driver. Logs Insights lets you search and count, for example:
fields @timestamp, @message
| filter @message like /ERROR/
| sort @timestamp desc
| limit 20
Set a retention period on every log group; by default logs are kept forever, which adds cost.
Alarms
An alarm watches one metric and changes state when it crosses a threshold for a set time, for example CPU above 80% for 10 minutes, or any 5xx errors on the load balancer for 5 minutes. Connect alarms to an SNS topic to receive an email or message. Start with a few meaningful alarms; too many noisy alarms get ignored.
Dashboards
A CloudWatch dashboard puts key graphs on one screen: traffic, errors, latency, CPU and database connections. It is the first place to look during an incident.
A starter setup for a small app
- Alarm on load balancer 5xx errors.
- Alarm on high CPU for the app servers.
- Alarm on low free storage for the database.
- Alarm on Lambda errors, if you use Lambda.
- One dashboard with these metrics, and 14 to 30 days of log retention.
Related: AWS Lambda for beginners, AWS EC2 and AWS RDS.
Hands-on labs like this are part of our AWS and DevOps course in Bangalore.
Book a free demo class at our BTM Layout centre, and ask a counsellor for batch details.
