Skip to content

Instantly share code, notes, and snippets.

@wehappyfew
Last active August 29, 2015 14:17
Show Gist options
  • Save wehappyfew/fb6b140f581ab394ba18 to your computer and use it in GitHub Desktop.
Save wehappyfew/fb6b140f581ab394ba18 to your computer and use it in GitHub Desktop.
def delete_instances(region, instance_tag="my_instance" , instance_state="stopped"):
"""
The function
1. filters all the instances by tag and instance-state
2. terminates/deletes the filtered instances (irreversible action!!!)
"""
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,
)
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.terminate()
print "OK! instance -"+str(instance.id)+"- was deleted. \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment