Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Created February 27, 2016 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalgjose/098375a1f4cac24189bf to your computer and use it in GitHub Desktop.
Save amalgjose/098375a1f4cac24189bf to your computer and use it in GitHub Desktop.
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
Copy link

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