Advertisement
Guest User

Untitled

a guest
May 7th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. def stop_instances(region, instance_tag="my_instance" , instance_state="running"):
  2. """
  3. The function
  4. 1. filters all the instances by tag and instance-state
  5. 2. stops the filtered instances (does not terminate/delete them)
  6. 3. returns the just stopped instances (ids) for future reference
  7. """
  8. c = boto.ec2.connect_to_region(region_name = region,
  9. aws_access_key_id = aws_access_key_id,
  10. aws_secret_access_key = aws_secret_access_key,
  11. )
  12.  
  13. stopped_instances = []
  14.  
  15. reserves = c.get_all_instances(filters={"tag-value":instance_tag , "instance-state-name":instance_state})
  16. for r in reserves:
  17. for instance in r.instances:
  18. instance.stop()
  19. print "OK! instance -"+str(instance.id)+"- was stopped. \n"
  20. # save the just stopped instances
  21. stopped_instances.append( instance.id )
  22.  
  23. return stopped_instances
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement