What is the best way to check the EC2 instance uptime and possibly send alerts if uptime for instance is more then N hours? How can it be organized with default AWS tools such as CloudWatch, Lambda ?
3 Answers
Here's another option which can be done just in CloudWatch.
Create an alarm for your EC2 instance with something like CPUUtilization - you will always get a value for this when the instance is running.
- Set the alarm to >= 0; this will ensure that whenever the instance is running, it matches.
- Set the period and consecutive periods to match the required alert uptime, for example for 24 hours you could set the period to 1 hour and the consecutive periods to 24.
- Set an action to send a notification when the alarm is in ALARM state.
Now, when the instance has been on less than the set time, the alarm will be in INSUFFICIENT DATA state. Once it has been on for the uptime, it will go to ALARM state and the notification will be sent.

- 744
- 6
- 12
-
Note: alarms can only check a span of up to 24 hours, so beyond that, this will no longer work. That being said, this is clever and a great solution up until that point since the newest CloudWatch agent (2021) does not report uptime. – UberMario Jan 04 '22 at 04:39
One option is to use AWS CLI and get the launch time. From that calculate the uptime and send it to Cloudwatch:
aws ec2 describe-instances --instance-ids i-00123458ca3fa2c4f --query 'Reservations[*].Instances[*].LaunchTime' --output text
Output
2016-05-20T19:23:47.000Z
Another option is to periodically run a cronjob
script that:
- calls
uptime -p
command - converts the output to hours
- sends the result to Cloudwatch with dimension
Count
After adding the cronjob
:
- add a Cloudwatch alarm that sends an alert when this value exceeds a threshold or if there is INSUFFICIENT DATA
- INSUFFICIENT DATA means the machine is not up

- 50,176
- 7
- 137
- 145
-
1Can it be done without direct accessing to server? Just using aws api or something else? – aRTURIUS Jan 12 '17 at 22:46
-
could you please provide info which kind of results should send as dimension Count and which type of metric to choose when creating alarm? I'm using lambda to count uptime. – aRTURIUS Jan 13 '17 at 13:55
-
@bparker why don't you check your claim before commenting? You down-voted an answer without understanding it. `Launch time` is a misnomer. It actually means the time the instance was last started. Try it. – helloV Jul 01 '17 at 22:57
-
-
7In my tests `LaunchTime` doesn't change with reboot, it just shows when the instance was provisioned. Uptime is time from the OS start. I would down-vote this answer, unless I'm missing something. – Ivan Jun 23 '19 at 11:57
I would recommend looking into an "AWS" native way of doing this.
If it is basically sending OS level metrics (e.g. Free Memory, Uptime, Disk Usage etc...) to Cloudwatch then this can be achieved by following the guide: This installs the Cloudwatch Logs Agent on your EC2 instances. http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/QuickStartEC2Instance.html
The great thing about this is you then get the metrics show up in Cloudwatch logs (see attached picture which shows the CW Logs interface in AWS Console.).

- 59
- 4
-
If you launch an ec2 instance will the agent automatically run? Or does it need to be started/configured each time? – asunrey Jul 11 '19 at 22:48