Sometimes we might have to reboot EC2 instances. If the requirement is to restart EC2 instances regularly, we can achieve it by writing a small piece of code. I also came across a similar requirement and a portion of the code I used is given below.

 


__author__ = 'Amal G Jose'
import boto.ec2
class EC2Reboot(object):
def __init__(self):
self.instance_id_list = ["i-xxxxxx", "i-xxxxxx", "i-xxxxx", "i-xxxx", "i-xxxxx", "i-xxxxx", "i-xxxxx"]
self.aws_access_key = "XXXXXXXXXXX"
self.aws_secret_key = "XXXXXXXXXXX"
self.region = "xx-xxxx-x"
self.conn = boto.ec2.connect_to_region(self.region,
aws_access_key_id=self.aws_access_key,
aws_secret_access_key=self.aws_secret_key)
def restart_instances(self):
try:
print "Rebooting instances: %s" %(str(self.instance_id_list))
self.conn.reboot_instances(self.instance_id_list)
except Exception, e:
print "Error occurred while restarting instances: %s" %(str(e))
if __name__ == '__main__':
restart = EC2Reboot()
restart.restart_instances()

view raw

EC2Reboot.py

hosted with ❤ by GitHub

Advertisement