forked from jsh2134/thirty-min-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2.py
47 lines (32 loc) · 973 Bytes
/
ec2.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
from boto.ec2.connection import EC2Connection
from settings import AWS
import time
SERVER = {
'image_id' : AWS['defaults']['image_id'],
'instance_type' : AWS['defaults']['instance_type'],
'security_groups' : AWS['defaults']['security_groups'],
'key_name' : AWS['defaults']['key_name'],
}
class EC2Conn:
def __init__(self):
self.conn = None
def connect(self):
self.conn = EC2Connection(AWS['secrets']['aws_key'],
AWS['secrets']['aws_secret'])
def create_instance(self):
reservation = self.conn.run_instances( **SERVER)
print reservation
instance = reservation.instances[0]
time.sleep(10)
while instance.state != 'running':
time.sleep(5)
instance.update()
print "Instance state: %s" % (instance.state)
# Sleep for a bit more before trying to connect
time.sleep(60)
print "instance %s done!" % (instance.id)
return instance
def create_new_instance():
a = EC2Conn()
a.connect()
return a.create_instance()