-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2.py
277 lines (257 loc) · 12.6 KB
/
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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import os
from subprocess import PIPE, run, check_output,call
profile = ""
keyfile = ""
# wrapper to get ouput of system command
def out(command):
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
return result.stdout
# function to launch AWS instances
def launchInstance():
keyname, securityGroup, rc = keyAndSecurity()
if rc != 0:
return
print("\n1. Amazon Linux 2 AMI 64-bit (x86)\t2. RHEL 8 64-bit (x86)\t 3. Custom Image ID\t 4. Exit Instance Creation")
for i in range(5):
amiChoice = input("Enter choice (1-4) : ")
if amiChoice == '1':
imageID = "ami-0e306788ff2473ccb"
break
elif amiChoice == '2':
imageID = "ami-052c08d70def0ac62"
break
elif amiChoice == '3':
imageID = input("Enter Image ID : ")
break
elif amiChoice == '4':
print("Exiting...")
return
else:
if i == 4:
print("Entered wrong option 5 times. Exiting...")
return
print("Invalid choice!!! Choose from 1-4\n")
print("Create Instance with instanceID : {}, type : t2.micro, key-name : {}, security-group : {} ?".format(imageID, keyname, securityGroup))
create = input("Enter Y to confirm : ")
if create == 'Y' or create == 'y':
errorInstance = call(
"aws ec2 run-instances --image-id {} --count 1 --instance-type t2.micro --key-name {} --security-groups {} --profile {}".format(imageID, keyname, securityGroup, profile))
if errorInstance == 0:
instanceID = input("Enter the instance ID : ")
nametag = input("Enter name tag for instance (Eg. MyInstance): ")
call(
"aws ec2 create-tags --resources {} --tags Key=Name,Value={} --profile {}".format(instanceID, nametag, profile))
print("Instance is created and running.\n")
return
# function to create key-pair and security group for instance
def keyAndSecurity():
while True:
print("\nDefault values for each field taken if left blank => key-pair : MyKeyPair | security-group : my-sg | --description : My Security Group ")
keyPair = input("1. Enter name for new key-pair : ")
secGroup = input("2. Enter name for security group : ")
descSecurity = input(
"Enter description for security group {} : ".format(secGroup))
if keyPair == '':
keyPair = "MyKeyPair"
if secGroup == '':
secGroup = "my-sg"
if descSecurity == '':
descSecurity = "My Security Group"
print("Entered fields : \nkey-pair : {}\nsecurity-group : {}\ndescription : {}\n".format(
keyPair, secGroup, descSecurity))
confirm = input("Press Y to confirm : ")
if(confirm == 'Y' or confirm == 'y'):
# Creating Key-Pair
os.system(r"mkdir %USERPROFILE%\KeyPairs")
print(r"{}.pem created in folder C:\Users\%USERNAME%\KeyPairs ".format(keyPair))
errorKeyPair = os.system(
r"aws ec2 create-key-pair --key-name {0} --profile {1} --output text > %USERPROFILE%\KeyPairs\{0}.pem".format(keyPair, profile))
# Creating Security-Group
errorSecurityGroup = call(
'aws ec2 create-security-group --group-name {} --description "{}" --profile {}'.format(secGroup, descSecurity, profile))
if errorKeyPair != 0 or errorSecurityGroup != 0:
recreate = input(
"\nWould you like to delete created key-pair or security-group and try again ? (Press Y for Yes): ")
if recreate == 'Y' or recreate == 'y':
if errorKeyPair != 0 and errorSecurityGroup == 0:
rc = 11
call(
"aws ec2 delete-security-group --group-name {} --profile {}".format(secGroup, profile))
elif errorSecurityGroup != 0 and errorKeyPair == 0:
rc = 12
call(
"aws ec2 delete-key-pair --key-name {} --profile {}".format(keyPair, profile))
continue
else:
rc = 1
break
else:
rc = 0
print("rc : {}".format(rc))
break
return (keyPair, secGroup, rc)
# function to add inbound rules to security group
def inboundRules():
sgname = input("\nEnter security group name : ")
print("1. All Traffic (Default) 2. TCP 3. UDP 4. SSH 5. Custom Protocol : ")
for i in range(5):
plChoice = input("Choose between 1-5 : ")
if plChoice == '1' or plChoice == '':
protocol = 'all'
port = 'all'
break
elif plChoice == '2':
protocol = 'tcp'
port = '0-65535'
break
elif plChoice == '3':
protocol = 'udp'
port = '0-65535'
break
elif plChoice == '4':
protocol = 'tcp'
port = '22'
break
elif plChoice == '5':
protocol = input("Enter protocol number (Eg. 1 for ICMP, 6 for TCP etc.) : ")
port = input("Enter port range (leave blank for all by default): ")
if port == '':
port = 'all'
break
else:
if i == 4:
print("Entered wrong option 5 times. Exiting...")
return
print("Invalid choice!!! Choose from 1-5\n")
cidr = input("Enter custom cidr (leave blank for 0.0.0.0/0 by default) : ")
if cidr == '':
cidr = '0.0.0.0/0'
confirm = input("Confirm creation of inbound rule for Security Group : {} => for Protocol : {} \
Port : {} and CIDR : {} ? (Press Y to confirm) : ".format(sgname, protocol, port, cidr))
if confirm == 'y' or confirm == 'Y':
errorcheck = call("aws ec2 authorize-security-group-ingress --group-name {} --protocol {} \
--port {} --cidr {} --profile {}".format(sgname, protocol, port, cidr, profile))
print("rc : {}".format(errorcheck))
return
def ec2menu(prof):
global profile, keyfile
profile = prof
print('\n')
while True:
print("\nEC2 MENU\
\n--------")
print("\n1. Launch instance \t2. Show Key-Pairs \t3. Delete Key-Pair\
\n4. Show Security-Groups \t5. Delete Security-Group \t6. Add Inbound Rules\
\n7. Show Instances \t8. Start Instance \t9. Stop Instance\
\n10.Terminate Instance \t11.Create EBS volume \t12.Attach EBS Volume\
\n13.Show available storage \t14.Mount New Partition \t15.Show the filesystem\
\n\nPress Q to exit")
choice = input("> ")
if choice == '1':
launchInstance()
elif choice == '2':
os.system(
"aws ec2 describe-key-pairs --profile {}".format(profile))
elif choice == '3':
keypair = input("\nEnter name of key-pair to delete : ")
error = os.system(
"aws ec2 delete-key-pair --key-name {} --profile {}".format(keypair, profile))
if error == 0:
print("Key-Pair : {} deleted.".format(keypair))
elif choice == '4':
os.system(
"aws ec2 describe-security-groups --profile {}".format(profile))
elif choice == '5':
sg = input("\nEnter name of security-group to delete : ")
error = os.system(
"aws ec2 delete-security-group --group-name {} --profile {}".format(sg, profile))
if error == 0:
print("Security-Group : {} deleted.".format(sg))
elif choice == '6':
inboundRules()
elif choice == '7':
os.system("aws ec2 describe-instances --profile {}".format(profile))
elif choice == '8':
instance_id = input("\nEnter instance-id : ")
error = call("aws ec2 start-instances --instance-ids {} \
--profile {}".format(instance_id, profile))
if error == 0:
print("Instance started successfully.")
print("rc : {}".format(error))
elif choice == '9':
instance_id = input("\nEnter instance-id : ")
error = call("aws ec2 stop-instances --instance-ids {} \
--profile {}".format(instance_id, profile))
if error == 0:
print("Instance stopped successfully.")
print("rc : {}".format(error))
elif choice == '10':
instance_id = input("\nEnter instance-id : ")
error = call("aws ec2 terminate-instances --instance-ids {} \
--profile {}".format(instance_id, profile))
if error == 0:
print("Instance termination processed. Instance will be deleted in some time.")
print("rc : {}".format(error))
elif choice == '11':
size = input("Enter volume size : ")
az = input("Enter availability zone (Default : ap-south-1a): ")
if az == '':
az = 'ap-south-1a'
error = call("aws ec2 create-volume --availability-zone {} --size {} \
--profile {}".format(az, size, profile))
if error == 0:
print("EBS Volume created successfully")
print("rc : {}".format(error))
elif choice == '12':
volume = input("Enter your volume-id : ")
instance = input("Enter instance-id : ")
error = call("aws ec2 attach-volume --volume-id {} --instance-id {} \
--device /dev/sdh --profile {}".format(volume, instance, profile))
if error == 0:
print("EBS Volume attached successfully")
print("rc : {}".format(error))
elif choice == '13':
key = input("Enter key name with .pem extension: ")
address = out("echo %USERPROFILE%").rstrip("\n")
key = r"{}\KeyPairs\{}".format(address, key)
instance = input("Enter instance-id : ")
publicDns = out("aws ec2 describe-instances --instance-ids {}\
--query Reservations[*].Instances[*].[PublicDnsName] --output text".format(instance)).rstrip("\n")
error = os.system('ssh -i {} ec2-user@{} sudo fdisk -l'.format(key, publicDns))
print("rc : {}".format(error))
elif choice == '14':
key = input("Enter key name with .pem extension: ")
address = out("echo %USERPROFILE%").rstrip("\n")
key = r"{}\KeyPairs\{}".format(address, key)
instance = input("\nEnter instance-id : ")
publicDns = out("aws ec2 describe-instances --instance-ids {}\
--query Reservations[*].Instances[*].[PublicDnsName] --output text".format(instance)).rstrip("\n")
name = input("Enter name of volume you wish to create partition in : ")
error = os.system("ssh -i {} ec2-user@{} sudo fdisk {}".format(key, publicDns, name))
error2, error3 = 1,1
if error == 0:
print("Partition {} created.".format(name))
error2 = os.system("ssh -i {} ec2-user@{} sudo mkfs.ext4 {}".format(key, publicDns, name))
if error2 == 0:
print("Partition {} formatted.".format(name))
mount = input("Enter name for new mount point : ")
error3 = os.system("ssh -i {} ec2-user@{} sudo mkdir {}".format(key, publicDns, mount))
error4 = os.system("ssh -i {} ec2-user@{} sudo mount {} {}".format(key, publicDns, name, mount))
if error3 == 0 and error4 == 0:
print("Partition {} mounted on directory {}.".format(name, mount))
print("rc : {}".format(error+error2+error3+error4))
elif choice == '15':
key = input("Enter key name with .pem extension: ")
address = out("echo %USERPROFILE%").rstrip("\n")
key = r"{}\KeyPairs\{}".format(address, key)
instance = input("\nEnter instance-id : ")
publicDns = out("aws ec2 describe-instances --instance-ids {}\
--query Reservations[*].Instances[*].[PublicDnsName] --output text".format(instance)).rstrip("\n")
error = os.system("ssh -i {} ec2-user@{} sudo df -h".format(key, publicDns))
print("rc : {}".format(error))
elif choice == 'Q' or choice == 'q':
print("Exiting...\n")
break
else:
print("Invalid choice!!! Choose from 1-15 or Q to exit.\n")
return