-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_lambda_explorer.py
388 lines (305 loc) · 15.3 KB
/
aws_lambda_explorer.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
'''
Useful for exploring lambda functions in different regions for those using Windows OS.
The application is build using PySimpleGUI.
It expects you have setup the aws id/key in
Linux: /home/[username]/.aws
Windows: /Users/[username]/.aws
To do:
1:
'''
import PySimpleGUI as sg
import boto3
from botocore.config import Config
from boto3.session import Session
import threading
import time
#import datetime
from datetime import datetime,timedelta
import sys
import pandas as pd
import time
import requests
session = boto3.session.Session()
lambda_func_list_data=[]
lambda_func_data=[]
global rate_code_dataframe
sg.theme('Reddit')
#-----------------GUI Layout--------------------------------
Console =[
[sg.Text("Console")],
[sg.Multiline(size=(60, 14),key="-CONSOLEMSG-",disabled=True)],
[sg.B("Clear Console",size=(27, 1)),sg.B("Save Console",size=(25, 1))]
]
frame_layout = [
[sg.T('FunctionName:'), sg.Text("",size=(60, 1),key="-text_funcname-")],
[sg.T('Description:'), sg.Text("",size=(60, 1),key="-text_desc-")],
[sg.T('CodeSha256:'), sg.Text("",size=(60, 1),key="-text_codesha-")],
[sg.T('Mode:'), sg.Text("",size=(46, 1),key="-text_mode-"),sg.T(' Runtime:'), sg.Text("",size=(13, 1),key="-text_runtime-")],
[sg.T('CodeSize (B):'), sg.Text("",size=(41, 1),key="-text_codesize-"),sg.T('MemorySize (MB):'), sg.Text("",size=(13, 1),key="-text_memorysize-")],
[sg.T('LastModified:'), sg.Text("",size=(41, 1),key="-text_lastmodifiedtime-"),sg.T(' Timeout (Sec):'), sg.Text("",size=(13, 1),key="-text_timeout-")],
[sg.T('State:'), sg.Text("",size=(47, 1),key="-text_state-"),sg.T('LastUpdateStatus:'), sg.Text("",size=(13, 1),key="-text_updatestatus-")],
[sg.T('Storage (MB):'), sg.Text("",size=(41, 1),key="-text_storage-"),sg.T('RepositoryType:'), sg.Text("",size=(13, 1),key="-text_repotype-")],
[sg.T('RevisionId:'), sg.Text("",size=(43, 1),key="-text_revid-"),sg.T(' Architecture:'), sg.Text("",size=(13, 1),key="-text_arch-")]
]
Desc =[
[sg.Frame('Description', frame_layout, font='Any 12', title_color='blue')]
]
cost_expl=[]
funct_list =[
[sg.Table(values=lambda_func_list_data,key="_list_", headings=['Function Name', 'Package Type','Runtime','Last modified'],auto_size_columns=False, col_widths=[50, 10, 10,27], num_rows=15,justification='left',enable_events=True )]
]
Region = [
[sg.Text("Region Name (Select Region)")],
[sg.Listbox(values=[],enable_events=True,size=(32, 12), key="-REGION-")],
[sg.B("List Regions",size=(30, 1))]
]
lambda_layout = [
[
sg.Column(Region,size=(255, 270)), sg.Column(funct_list)],
[sg.TabGroup(
[[sg.Tab('Function Detail', Desc)],
[sg.Tab('Cost Explorer', cost_expl)]])
# sg.Column(Desc,size=(700, 270))
,sg.VSeperator(),
sg.Column(Console)
]
]
config =[
[sg.Text('Enter Your AWS Id',size=(30, 1)), sg.InputText(key="-AWSID-",size=(30, 1))],
[sg.Text('Enter Your AWS Key',size=(30, 1)), sg.InputText(key="-AWSKEY-",size=(30, 1))],
[sg.Text('Enter Your Default Region',size=(30, 1)), sg.InputText(key="-DEFREGION-",size=(30, 1))],
[sg.B("Reset",size=(28, 1)),sg.B("Connect",size=(27, 1))]
]
config_layout = [[sg.Column(config)]]
tabgrp = [[sg.TabGroup([[sg.Tab('Config', config_layout)],[sg.Tab('Lambda', lambda_layout)]])]]
#---------- Lambda functions----------------------
def lambda_function_worker_thread(region_name, window):
try:
data=[]
data = get_lambda_function_list(region_name,window)
window["_list_"].update(data)
except Exception as e:
window.write_event_value('-WRITE-',e)
def lambda_detail_worker_thread(region_name, function_name, window):
try:
describe_lambda_functions(region_name, function_name, window)
except Exception as e:
window.write_event_value('-WRITE-',e)
def get_lambda_function_list(REGION_NAME,window):
REGION_CONFIG = Config(
region_name = REGION_NAME,
signature_version = 'v4',
retries = {
'max_attempts': 3
}
)
try:
CLIENT = session.client('lambda', config=REGION_CONFIG)
response = CLIENT.list_functions()
lambda_func_list_data.clear()
if not 'Functions' in response or len(response['Functions']) == 0:
set_lambda_detail(window)
window.write_event_value('-WRITE-',"There is no lambda function in this region")
else:
for function in response['Functions']:
lambda_func_list_data.append([function['FunctionName'],
function['PackageType'],
function['Runtime'],
function['LastModified']])
return lambda_func_list_data
except Exception as e:
window.write_event_value('-WRITE-',e)
#get all the AWS regions
def get_lambda_regions():
lambda_region = session.get_available_regions('lambda')
return (lambda_region)
def set_lambda_detail(window):
try:
window["-text_funcname-"].update("")
window["-text_runtime-"].update("")
window["-text_codesize-"].update("")
window["-text_memorysize-"].update("")
window["-text_lastmodifiedtime-"].update("")
window["-text_timeout-"].update("")
window["-text_state-"].update("")
window["-text_updatestatus-"].update("")
window["-text_arch-"].update("")
window["-text_repotype-"].update("")
window["-text_desc-"].update("")
window["-text_mode-"].update("")
window["-text_codesha-"].update("")
window["-text_revid-"].update("")
window["-text_storage-"].update("")
except Exception as e:
window.write_event_value('-WRITE-',e)
def describe_lambda_functions(REGION_NAME, function_name, window):
REGION_CONFIG = Config(
region_name = REGION_NAME,
signature_version = 'v4',
retries = {
'max_attempts': 3
}
)
try:
CLIENT = session.client('lambda', config=REGION_CONFIG)
response = CLIENT.get_function(FunctionName=function_name)
lambda_func_data.append([response['Configuration']['FunctionName'],
response['Configuration']['Runtime'],
response['Configuration']['CodeSize'],
response['Configuration']['Description'],
response['Configuration']['Timeout'],
response['Configuration']['MemorySize'],
response['Configuration']['LastModified'],
response['Configuration']['CodeSha256'],
response['Configuration']['TracingConfig']['Mode'],
response['Configuration']['State'],
response['Configuration']['LastUpdateStatus'],
#response['Configuration']['PackageType'],
response['Configuration']['RevisionId'],
response['Code']['RepositoryType'],
response['Configuration']['Architectures'],
response['Configuration']['EphemeralStorage']['Size']
]
)
window["-text_funcname-"].update(response['Configuration']['FunctionName'])
window["-text_runtime-"].update(response['Configuration']['Runtime'])
window["-text_codesize-"].update(response['Configuration']['CodeSize'])
window["-text_memorysize-"].update(response['Configuration']['MemorySize'])
window["-text_lastmodifiedtime-"].update(response['Configuration']['LastModified'])
window["-text_timeout-"].update(response['Configuration']['Timeout'])
window["-text_state-"].update(response['Configuration']['State'])
window["-text_updatestatus-"].update(response['Configuration']['LastUpdateStatus'])
#window["-text_packagetype-"].update(response['Configuration']['PackageType'])
window["-text_repotype-"].update(response['Code']['RepositoryType'])
window["-text_desc-"].update(response['Configuration']['Description'])
window["-text_mode-"].update(response['Configuration']['TracingConfig']['Mode'])
window["-text_codesha-"].update(response['Configuration']['CodeSha256'])
window["-text_revid-"].update(response['Configuration']['RevisionId'])
window["-text_arch-"].update(response['Configuration']['Architectures'])
window["-text_storage-"].update(response['Configuration']['EphemeralStorage']['Size'])
except Exception as e:
window.write_event_value('-WRITE-',e)
#get lamaba rate by region
def get_rates_for_all_regions(window):
try:
t = time.time()
t_ms = int(t * 1000)
#Get price for each lambda parameter by region name
headers = {"Accept-Encoding": "gzip, deflate, br",
"Origin": "https://aws.amazon.com",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"Cache-Control": "no-cache",
"Sec-Fetch-Dest": "empty",
}
lambda_resp = requests.get('https://b0.p.awsstatic.com/pricing/2.0/meteredUnitMaps/lambda/USD/current/lambda.json?timestamp={value}'.format(value=t_ms), headers=headers)
lambda_resp_dict = lambda_resp.json()
#from json only extract region name, lambda parameter and price
price_list = []
for key, val in lambda_resp_dict['regions'].items():
for key1, val1 in val.items():
price_list.append([key, key1, val1['price']])
#Get region code
headers = {"Accept-Encoding": "gzip, deflate, br",
"Origin": "https://aws.amazon.com",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"Cache-Control": "no-cache",
"Sec-Fetch-Dest": "empty",
}
location_resp = requests.get('https://b0.p.awsstatic.com/locations/1.0/aws/current/locations.json?timestamp={value}'.format(value=t_ms), headers=headers)
location_response_dict = location_resp.json()
region_list =[]
for key, val in location_response_dict.items():
region_list.append([key,val['code']])
#convert list to dataframe
df = pd.DataFrame (price_list, columns = ['Region Name', 'Lambda Type', 'Price'])
df2 = pd.DataFrame (region_list, columns = ['Region Name', 'RegionCode'])
'''merge dataframe so we now have region name, lambda parameter, price and region code.
We will need this to calculate the cost of lambda function in a region.
'''
rate_code_dataframe = df.merge(df2)
return rate_code_dataframe
except Exception as e:
window.write_event_value('-WRITE-',e)
def rate_worker_thread(window):
try:
rate_code_dataframe = get_rates_for_all_regions(window)
rate_code_dataframe.to_csv('./output.csv',index=False)
except Exception as e:
window.write_event_value('-WRITE-',e)
#-----------------Main function------------------------------------
def main():
window = sg.Window('AWS Lambda Explorer', tabgrp) #layout
region_loop = False
threading.Thread(target=rate_worker_thread,
args=(window,), daemon=True).start()
while True: # The Event Loop
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
#---------Connection Tab-----------------------------
if event == 'Reset':
try:
window["-AWSID-"].update("")
window["-AWSKEY-"].update("")
window["-DEFREGION-"].update("")
window["-AWSID-"].SetFocus(force = True)
except Exception as e:
sg.popup(e)
if event == 'Connect':
try:
global session
if values['-DEFREGION-'] == "":
sg.popup("Region Field is missing")
elif values['-AWSID-'] == "":
sg.popup("AWS ID Field is missing")
elif values['-AWSKEY-'] == "":
sg.popup("AWS KEY Field is missing")
else:
session = Session(region_name=values['-DEFREGION-'], aws_access_key_id=values['-AWSID-'],
aws_secret_access_key=values['-AWSKEY-'])
#need to validate if connection is successful or not
sg.popup("Connection Established")
except Exception as e:
sg.popup(e)
#--------------Lambda Tab-------------------------
if event == 'List Regions':
try:
if region_loop == False: #don't refresh list everytime
region_list = get_lambda_regions()
window["-REGION-"].update(region_list)
region_loop = True
except Exception as e:
window["-CONSOLEMSG-"].update(str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) +": "+str(e)+"\n", append=True )
if event == '-REGION-':
try:
threading.Thread(target=lambda_function_worker_thread,
args=(values['-REGION-'][0],
window,), daemon=True).start()
except Exception as e:
window["-CONSOLEMSG-"].update(str(datetime.datetime.now().strftime(
"%Y-%m-%d %H:%M:%S")) + ": "+str(e)+"\n", append=True)
if event == '-WRITE-':
window["-CONSOLEMSG-"].update(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) +": "+str(values['-WRITE-'])+"\n", append=True)
if event == "_list_":
try:
data_selected = [lambda_func_list_data[row] for row in values[event]]
threading.Thread(target=lambda_detail_worker_thread,
args=(values['-REGION-'][0],data_selected[0][0],window,),
daemon=True).start()
except Exception as e:
window["-CONSOLEMSG-"].update(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) +": "+str(e)+"\n", append=True )
if event == 'Save Console':
try:
file= open(".\output.txt", 'a+')
except FileNotFoundError:
file= open(".\output.txt", 'w+')
file.write(str(window["-CONSOLEMSG-"].get()))
file.close()
sg.popup("File Saved")
if event == 'Clear Console':
window["-CONSOLEMSG-"].update("")
window.close()
if __name__ == '__main__':
main()