Amazon CloudWatch is an AWS service for metrics, events, logs, and alarms. It helps you check whether a resource ran, investigate what happened, and respond when a monitored metric crosses a threshold. Managed services still need monitoring: an RDS database, for example, can cause application performance problems if it runs short of resources. section 13.1 section 10.7
Start with the signal that answers your operational question:
- For a Lambda function, open its Monitor tab to see invocations, then select View Logs in CloudWatch. The automatically created log group collects messages from multiple log streams; Search Log Group searches across them. Logs can take a few minutes to appear. section 6.2.2
- For an RDS database, inspect its CloudWatch metrics in the Management Console and create alarms for thresholds that matter. Examples include low FreeStorageSpace, high CPUUtilization, and low FreeableMemory. section 10.7
- For an EC2 instance, an alarm can do more than notify: it can initiate recovery when the system status check indicates an underlying hardware failure. section 13.1
Check a website with Lambda
An EventBridge rule triggers a Lambda function every five minutes. The function requests a website and checks its response. Lambda publishes metrics to CloudWatch by default; an alarm monitors failed checks and notifies you by email when the site is unavailable. To verify that the check actually ran, inspect its invocations and search its CloudWatch logs for a success message. section 6.2 section 6.2.2
Listing 13.2 Creating a CloudWatch alarm to recover a failed EC2 instance
RecoveryAlarm: #1
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmDescription: 'Recover EC2 instance when underlying hardware fails.'
Namespace: 'AWS/EC2' #2
MetricName: 'StatusCheckFailed_System' #3
Statistic: Maximum #4
Period: 60 #5
EvaluationPeriods: 5 #6
ComparisonOperator: GreaterThanThreshold #7
Threshold: 0 #8
AlarmActions: #9
- !Sub 'arn:aws:automate:${AWS::Region}:ec2:recover'
Dimensions:
- Name: InstanceId #10
Value: !Ref Instance
The CloudFormation alarm sets the AWS/EC2 namespace, StatusCheckFailed_System metric, instance ID, threshold, evaluation period, and EC2 recovery action. Use it as a concrete configuration example for automatic recovery. section 13.1
Choose the response to match the failure. A website check can alert someone, while an EC2 recovery alarm can act automatically when brief downtime is acceptable. Neither removes the need to select useful metrics and thresholds. CloudWatch also does not make an application fault-tolerant: recovery may still involve a short outage. section 6.2 section 13.1
