-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch_ec2.py
More file actions
150 lines (128 loc) · 4.65 KB
/
launch_ec2.py
File metadata and controls
150 lines (128 loc) · 4.65 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
import yaml
import boto3
import os
CONFIG_FILE_PATH = './config.yaml'
with open(CONFIG_FILE_PATH, 'r') as config_file:
data = config_file.read()
#Try laoding the config file
try:
config = yaml.safe_load(data)
except Exception as e:
print(e)
#Read necessary configurations for creating ec2
instanceType = config['server']['instance_type']
imageId = config['server']['ami_type']
minCount = config['server']['min_count']
maxCount = config['server']['max_count']
virtualizationType = config['server']['virtualization_type']
#AWS resources initialization
ec2_client = boto3.client('ec2')
ec2_resource = boto3.resource('ec2')
#Create key-pair for ec2-user
try:
print('Creating key-pair for ec2-user...')
keyName ='fetch-rewards-user'
key_response = ec2_client.create_key_pair(KeyName = keyName)
with open('private.pem', 'w') as key:
key.write(str(key_response['KeyMaterial']))
chmd = os.system('chmod 400 private.pem')
except Exception as e:
print(e)
userData = '''
#!bin/bash
'''
#Generate volume userData & volume parameter
blockDevices = []
for volume in config['server']['volumes']:
volumeConfig = {
'DeviceName': volume['device'],
'Ebs': {
'VolumeSize': volume['size_gb'],
#'VolumeType': volume['type']
}
}
blockDevices.append(volumeConfig)
userData += f"sudo mkdir {volume['mount']}\n"
userData += f"sudo mkfs -t {volume['type']} {volume['device']}\n"
userData += f"sudo mount {volume['device']} {volume['mount']}\n"
userData += 'sudo chmod o+rw /\n'
userData += 'sudo chmod o+rw /data/\n'
#Generate UserData
for index, user in enumerate(config['server']['users'], start=1):
#Generate key-pair for users
try:
print(f"generating key-pair for {user['login']}...")
key_response = ec2_client.create_key_pair(KeyName = user['login'])
with open(f"{user['login']}.pem", 'w') as key:
key.write(str(key_response['KeyMaterial']))
chmd = os.system(f"chmod 400 {user['login']}.pem")
except Exception as e:
print(e)
userData += f"sudo adduser {user['login']}\n"
userData += f"sudo mkdir -p /home/{user['login']}/.ssh\n"
userData += f"sudo chown -R {user['login']}:{user['login']} /home/{user['login']}\n"
pub_key = os.popen(f'ssh-keygen -y -f user{index}.pem').readlines()
userData += f"echo '{pub_key[0]}' >> /home/{user['login']}/.ssh/authorized_keys\n"
# create VPC and other resources
try:
vpc = ec2_resource.create_vpc(CidrBlock='10.10.0.0/16')
vpc.create_tags(Tags=[{"Key": "Name", "Value": "fetchReward_exe_vpc"}])
vpc.wait_until_available()
print(vpc.id)
# create then attach internet gateway
ig = ec2_resource.create_internet_gateway()
vpc.attach_internet_gateway(InternetGatewayId=ig.id)
print(ig.id)
# create a route table and a public route
route_table = vpc.create_route_table()
route = route_table.create_route(
DestinationCidrBlock='0.0.0.0/0',
GatewayId=ig.id
)
print(route_table.id)
# create subnet
subnet = ec2_resource.create_subnet(CidrBlock='10.10.1.0/24', VpcId=vpc.id)
print(subnet.id)
# associate the route table with the subnet
route_table.associate_with_subnet(SubnetId=subnet.id)
# Create sec group
sec_group = ec2_resource.create_security_group(
GroupName='sec_group_fetchRewards', Description='Fetch Rewards Security Group', VpcId=vpc.id)
sec_group.authorize_ingress(
CidrIp='0.0.0.0/0',
IpProtocol='tcp',
FromPort=22,
ToPort=22
)
print(sec_group.id)
except Exception as e:
print(e)
#Create ec2 instance
try:
print('Creating EC2 instance...')
instance = ec2_client.run_instances(
BlockDeviceMappings = blockDevices,
ImageId = imageId,
InstanceType = instanceType,
MinCount = minCount,
MaxCount = maxCount,
NetworkInterfaces = [
{
'SubnetId': subnet.id,
'DeviceIndex': 0,
'AssociatePublicIpAddress': True,
'Groups': [sec_group.group_id]
}
],
KeyName = keyName,
UserData = userData
)
print('EC2 instance created: ', instance['Instances'][0]['InstanceId'])
instance_resource = ec2_resource.Instance(instance['Instances'][0]['InstanceId'])
instance_resource.wait_until_running()
reservations = ec2_client.describe_instances(InstanceIds = [instance['Instances'][0]['InstanceId']]).get('Reservations')
for reservation in reservations:
for instance in reservation['Instances']:
print('IPv4: ', instance.get('PublicIpAddress'))
except Exception as e:
print(e)