You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置CloudWatch Alarm在EC2实例uptime超5小时时触发告警?

Solution: Set Up CloudWatch Alarm for EC2 Uptime Exceeding 5 Hours

Hey there! Since you're new to AWS CloudWatch, let's break this down into simple, actionable steps. The catch here is that AWS doesn't provide a native "uptime" metric for EC2 instances out of the box, so we'll need to either collect system-level uptime data or calculate it using the instance's launch time. Let's cover both approaches:

Approach 1: Collect Uptime via CloudWatch Agent (Direct System Metric)

This method uses the CloudWatch Agent to send your EC2 instance's actual system uptime to CloudWatch, then builds an alarm around that metric.

Step 1: Install and Configure the CloudWatch Agent

  • Connect to your EC2 instance via SSH (or RDP for Windows instances).
  • For Linux-based instances, run the appropriate install command:
    # For Amazon Linux/RHEL
    sudo yum install amazon-cloudwatch-agent -y
    # For Ubuntu/Debian
    sudo apt-get install amazon-cloudwatch-agent -y
    
  • Launch the configuration wizard to set up metric collection:
    sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
    
  • Follow the wizard prompts:
    • Select EC2 as the host type.
    • Choose to store the configuration in AWS Systems Manager Parameter Store (simpler for future management).
    • When selecting metrics to collect, enable System metrics and ensure the uptime metric is included (under Linux/Windows system metrics sections).
    • Complete the wizard, then start the agent:
      sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:your-parameter-store-name -s
      

Step 2: Create the CloudWatch Alarm

  • Navigate to the AWS CloudWatch Console → Alarms → All alarms → Create alarm.
  • Select Select metric → Search for the CWAgent namespace → Choose your instance's dimension → Select the uptime metric.
  • Set the Statistic to Maximum (since uptime is a cumulative value) and Period to 5 minutes (adjust based on how frequently you want to check).
  • Under Conditions:
    • Threshold type: Static
    • Whenever uptime is: Greater than
    • Threshold value: 18000 (5 hours converted to seconds: 56060)
  • Configure notifications: Select an existing SNS topic or create a new one to send alerts (email, SMS, etc.).
  • Name your alarm (e.g., "EC2-Uptime-Exceeds-5Hours"), add a clear description, then click Create alarm.

Approach 2: Calculate Uptime Using Instance Launch Time (No Agent Required)

If you prefer not to install the CloudWatch Agent, you can use CloudWatch Metric Math to calculate uptime based on the instance's launch time metadata.

Step 1: Build a Metric Math Alarm

  • Go to CloudWatch Console → Alarms → Create alarm → Select metric.
  • Under All metrics, navigate to EC2 → Per-Instance Metrics → Select any metric tied to your instance (e.g., CPUUtilization—we just need the instance dimension).
  • Click Add math → Start with empty expression.
  • Enter this Metric Math expression (replace i-1234567890abcdef0 with your actual instance ID):
    TIME_SINCE(GET_METRIC_DATA(SEARCH('{AWS/EC2,InstanceId} InstanceId="i-1234567890abcdef0"', 'Maximum', 300)), 'SECONDS')
    
    This formula calculates the time elapsed since the instance launched.
  • Set the Period to 5 minutes.
  • Under Conditions:
    • Threshold type: Static
    • Whenever math expression is: Greater than
    • Threshold value: 18000 (5 hours in seconds)
  • Configure your notification preferences, name the alarm, and click Create alarm.

Key Notes

  • For Approach 2: The calculation resets if you stop and start the instance (since a stopped/started instance gets a new launch time).
  • For Approach 1: Ensure your EC2 instance has the AmazonCloudWatchAgentServerPolicy managed IAM policy to send metrics to CloudWatch.

内容的提问来源于stack exchange,提问作者user12417145

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:27:26