Search code examples
amazon-web-servicesamazon-ec2aws-cliamazon-cloudwatchuptime-monitoring

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 ?


Solution

  • 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