Created
February 13, 2016 03:50
-
-
Save stevear22/3f5e3e55eaf24e52220c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This will shut down all instances in all regions at 17:30 | |
import boto3.ec2 | |
from datetime import datetime | |
import time | |
region_list = [] | |
if __name__ == "__main__": | |
# Generate regions list | |
for region in boto3.client('ec2').describe_regions()['Regions']: | |
region_list.append(region['RegionName']) | |
currenttime = datetime.now().time() | |
# Shut down all EC2 instsances after 17:30 | |
if currenttime > datetime.time(17, 30): | |
print 'It is after 17:30' | |
for region in region_list: | |
print 'checking region', region | |
ec2 = boto3.resource('ec2', region) | |
for instance in ec2.instances.all(): | |
print ' instance found:', instance | |
state = instance.state['Name'] | |
print ' state:', state | |
if instance.state['Name'] == 'running': | |
print ' Shutting down instance', instance.id | |
instance.stop(instance.id) | |
while state != 'stopped': | |
print ' state:', state | |
time.sleep(5) | |
print ' state:', state | |
else: | |
print 'It is before 17:30' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment