summaryrefslogtreecommitdiff
path: root/bin/kill_instance
blob: 0c637413b4d6f5763f7ea464c157585851ca8ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python

import sys
from optparse import OptionParser

import boto
from boto.ec2 import regions



def kill_instance(region, ids):
    """Kill an instances given it's instance IDs"""
    # Connect the region
    ec2 = boto.connect_ec2(region=region)
    for instance_id in ids:
        print "Stopping instance: %s" % instance_id
        ec2.terminate_instances([instance_id])


if __name__ == "__main__":
    parser = OptionParser(usage="kill_instance [-r] id [id ...]")
    parser.add_option("-r", "--region", help="Region (default us-east-1)", dest="region", default="us-east-1")
    (options, args) = parser.parse_args()
    if not args:
        parser.print_help()
        sys.exit(1)
    for r in regions():
        if r.name == options.region:
            region = r
            break
    else:
        print "Region %s not found." % options.region
        sys.exit(1)

    kill_instance(region, args)