-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.py
305 lines (225 loc) · 9.39 KB
/
aws.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import subprocess as sp
import os
def iamuser(name):
a = sp.getstatusoutput('aws iam create-user --user-name {}'.format(name))
if a[0] != 0:
print("Can't Create the User : {}".format(a[1]))
return
print("Iam user {} created Successfully".format(name))
def s3bucket(name):
a = sp.getstatusoutput("aws s3 mb s3://{}".format(name))
if a[0] != 0:
print("Can't Create the Bucket : {}".format(a[1]))
return
print("Bucket {} created Successfully".format(name))
def s3list():
a = sp.getstatusoutput("aws s3 ls")
if a[0] != 0:
print("Can't List Buckets : {}".format(a[1]))
return
print("Available Buckets :- \n{}".format(a[1]))
def s3object(path, bucket, public=True):
if public:
a = sp.getstatusoutput("aws s3 cp --acl public-read {} s3://{}".format(path, bucket))
else :
a = sp.getstatusoutput("aws s3 cp {} s3://{}".format(path, bucket))
if a[0] != 0:
print("Can't Upload The Object : {}".format(a[1]))
return
print("Object {} Uploaded Successfully To The Bucket {}".format(path, bucket))
def cloudfront(origin):
a = sp.getstatusoutput("aws cloudfront create-distribution --origin-domain-name {}.s3.apsouth1.amazonaws.com".format(origin))
if a[0] != 0:
print("\nCan't Create The CDN : {}".format(a[1]))
return
print("\nCDN Created Successfully With Origin {}".format(origin))
def ec2launch(ami, keypair, sg, count=1, instance="t2.micro", sid=""):
if sid == "":
a = sp.getstatusoutput("aws ec2 run-instances --image {} --instance-type {} --key-name {} --security-group-ids {} --count {}".format(ami, instance, keypair, sg, count))
else :
a = sp.getstatusoutput("aws ec2 run-instances --image {} --instance-type {} --key-name {} --security-group-ids {} --count {} --subnet-id {}".format(ami, instance, keypair, sg, count, sid))
if a[0] != 0:
print("Can't Launch the instance: {}".format(a[1]))
return
print("Instance Launched Succesfully!!")
def viewinstances():
a = sp.getstatusoutput('aws ec2 describe-instances')
print("Instance Status : {}".format(a[1]))
def stopec2(id):
a = sp.getstatusoutput("aws ec2 stop-instances --instance-ids {}".format(id))
if a[0] != 0:
print("Can't Stop the instance: {}".format(a[1]))
return
print("Instance Stopped Succesfully!!")
def configure():
try:
os.system("aws configure")
except KeyboardInterrupt :
print("User Interrupt Detected Aborting... ")
return
def switch():
cur = sp.getoutput('aws configure get default.region')
new = input('\nEnter the code for New Region (Current : {}) : '.format(cur))
if new == '':
print('\nNo Input Provided Defaulting to {} ..'.format(cur))
return
b = sp.getstatusoutput('aws configure set default.region {}'.format(new))
a = sp.getstatusoutput('aws ec2 describe-instances')
if a[0] != 0 or b[0] != 0:
print('\nRegion {} Does Not Exist..'.format(new))
sp.run('aws configure set default.region {}'.format(cur))
return
print('\nRegion Changed to {} Successfully ! '.format(new))
def ebslaunch(az="ap-south-1a", size=1):
a = sp.getstatusoutput("aws ec2 create-volume --availability-zone {} --size {}".format(az, size))
if a[0] != 0:
print("Can't Launch the Volume: {}".format(a[1]))
return
print("Ebs Volume Launched Succesfully!!")
def attach(vol_id, ins_id):
a = sp.getstatusoutput("aws ec2 attach-volume --instance-id {} --volume-id {} --device /dev/sdb".format(ins_id, vol_id))
if a[0] != 0:
print("Can't Attach the Volume: {}".format(a[1]))
return
print("Ebs Volume Attached Succesfully!!")
def keypair(name):
a = sp.getstatusoutput("aws ec2 create-key-pair --key-name {}".format(name))
if a[0] != 0:
print("Can't Create the Keyfile: {}".format(a[1]))
return
print("Key pair created successfully, {}".format(a[1]))
def security(name, desc):
a = sp.getstatusoutput('aws ec2 create-security-group --group-name {} --description "{}"'.format(name, desc))
if a[0] != 0:
print("\nCan't Create Security Group : {}".format(a[1]))
return
print('\nSecurity Group {} created Successfully!'.format(name))
def prereq():
a = sp.getstatusoutput('aws help')
if a[0] != 0:
print('\nOops Looks like Aws Cli Command is Not Available...')
exit()
def snap(vol, desc):
a = sp.getstatusoutput('aws ec2 create-snapshot --volume-id {} --description "{}"'.format(vol, desc))
if a[0] != 0:
print("\nCan't Create Snap : {} ".format(a[1]))
return
print('\nSnapshot Created Successfully!')
prereq()
ami = ('ami-0e306788ff2473ccb', ' ami-052c08d70def0ac62', 'ami-0cda377a1b884a1bc')
col, lines = os.get_terminal_size()
welcome = "-----Welcome To AWS Automation Service-----".center(col)
while True :
os.system('cls')
print(welcome)
print("""\n\n 1) Configure Aws
2) Switch Region
3) Launch an Ec2 Instance
4) View All Ec2 Instances
5) Stop An Instance
6) Launch an Ebs Volume
7) Attach Volume To an Instance
8) Create Snapshot
9) Create a s3 Bucket
10) List All s3 buckets
11) Upload An Object To a Bucket
12) Create a CDN Using Cloudfront
13) Create a KeyPair
14) Create a new Iam User
15) Create a Security Group
16) Quit\n""")
try:
choice = int(input("Please Choose an Option To Continue : "))
except ValueError:
print("\n\nwhoops Please Enter the Choice in Number, for eg. 2 for Launching Ec2 Instance.. ")
input('\nPress Enter to continue..')
continue
if choice == 1:
configure()
input('\nPress Enter to Continue')
elif choice == 2:
switch()
input('\nPress Enter To Continue..')
elif choice == 3:
img = int(input("""\n Please Choose Your os\n
1) Amazon-Linux-2
2) RHEL8
3) Ubuntu\n\nYour Choice : """))
key = input("\nEnter Your KeyPair Name : ")
sg = input("\nEnter Security Group : ")
inp = input('\nDo You Wish To use Advanced Options ?(y/n) : ')
if inp == 'y'.casefold():
subnetid = ''
inst = 't2.micro'
count = input('\nEnter No. Of Instances You Want : ')
instance = input('\nEnter Desired Instance Type (t2.micro by default) : ')
sid = input('\nEnter a Subnet id (press Enter for default Subnet) : ')
if sid != '':
subnetid = sid
if instance != '':
inst = instance
ec2launch(ami[img], key, sg, count, inst, subnetid)
else:
ec2launch(ami[img], key, sg)
input('\nPress Enter To Continue..')
elif choice == 4:
viewinstances()
input('\nPress Enter To Continue..')
elif choice == 5:
id = input('\nPlease Enter The Id of The Instance You Wish To Stop : ')
stopec2(id)
input('\nPress Enter To Continue..')
elif choice == 6:
az = input('\nName Of The Availability Zone to launch the instance in : ')
size = int(input('\nSize Of The Volume in GB : '))
ebslaunch(az, size)
input('\nPress Enter To Continue..')
elif choice == 7:
vol = input('\nEnter The Volume ID : ')
id = input('\nEnter The Instance ID : ')
attach(vol, id)
input('\nPress Enter To Continue..')
elif choice == 8:
vol = input('\nEnter The Volume ID : ')
desc = input('\nEnter Description For This Snapshot : ')
snap(vol, desc)
input('\nPress Enter To Continue..')
elif choice == 9:
name = input('\nEnter a Globally Unique Bucket Name : ')
s3bucket(name)
input('\nPress Enter To Continue..')
elif choice == 10:
s3list()
input('\nPress Enter To Continue..')
elif choice == 11:
path = input('\nEnter Complete Path To The Object : ')
bucket = input('\nEnter Name of The Bucket : ')
b = input('\nDo You want To Make The Object Public? (y/n) : ')
if b == 'y'.casefold() :
s3object(path, bucket)
else:
s3object(path, bucket, False)
input('\nPress Enter To Continue..')
elif choice == 12:
s3list()
origin = input('\nEnter the bucket You want to make origin : ')
cloudfront(origin)
input('\nPress Enter To Continue..')
elif choice == 13:
name = input('\nEnter The Name Of The Keypair : ')
keypair(name)
input('\nPress Enter To Continue..')
elif choice == 14:
name = input('\nEnter The Name of Iam User : ')
iamuser(name)
input('\nPress Enter To Continue..')
elif choice == 15:
nm = input('\nEnter Name Of Security Group : ')
desc = input('\nEnter a Description For The Security Group : ')
security(nm, desc)
input('\nPress Enter To Continue..')
elif choice == 16:
print('THANKS FOR USING THIS SERVICE'.center(col))
input('\n\nPress Enter To Exit..')
os.system('cls')
break