AWS — CloudWatch
What is CloudWatch?
Monitoring and observability service for AWS resources.
Metrics
# Get metrics
aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--start-time 2024-01-01T00:00:00Z \
--end-time 2024-01-01T23:59:59Z \
--period 3600 \
--statistics Average \
--dimensions Name=InstanceId,Value=i-1234567890abcdef0
Alarms
aws cloudwatch put-metric-alarm \
--alarm-name high-cpu \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 80 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789:my-topic
Logs
# Create log group
aws logs create-log-group --log-group-name /my/app
# Create log stream
aws logs create-log-stream \
--log-group-name /my/app \
--log-stream-name my-stream
# Put log events
aws logs put-log-events \
--log-group-name /my/app \
--log-stream-name my-stream \
--log-events timestamp=1234567890,message="Hello"
Dashboards
aws cloudwatch put-dashboard \
--dashboard-name MyDashboard \
--dashboard-body '{"widgets":[{"type":"metric","x":0,"y":0,"width":12,"height":6,"properties":{"metrics":[["AWS/EC2","CPUUtilization","InstanceId","i-1234567890abcdef0"]]}}]}'
Custom metrics
import boto3
cloudwatch = boto3.client('cloudwatch')
cloudwatch.put_metric_data(
Namespace='MyApp',
MetricData=[{
'MetricName': 'Requests',
'Value': 100,
'Unit': 'Count'
}]
)
Best practices
- Set up alarms
- Use dashboards
- Enable detailed monitoring
- Set log retention
Mini Practice
- Create an alarm
- View metrics
- Set up logging
- Create a dashboard
Up Next
Continue with CloudFormation - Infrastructure as Code.
Related Topics
Frequently Asked Questions about CloudWatch
What is CloudWatch in AWS?
CloudWatch is a fundamental concept in AWS. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn CloudWatch?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn CloudWatch.
Why is CloudWatch important in AWS?
CloudWatch is essential for AWS development. Understanding this concept will help you write better code and solve real-world problems more effectively.