CloudWatch Core Components
| Component | Role |
|---|---|
| Metrics | Time-series numeric data (CPU, memory, etc.) |
| Alarms | Notifications and actions when a metric exceeds a threshold |
| Logs | Collect log groups and streams |
| Logs Insights | Log query language |
| Dashboards | Visualize metrics and logs |
| Events/EventBridge | Event-driven automation |
Querying Metrics
Bash
# EC2 CPU ( 1when, 5 )
aws cloudwatch get-metric-statistics --namespace AWS/EC2 --metric-name CPUUtilization --dimensions Name=InstanceId,Value=i-xxxxxxxxxxxxxxxxx --start-time $(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ) --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) --period 300 --statistics Average --query 'Datapoints[*].[Timestamp,Average]' --output table
# list
aws cloudwatch list-metrics --namespace AWS/EC2
aws cloudwatch list-metrics --namespace AWS/RDS
aws cloudwatch list-metrics --namespace AWS/LambdaPublishing Custom Metrics
Bash
# CLI
aws cloudwatch put-metric-data --namespace "MyApp" --metric-data '[{
"MetricName": "ActiveUsers",
"Value": 150,
"Unit": "Count",
"Dimensions": [
{"Name": "Environment", "Value": "production"}
]
}]'
# Python
import boto3
cw = boto3.client('cloudwatch')
cw.put_metric_data(
Namespace='MyApp',
MetricData=[{
'MetricName': 'OrderCount',
'Value': 42,
'Unit': 'Count',
'Dimensions': [
{'Name': 'Region', 'Value': 'ap-northeast-2'}
]
}]
)Configuring Alarms
Bash
# CPU 80% 5 when SNS
aws cloudwatch put-metric-alarm --alarm-name high-cpu-prod --alarm-description "CPU over 80%" --namespace AWS/EC2 --metric-name CPUUtilization --dimensions Name=InstanceId,Value=i-xxxxxxxxx --statistic Average --period 300 --evaluation-periods 2 --threshold 80 --comparison-operator GreaterThanOrEqualToThreshold --alarm-actions arn:aws:sns:ap-northeast-2:ACCOUNT:alerts --ok-actions arn:aws:sns:ap-northeast-2:ACCOUNT:alerts
# list
aws cloudwatch describe-alarms --query 'MetricAlarms[*].[AlarmName,StateValue,MetricName]' --output table
#
aws cloudwatch set-alarm-state --alarm-name high-cpu-prod --state-value ALARM --state-reason "Testing"CloudWatch Agent (EC2 Memory and Disk)
EC2's default metrics do not include memory or disk usage, so you need to install the Agent.
Bash
# Agent (Ubuntu)
wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
sudo dpkg -i amazon-cloudwatch-agent.deb
# settings run
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
# or settings file
sudo tee /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json << 'EOF'
{
"metrics": {
"metrics_collected": {
"mem": {
"measurement": ["mem_used_percent"],
"metrics_collection_interval": 60
},
"disk": {
"measurement": ["disk_used_percent"],
"metrics_collection_interval": 60,
"resources": ["/", "/data"]
}
}
}
}
EOF
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -sLogs Insights Queries
Bash
# CLI run
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/ | limit 20'Queries commonly used in the console:
CODE
# Lambda
fields @timestamp, @message
| filter @message like /ERROR/
| stats count() by bin(5m)
# Lambda run when Top 10
fields @timestamp, @duration
| sort @duration desc
| limit 10
# IP Request (ALB )
fields @timestamp, clientip, request
| filter clientip = "203.0.113.5"
| stats count() by requestCreating a Dashboard
Bash
aws cloudwatch put-dashboard --dashboard-name production --dashboard-body '{
"widgets": [{
"type": "metric",
"properties": {
"title": "EC2 CPU",
"metrics": [["AWS/EC2","CPUUtilization","InstanceId","i-xxxxxxxx"]],
"period": 300,
"stat": "Average",
"view": "timeSeries"
}
}]
}'Editorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)AWS documentation ↗
Questions & answers
Ask a question about this guide. We'll review and reply.