AWS EC2 Monitoring


https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/viewing_metrics_with_cloudwatch.html
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html
https://docs.aws.amazon.com/ebs/latest/userguide/using_cloudwatch_ebs.html
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-common-scenarios.html

1. Important Points#

EC2 monitoring 要分清 AWS 原生指标和 OS 指标:

AWS/EC2 default metrics:
    status check
    CPU
    network
    EBS IO at instance level

CloudWatch Agent metrics:
    memory
    filesystem
    swap
    process / agent status if configured

AWS/EBS metrics:
    volume queue
    volume throughput / IOPS
    burst balance for burstable volumes

EC2 监控只覆盖 VM / OS / attached storage / network capacity。应用进程、数据库、中间件和业务服务应该放到对应组件自己的 monitoring page。

2. CloudWatch Metrics And Alarms#

Severity Alert Metric Namespace Statistic Period Definition Evaluation Alarm After Why Monitor
P0 EC2 status check failed StatusCheckFailed AWS/EC2 Maximum 60s > 0 2/2 datapoints ~2m AWS 层面判断实例或底层系统不健康
P1 Instance status check failed StatusCheckFailed_Instance AWS/EC2 Maximum 60s > 0 2/2 datapoints ~2m OS / network config / boot problem,通常需要实例内排查
P1 System status check failed StatusCheckFailed_System AWS/EC2 Maximum 60s > 0 2/2 datapoints ~2m AWS 底层宿主机或平台侧异常,可能需要 stop/start 或 auto recovery
P1 Disk almost full disk_used_percent CWAgent Average 60s >= 90% 10/10 datapoints ~10m 磁盘满会导致服务写入失败、日志丢失和系统异常
P1 Memory almost full mem_used_percent CWAgent Average 60s >= 90% 10/10 datapoints ~10m 防止 OOM、swap 抖动和进程被杀
P2 CPU high CPUUtilization AWS/EC2 Average 60s >= 80% 15/15 datapoints ~15m 发现容量不足、异常进程或负载升高
P2 CPU credit low CPUCreditBalance AWS/EC2 Average 300s < 20 6/6 datapoints ~30m T-family 实例 CPU credit 耗尽后会降速
P2 CPU credit charged CPUSurplusCreditsCharged AWS/EC2 Sum 300s > 0 1/1 datapoint ~5m T Unlimited 开始产生额外 CPU credit 费用
P2 EBS queue high VolumeQueueLength AWS/EBS Average 60s >= 10 10/10 datapoints ~10m EBS IO 排队会推高应用延迟
P2 EBS burst balance low BurstBalance AWS/EBS Average 60s < 20% 15/15 datapoints ~15m gp2/st1/sc1 等突发型卷积分耗尽会降低 IO 性能
P2 Network throughput high NetworkIn / NetworkOut AWS/EC2 Sum 300s env-specific baseline 3/3 datapoints ~15m 发现异常流量、备份/同步任务或可能接近网络瓶颈
P2 CloudWatch Agent stopped procstat or heartbeat metric CWAgent Maximum 60s env-specific 5/5 datapoints ~5m agent 停止会导致 memory/disk 指标缺失

Evaluation rule:

Period:
    每个 datapoint 的统计窗口,不是告警持续时间。

Evaluation:
    M/N datapoints means datapoints_to_alarm / evaluation_periods.
    例如 10/10 datapoints with 60s period means 10 consecutive 60s datapoints.

Alarm After:
    roughly Period * EvaluationPeriods.
    例如 10 * 60s = about 10m.

status check:
    P0/P1 用 2/2 datapoints,快速确认但避免单点抖动。

resource saturation:
    CPU / memory / disk / EBS queue 默认连续 datapoints,避免一次采样误报。

5m Sum metric:
    cost / network / discrete count 用 300s period,减少噪音。

env-specific:
    network throughput and agent heartbeat must be based on instance type, workload baseline, and collection method.

3. CloudWatch Agent#

AWS/EC2 默认没有 memory 和 filesystem 指标。生产 EC2 应该安装 CloudWatch Agent 或其他统一采集器。

Minimal CloudWatch Agent config:

{
  "metrics": {
    "namespace": "CWAgent",
    "append_dimensions": {
      "InstanceId": "${aws:InstanceId}",
      "InstanceType": "${aws:InstanceType}",
      "AutoScalingGroupName": "${aws:AutoScalingGroupName}"
    },
    "metrics_collected": {
      "mem": {
        "measurement": [
          "mem_used_percent"
        ],
        "metrics_collection_interval": 60
      },
      "disk": {
        "measurement": [
          "used_percent"
        ],
        "metrics_collection_interval": 60,
        "resources": [
          "/"
        ],
        "ignore_file_system_types": [
          "sysfs",
          "devtmpfs",
          "tmpfs",
          "overlay"
        ]
      },
      "swap": {
        "measurement": [
          "swap_used_percent"
        ],
        "metrics_collection_interval": 60
      }
    }
  }
}

