How to check AWS EC2 instance current uptime

BigBoss picture BigBoss · Jan 12, 2017 · Viewed 11.7k times · Source

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 ?

Answer

helloV picture helloV · Jan 12, 2017

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