-
Notifications
You must be signed in to change notification settings - Fork 2
/
report_lambda_all_profiles.py
43 lines (37 loc) · 1.53 KB
/
report_lambda_all_profiles.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
"""
This tool generates a csv report of lambda functions in all available profiles.
The report contains the following columns profile,function_name,runtime
``Example``
$python3 report_lambda_all_profiles.py
© Moataz ElQadi, 2022
"""
import boto3
#get all available profiles
profiles = boto3.session.Session().available_profiles
#select the readonly profiles
readOnlyProfiles = [prof for prof in profiles if 'administrator' in prof]
#create the first line in the result csv, containing the header
result = ['profile,function_name,runtime\n']
for profile in readOnlyProfiles:
try:
print(profile)
#get a session for the profile
session = boto3.Session(profile_name=profile)
#get a low-level client for lambda api
lam = session.client('lambda')
#call list_functions (which returns only 50 results/page)
pages = lam.get_paginator('list_functions').paginate()
#get a list of pages, each page contains a list of functions
pages = list(pages)
#fns is a "flat list" of all functions in all pages in the profile
fns=[]
for page in pages:
fns.extend(page['Functions'])
#create the csv lines corresponding
profile_records = ["{},{},{}\n".format(profile,fn['FunctionName'],fn['Runtime']) for fn in fns]
result.extend(profile_records)
except:
print("-------error in {}".format(profile))
#write to file
with open('lambda_all_profiles.csv','w') as f:
f.writelines(result)