Install / start:

sudo yum install -y amazon-cloudwatch-agent

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 \
  -s

Verify:

aws cloudwatch list-metrics \
  --namespace CWAgent \
  --dimensions Name=InstanceId,Value=i-0123456789abcdef0 \
  --region ap-east-1

4. Alarm Examples#

Status check alarm:

aws cloudwatch put-metric-alarm \
  --alarm-name "P0-EC2-i-0123456789abcdef0-status-check-failed" \
  --namespace AWS/EC2 \
  --metric-name StatusCheckFailed \
  --dimensions Name=InstanceId,Value=i-0123456789abcdef0 \
  --statistic Maximum \
  --period 60 \
  --evaluation-periods 2 \
  --datapoints-to-alarm 2 \
  --threshold 0 \
  --comparison-operator GreaterThanThreshold \
  --treat-missing-data missing \
  --alarm-actions arn:aws:sns:ap-east-1:111122223333:prod-critical-alerts \
  --region ap-east-1

Disk usage alarm:

aws cloudwatch put-metric-alarm \
  --alarm-name "P1-EC2-i-0123456789abcdef0-root-disk-used-high" \
  --namespace CWAgent \
  --metric-name disk_used_percent \
  --dimensions \
      Name=InstanceId,Value=i-0123456789abcdef0 \
      Name=path,Value=/ \
      Name=fstype,Value=xfs \
  --statistic Average \
  --period 60 \
  --evaluation-periods 10 \
  --datapoints-to-alarm 10 \
  --threshold 90 \
  --comparison-operator GreaterThanOrEqualToThreshold \
  --treat-missing-data missing \
  --alarm-actions arn:aws:sns:ap-east-1:111122223333:prod-critical-alerts \
  --region ap-east-1

EBS queue alarm:

aws cloudwatch put-metric-alarm \
  --alarm-name "P2-EBS-vol-0123456789abcdef0-queue-high" \
  --namespace AWS/EBS \
  --metric-name VolumeQueueLength \
  --dimensions Name=VolumeId,Value=vol-0123456789abcdef0 \
  --statistic Average \
  --period 60 \
  --evaluation-periods 10 \
  --datapoints-to-alarm 10 \
  --threshold 10 \
  --comparison-operator GreaterThanOrEqualToThreshold \
  --treat-missing-data missing \
  --alarm-actions arn:aws:sns:ap-east-1:111122223333:prod-warning-alerts \
  --region ap-east-1

5. Dashboard#

Minimum dashboard panels:

health:
    StatusCheckFailed
    StatusCheckFailed_Instance
    StatusCheckFailed_System

compute:
    CPUUtilization
    CPUCreditBalance / CPUSurplusCreditsCharged for T family

memory / disk:
    mem_used_percent
    disk_used_percent by path
    swap_used_percent

storage:
    VolumeQueueLength
    VolumeReadOps / VolumeWriteOps
    VolumeReadBytes / VolumeWriteBytes
    BurstBalance for burstable volumes

network:
    NetworkIn / NetworkOut
    NetworkPacketsIn / NetworkPacketsOut

agent:
    CloudWatch Agent metric freshness

6. Readiness Gap#

This page can cover EC2 infrastructure monitoring baseline, but it is not complete for every workload.

Still need to add or decide per environment:

process monitoring:
    critical process alive
    systemd unit failed
    CloudWatch Agent procstat or SSM inventory

log monitoring:
    kernel OOM
    disk filesystem errors
    application fatal logs
    auth failure / sudo / ssh logs when required

patch / security:
    SSM patch compliance
    Inspector findings
    EDR / antivirus health if used

Auto Scaling:
    ASG InService instance count
    launch failure
    lifecycle hook stuck
    capacity rebalance / spot interruption if used

backup / recovery:
    EBS snapshot success
    AWS Backup job status
    restore test result

network detail:
    security group / NACL change events
    VPC Flow Logs for rejected traffic if needed

Conclusion:

enough for:
    EC2 health
    CPU / memory / disk
    EBS queue / burst risk
    basic network trend

not enough for:
    application availability
    OS security compliance
    backup assurance
    ASG fleet health
    detailed network troubleshooting