-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStart-Stop-EC2-Instance.py
68 lines (53 loc) · 1.53 KB
/
Start-Stop-EC2-Instance.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# /usr/bin/python2.7
# written by Tomas (www.lisenet.com) on 05/11/2012
# copyleft free software
import boto.ec2
import sys
# specify AWS keys
auth = {"aws_access_key_id": "<key_id>", "aws_secret_access_key": "<access_key>"}
def main():
# read arguments from the command line and
# check whether at least two elements were entered
if len(sys.argv) < 2:
print "Usage: python aws.py {start|stop}\n"
sys.exit(0)
else:
action = sys.argv[1]
if action == "start":
startInstance()
elif action == "stop":
stopInstance()
else:
print "Usage: python aws.py {start|stop}\n"
def startInstance():
print "Starting the instance..."
# change "eu-west-1" region if different
try:
ec2 = boto.ec2.connect_to_region("eu-west-1", **auth)
except Exception, e1:
error1 = "Error1: %s" % str(e1)
print(error1)
sys.exit(0)
# change instance ID appropriately
try:
ec2.start_instances(instance_ids="i-12345678")
except Exception, e2:
error2 = "Error2: %s" % str(e2)
print(error2)
sys.exit(0)
def stopInstance():
print "Stopping the instance..."
try:
ec2 = boto.ec2.connect_to_region("eu-west-1", **auth)
except Exception, e1:
error1 = "Error1: %s" % str(e1)
print(error1)
sys.exit(0)
try:
ec2.stop_instances(instance_ids="i-12345678")
except Exception, e2:
error2 = "Error2: %s" % str(e2)
print(error2)
sys.exit(0)
if __name__ == '__main__':
main(