Last active
August 29, 2015 14:17
-
-
Save wehappyfew/39ca4e4b4ffcdfd3d899 to your computer and use it in GitHub Desktop.
This file contains 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
def stop_instances(region, instance_tag="my_instance" , instance_state="running"): | |
""" | |
The function | |
1. filters all the instances by tag and instance-state | |
2. stops the filtered instances (does not terminate/delete them) | |
3. returns the just stopped instances (ids) for future reference | |
""" | |
c = boto.ec2.connect_to_region(region_name = region, | |
aws_access_key_id = aws_access_key_id, | |
aws_secret_access_key = aws_secret_access_key, | |
) | |
stopped_instances = [] | |
reserves = c.get_all_instances(filters={"tag-value":instance_tag , "instance-state-name":instance_state}) | |
for r in reserves: | |
for instance in r.instances: | |
instance.stop() | |
print "OK! instance -"+str(instance.id)+"- was stopped. \n" | |
# save the just stopped instances | |
stopped_instances.append( instance.id ) | |
return stopped_instances |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment