4

I have a server on Amazon EC2 and I would like to reboot it whenever it stops responding for HTTP requests. It is a single micro instance.

I was thinking about using AWS Lambda but I could not find any scripts (preferably in Python). I also tried to use Route 53 healthcheck but it is impossible to link it to alarm with reboot EC2 (because EC2 actions are not available on healthchecks alarms).

Thanks

Koss645
  • 111

3 Answers3

2

If the instance stops responding to HTTP it will probably stop being "healthy" and will show up as such in CloudWatch -> Metrics -> EC2 -> Per Instance Metrics -> i-1234abcd...

Then find StatusCheckFailed and StatusCheckFailed_Instance and StatusCheckFailed_System and see if they show when the instance stops responding. One of them should. Alternatively find some other usable metric, maybe in Route53 namespace.

Once you find a suitable metric create an Alarm by clicking the Graphed Metrics and then the little "bell" on the right.

enter image description here

In the next dialog click +EC2 Action and select Reboot Instance. You may need to tune some other parameters, that will probably take a couple of iterations.

enter image description here

Done :)

Hope that helps!

MLu
  • 26,247
2

I solved it myself, I wrote an lambda function in Python and run it every hour by event scheduler in AWS CloudWatch

import json
from botocore.vendored import requests
import boto3
import time

region = 'xx-xxxx-x'
instances = ['x-xxxxxxxxxxxx']
website = 'https://website.com/'
webstring = 'SearchText'

def lambda_handler(event, context):
    for i in range(0,3):
        if check_website():
            return 'Website OK'
        time.sleep(60)
    reboot_instance()
    return 'Restarted instances'


def check_website():
    r = requests.get(website)
    if webstring in r.text:
        return True
    else:
        return False

def reboot_instance():
    ec2 = boto3.client('ec2', region_name=region)
    ec2.reboot_instances(InstanceIds=instances)
Koss645
  • 111
0

You need to use the AWS API. One way could be to use the boto

Or you could use something more high-level like the Ansible EC_instance module

You could link this to monitoring events in many ways, from a simple cron job to something event-based like a Node-Red instance or something in between like an IFTT trigger.