-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_instance.py
137 lines (119 loc) · 3.84 KB
/
create_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import boto3
import sys
import time
import config
boto3.setup_default_session(
aws_access_key_id = config.AWS_ACCESS_KEY,
aws_secret_access_key = config.AWS_SECRET_KEY,
region_name = config.AWS_REGION_NAME
)
client = boto3.client('ec2')
ec2 = boto3.resource('ec2')
# Read file to load in as user data
def user_data():
try:
f = open('user-data.sh', 'rU')
data = f.read()
f.close()
return data
except IOError as e:
print ('Cannot read user data:', str(e))
sys.exit(1)
# Configure security group
def security_group():
group_name = 'http_ssh'
group_desc = 'Allow incoming http and ssh traffic'
# If security group exists, get group id
groups = [(sg.group_name, sg.group_id) for sg in ec2.security_groups.all()]
# Return security groups with name matching group_name
# sg is a tuple of (group_name, group_id)
group = [(sg[0], sg[1]) for sg in groups if sg[0] == group_name]
if group:
# First item in array, first item in tuple
group_id = group[0][0]
print ('Using existing security group with id %s' % (group_id))
return group_id
# Otherwise, create security group
else:
try:
group = ec2.create_security_group(
GroupName=group_name,
Description=group_desc
)
group_id = group.id
print ('Security Group created with id %s' % (group_id))
response = group.authorize_ingress(
IpPermissions=[
{
'IpProtocol': 'tcp',
'FromPort': 80,
'ToPort': 80,
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
},
{
'IpProtocol': 'tcp',
'FromPort': 22,
'ToPort': 22,
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
}
]
)
print ('Ingress rules successfully added')
return group_id
except Exception as e:
print ('Error occurred while creating security group:', str(e))
sys.exit(1)
# Create the instance
def create_instance(key, instance_name):
security_group_id = security_group()
data = user_data()
# Use default instance name
if not instance_name:
instance_name = 'captain-hook'
try:
instance = ec2.create_instances(
ImageId='ami-acd005d5',
KeyName=key,
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
UserData=data,
SecurityGroupIds=[security_group_id],
Placement={
'AvailabilityZone': 'eu-west-1a'
},
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': instance_name
}
]
}
]
)
# Wait for instance to start up before returning
instance = instance[0]
while instance.state['Name'] != 'running':
print ('Instance is starting up...')
time.sleep(10)
instance.reload()
print ('Instance created with id:', instance.id)
return instance
except Exception as e:
print ('Error while creating instance:', str(e))
sys.exit(1)
def main():
args = sys.argv[1:]
if not args:
print ('Please supply key as first argument')
sys.exit(1)
if len(args) > 1:
create_instance(args[0], args[1])
else:
create_instance(args[0], '')
if __name__ == '__main__':
main()