Skip to content

Commit cf7f4f7

Browse files
committed
Complete AWS Automation for Instance Creation
1 parent 0a82edd commit cf7f4f7

File tree

1 file changed

+113
-18
lines changed

1 file changed

+113
-18
lines changed

menu.py

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,35 @@
77
loginAWS = False
88
profile = ""
99

10-
# function to launch AWS instances
11-
1210

11+
# function to launch AWS instances
1312
def launchInstance():
14-
1513
keyname, securityGroup, rc = keyAndSecurity()
1614

1715
if rc != 0:
1816
return
1917

2018
exitflag = 0
21-
imageID = "ami-0e306788ff2473ccb"
22-
print("\n1. Amazon Linux 2 AMI 64-bit (x86)\t2. RHEL 8 64-bit (x86)\t3. Custom Image ID (Default : Amazon Linux 2 AMI)")
19+
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")
2320
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':
2626
imageID = "ami-052c08d70def0ac62"
2727
break
2828
elif amiChoice == '3':
2929
imageID = input("Enter Image ID : ")
3030
break
31+
elif amiChoice == '4':
32+
print("Exiting...")
33+
return
3134
else:
3235
if i == 4:
3336
exitflag = 1
3437
break
35-
print("Invalid choice!!! Choose from 1-3\n")
38+
print("Invalid choice!!! Choose from 1-4\n")
3639
if exitflag == 1:
3740
print("Entered wrong option 5 times. Exiting...")
3841
return
@@ -46,11 +49,12 @@ def launchInstance():
4649
instanceID = input("Enter the instance ID : ")
4750
nametag = input("Enter name tag for instance (Eg. MyInstance): ")
4851
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))
5053
print("Instance is created and running.\n")
5154
return
5255

5356

57+
# function to create key-pair and security group for instance
5458
def keyAndSecurity():
5559
while True:
5660
print("\nDefault 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():
6973
confirm = input("Press Y to confirm : ")
7074
if(confirm == 'Y' or confirm == 'y'):
7175
# Creating Key-Pair
76+
os.system(r"mkdir C:\KeyPairs")
77+
print(r"{}.pem created in folder C:\KeyPairs\ ".format(keyPair))
7278
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))
7480
# Creating Security-Group
7581
errorSecurityGroup = subprocess.call(
7682
'aws ec2 create-security-group --group-name {} --description "{}" --profile {}'.format(secGroup, descSecurity, profile))
@@ -97,11 +103,57 @@ def keyAndSecurity():
97103
return (keyPair, secGroup, rc)
98104

99105

106+
# function to add inbound rules to security group
107+
def inboundRules():
108+
sgname = input("\nEnter 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
100151
def awsConfigure():
101152
global loginAWS
102153
global profile
103154

104-
print("\nAWS Configure\n-----------\n")
155+
print("\nAWS Configure\
156+
\n-------------")
105157

106158
name = input("Enter name of profile : ")
107159
print("Region name for Mumbai AZ : ap-south-1 | Leave default output format blank for JSON")
@@ -122,18 +174,59 @@ def awsMenu():
122174
if not loginAWS:
123175
awsConfigure()
124176
else:
125-
choice = input(
126-
"\n1. Launch instance\t2. AWS Configure\t3. List Profiles\t4. Exit\n> ")
177+
print("\nAWS MENU\
178+
\n--------")
179+
print("\n1. Launch instance \t2. Show Key-Pairs \t3. Delete Key-Pair\
180+
\n4. Show Security-Groups\t5. Delete Security-Group\t6. Add Inbound Rules\
181+
\n7. Show Instances \t8. Start Instance \t9. Stop Instance\
182+
\n10.AWS Configure \t11.List Profiles \t\
183+
\n\nPress Q to exit")
184+
choice = input("> ")
127185
if choice == '1':
128186
launchInstance()
129187
elif choice == '2':
130-
awsConfigure()
188+
os.system(
189+
"aws ec2 describe-key-pairs --profile {}".format(profile))
131190
elif choice == '3':
132-
os.system("aws configure list-profiles")
191+
keypair = input("\nEnter 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))
133195
elif choice == '4':
196+
os.system(
197+
"aws ec2 describe-security-groups --profile {}".format(profile))
198+
elif choice == '5':
199+
sg = input("\nEnter 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("\nEnter 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("\nEnter 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")
134227
break
135228
else:
136-
print("Invalid choice!!! Choose from 1-3\n")
229+
print("Invalid choice!!! Choose from 1-11 or Q to exit.\n")
137230
return
138231

139232

@@ -156,11 +249,13 @@ def simml():
156249

157250

158251
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")
160255

161256
# Add menu options based on your requirement or idea. These are just temporary for now.
162257
# Based on added menu options, create functions too with additional elif statements.
163-
print("1. Launch AWS Instance\t 2. Launch Hadoop Cluster\t3. Launch Docker Containers\n4. Simulate ML Model\n")
258+
print("1. AWS CLI\t 2. Configure Hadoop\t3. Configure Docker\n4. Simulate ML Model\n")
164259
print("Press Q to quit.\n")
165260
choice = input("> ")
166261

0 commit comments

Comments
 (0)