forked from rupalibehera/boto-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch_instance.py
executable file
·65 lines (48 loc) · 1.9 KB
/
launch_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
#!/usr/bin/env python
import boto
import boto.ec2
import os
from optparse import OptionParser
import sys
import time
import yaml
dirname = os.path.abspath(os.path.dirname(__file__))
config = yaml.load(open(os.path.join(dirname,"config.yaml"), "r"))
def getConnection():
return boto.ec2.connect_to_region(config['REGION'], \
aws_access_key_id = config['AWS_ACCESS_KEY_ID'],
aws_secret_access_key = config['AWS_SECRET_ACCESS_KEY'])
def launchInstance(ami):
conn = getConnection()
image = conn.get_image(ami)
security_groups = ['basic_server',]
reservation = image.run(key_name=config['KEY'],
security_groups=security_groups,
instance_type=config['INSTANCE_TYPE'])
instance = reservation.instances[0]
print instance
instanceId=instance.id
conn.create_tags([instanceId],
{"Name": '%s' % (tag_to_instance or "instance_launched_with_boto")})
print "Spinning up instance for '%s'. Waiting for it to boot up." % (ami)
while instance.state == 'pending':
print "Instance state: %s" % (instance.state)
time.sleep(10)
instance.update()
publicDnsName = instance.public_dns_name
print "Instance is running, public dns : %s" % publicDnsName
return publicDnsName,instanceId
if __name__=='__main__':
parser = OptionParser()
parser.add_option('-a', '--ami', dest = 'ami', type = "string",
help = 'The AMI from which the instance should be launched')
parser.add_option('-t', '--tag', dest = 'tag', type = "string",
help = 'Give a tag to the instance')
(options, args) = parser.parse_args()
ami = options.ami
tag_to_instance = options.tag
if not ami :
print "Please provide the AMI to bring up the instance."
sys.exit(1)
#launching instance with the give AMI
publicDnsName, instanceId = launchInstance(ami)