Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python program to reboot multiple ec2 instances together. This can be used for performing some scheduled restart of EC2 machines in an Amazon account. I used python boto library for performing this operation.
__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()
@imranbhullar

This comment has been minimized.

Copy link

@imranbhullar imranbhullar commented Aug 21, 2017

I was trying to get a similar thing done but realized that AWS throttle the API access so If you reboot multiple instances at once this will create a bit of problem. You might want to put a sleep for like 5 seconds between each attempt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment