7
7
loginAWS = False
8
8
profile = ""
9
9
10
- # function to launch AWS instances
11
-
12
10
11
+ # function to launch AWS instances
13
12
def launchInstance ():
14
-
15
13
keyname , securityGroup , rc = keyAndSecurity ()
16
14
17
15
if rc != 0 :
18
16
return
19
17
20
18
exitflag = 0
21
- imageID = "ami-0e306788ff2473ccb"
22
- print ("\n 1. Amazon Linux 2 AMI 64-bit (x86)\t 2. RHEL 8 64-bit (x86)\t 3. Custom Image ID (Default : Amazon Linux 2 AMI)" )
19
+ print ("\n 1. Amazon Linux 2 AMI 64-bit (x86)\t 2. RHEL 8 64-bit (x86)\t 3. Custom Image ID\t 4. Exit Instance Creation" )
23
20
for i in range (5 ):
24
- amiChoice = input ("Enter choice (1-3) : " )
25
- if amiChoice == '2' :
21
+ amiChoice = input ("Enter choice (1-4) : " )
22
+ if amiChoice == '1' :
23
+ imageID = "ami-0e306788ff2473ccb"
24
+ break
25
+ elif amiChoice == '2' :
26
26
imageID = "ami-052c08d70def0ac62"
27
27
break
28
28
elif amiChoice == '3' :
29
29
imageID = input ("Enter Image ID : " )
30
30
break
31
+ elif amiChoice == '4' :
32
+ print ("Exiting..." )
33
+ return
31
34
else :
32
35
if i == 4 :
33
36
exitflag = 1
34
37
break
35
- print ("Invalid choice!!! Choose from 1-3 \n " )
38
+ print ("Invalid choice!!! Choose from 1-4 \n " )
36
39
if exitflag == 1 :
37
40
print ("Entered wrong option 5 times. Exiting..." )
38
41
return
@@ -46,11 +49,12 @@ def launchInstance():
46
49
instanceID = input ("Enter the instance ID : " )
47
50
nametag = input ("Enter name tag for instance (Eg. MyInstance): " )
48
51
subprocess .call (
49
- "aws ec2 create-tags --resources {} --tags Key=Name,Value={}" .format (instanceID , nametag ))
52
+ "aws ec2 create-tags --resources {} --tags Key=Name,Value={} --profile {} " .format (instanceID , nametag , profile ))
50
53
print ("Instance is created and running.\n " )
51
54
return
52
55
53
56
57
+ # function to create key-pair and security group for instance
54
58
def keyAndSecurity ():
55
59
while True :
56
60
print ("\n Default values for each field taken if left blank => key-pair : MyKeyPair | security-group : my-sg | --description : My Security Group " )
@@ -69,8 +73,10 @@ def keyAndSecurity():
69
73
confirm = input ("Press Y to confirm : " )
70
74
if (confirm == 'Y' or confirm == 'y' ):
71
75
# Creating Key-Pair
76
+ os .system (r"mkdir C:\KeyPairs" )
77
+ print (r"{}.pem created in folder C:\KeyPairs\ " .format (keyPair ))
72
78
errorKeyPair = os .system (
73
- "aws ec2 create-key-pair --key-name {0} --profile {1} --output text > {0}.pem" .format (keyPair , profile ))
79
+ r "aws ec2 create-key-pair --key-name {0} --profile {1} --output text > C:\KeyPairs\ {0}.pem" .format (keyPair , profile ))
74
80
# Creating Security-Group
75
81
errorSecurityGroup = subprocess .call (
76
82
'aws ec2 create-security-group --group-name {} --description "{}" --profile {}' .format (secGroup , descSecurity , profile ))
@@ -97,11 +103,57 @@ def keyAndSecurity():
97
103
return (keyPair , secGroup , rc )
98
104
99
105
106
+ # function to add inbound rules to security group
107
+ def inboundRules ():
108
+ sgname = input ("\n Enter security group name : " )
109
+ print ("1. All Traffic (Default) 2. TCP 3. UDP 4. SSH 5. Custom Protocol : " )
110
+ for i in range (5 ):
111
+ plChoice = input ("Choose between 1-5 : " )
112
+ if plChoice == '1' or plChoice == '' :
113
+ protocol = 'all'
114
+ port = 'all'
115
+ break
116
+ elif plChoice == '2' :
117
+ protocol = 'tcp'
118
+ port = '0-65535'
119
+ break
120
+ elif plChoice == '3' :
121
+ protocol = 'udp'
122
+ port = '0-65535'
123
+ break
124
+ elif plChoice == '4' :
125
+ protocol = 'tcp'
126
+ port = '22'
127
+ break
128
+ elif plChoice == '5' :
129
+ protocol = input ("Enter protocol number (Eg. 1 for ICMP, 6 for TCP etc.) : " )
130
+ port = input ("Enter port range (leave blank for all by default): " )
131
+ if port == '' :
132
+ port = 'all'
133
+ break
134
+ else :
135
+ if i == 4 :
136
+ print ("Entered wrong option 5 times. Exiting..." )
137
+ return
138
+ print ("Invalid choice!!! Choose from 1-5\n " )
139
+ cidr = input ("Enter custom cidr (leave blank for 0.0.0.0/0 by default) : " )
140
+ if cidr == '' :
141
+ cidr = '0.0.0.0/0'
142
+ confirm = input ("Confirm creation of inbound rule for Security Group : {} => for Protocol : {} \
143
+ Port : {} and CIDR : {} ? (Press Y to confirm) : " .format (sgname , protocol , port , cidr ))
144
+ if confirm == 'y' or confirm == 'Y' :
145
+ errorcheck = subprocess .call ("aws ec2 authorize-security-group-ingress --group-name {} --protocol {} \
146
+ --port {} --cidr {} --profile {}" .format (sgname , protocol , port , cidr , profile ))
147
+ print ("rc : {}" .format (errorcheck ))
148
+
149
+
150
+ # function to configure AWS user profile
100
151
def awsConfigure ():
101
152
global loginAWS
102
153
global profile
103
154
104
- print ("\n AWS Configure\n -----------\n " )
155
+ print ("\n AWS Configure\
156
+ \n -------------" )
105
157
106
158
name = input ("Enter name of profile : " )
107
159
print ("Region name for Mumbai AZ : ap-south-1 | Leave default output format blank for JSON" )
@@ -122,18 +174,59 @@ def awsMenu():
122
174
if not loginAWS :
123
175
awsConfigure ()
124
176
else :
125
- choice = input (
126
- "\n 1. Launch instance\t 2. AWS Configure\t 3. List Profiles\t 4. Exit\n > " )
177
+ print ("\n AWS MENU\
178
+ \n --------" )
179
+ print ("\n 1. Launch instance \t 2. Show Key-Pairs \t 3. Delete Key-Pair\
180
+ \n 4. Show Security-Groups\t 5. Delete Security-Group\t 6. Add Inbound Rules\
181
+ \n 7. Show Instances \t 8. Start Instance \t 9. Stop Instance\
182
+ \n 10.AWS Configure \t 11.List Profiles \t \
183
+ \n \n Press Q to exit" )
184
+ choice = input ("> " )
127
185
if choice == '1' :
128
186
launchInstance ()
129
187
elif choice == '2' :
130
- awsConfigure ()
188
+ os .system (
189
+ "aws ec2 describe-key-pairs --profile {}" .format (profile ))
131
190
elif choice == '3' :
132
- os .system ("aws configure list-profiles" )
191
+ keypair = input ("\n Enter name of key-pair to delete : " )
192
+ os .system (
193
+ "aws ec2 delete-key-pair --key-name {} --profile {}" .format (keypair , profile ))
194
+ print ("Key-Pair : {} deleted." .format (keypair ))
133
195
elif choice == '4' :
196
+ os .system (
197
+ "aws ec2 describe-security-groups --profile {}" .format (profile ))
198
+ elif choice == '5' :
199
+ sg = input ("\n Enter name of security-group to delete : " )
200
+ os .system (
201
+ "aws ec2 delete-security-group --group-name {} --profile {}" .format (sg , profile ))
202
+ print ("Security-Group : {} deleted." .format (sg ))
203
+ elif choice == '6' :
204
+ inboundRules ()
205
+ elif choice == '7' :
206
+ os .system ("aws ec2 describe-instances --profile {}" .format (profile ))
207
+ elif choice == '8' :
208
+ instance_id = input ("\n Enter instance-id : " )
209
+ error = subprocess .call ("aws ec2 start-instances --instance-ids {} \
210
+ --profile {}" .format (instance_id , profile ))
211
+ if error == '0' :
212
+ print ("Instance started successfully." )
213
+ print ("rc : {}" .format (error ))
214
+ elif choice == '9' :
215
+ instance_id = input ("\n Enter instance-id : " )
216
+ error = subprocess .call ("aws ec2 stop-instances --instance-ids {} \
217
+ --profile {}" .format (instance_id , profile ))
218
+ if error == '0' :
219
+ print ("Instance stopped successfully." )
220
+ print ("rc : {}" .format (error ))
221
+ elif choice == '10' :
222
+ awsConfigure ()
223
+ elif choice == '11' :
224
+ os .system ("aws configure list-profiles" )
225
+ elif choice == 'Q' or choice == 'q' :
226
+ print ("Exiting...\n " )
134
227
break
135
228
else :
136
- print ("Invalid choice!!! Choose from 1-3 \n " )
229
+ print ("Invalid choice!!! Choose from 1-11 or Q to exit. \n " )
137
230
return
138
231
139
232
@@ -156,11 +249,13 @@ def simml():
156
249
157
250
158
251
while True :
159
- print ("\n \t \t \t \t -------------------\n \t \t \t \t | TECH STACK MENU |\n \t \t \t \t -------------------\n " )
252
+ print ("\n \t \t \t \t -------------------\
253
+ \n \t \t \t \t | TECH STACK MENU |\
254
+ \n \t \t \t \t -------------------\n " )
160
255
161
256
# Add menu options based on your requirement or idea. These are just temporary for now.
162
257
# Based on added menu options, create functions too with additional elif statements.
163
- print ("1. Launch AWS Instance \t 2. Launch Hadoop Cluster \t 3. Launch Docker Containers \n 4. Simulate ML Model\n " )
258
+ print ("1. AWS CLI \t 2. Configure Hadoop\t 3. Configure Docker\n 4. Simulate ML Model\n " )
164
259
print ("Press Q to quit.\n " )
165
260
choice = input ("> " )
166
261
0 commit comments