Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check AWS EC2 instance current uptime

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 ?

like image 460
BigBoss Avatar asked Jan 12 '17 21:01

BigBoss


2 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.

like image 144
emorris Avatar answered Nov 03 '22 14:11

emorris


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.).enter image description here

like image 42
Ben Fellows Avatar answered Nov 03 '22 15:11

Ben Fellows