-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_dump.py
More file actions
80 lines (55 loc) · 2.26 KB
/
lambda_dump.py
File metadata and controls
80 lines (55 loc) · 2.26 KB
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
import os
import json
import boto3
from utils.session import get_session
from utils.regions import get_all_regions
from utils.json_writer import json_writer
from utils.json_printer import json_printer
from utils.boto_error_handling import yield_handling_errors
def get_lambda_functions_for_region(client):
for lambda_function in client.list_functions()['Functions']:
yield lambda_function
def get_function(client, function_name):
try:
function_details = client.get_function(FunctionName=function_name)
except Exception as e:
msg = 'Failed to retrieve function details for %s. Error: "%s"'
args = (function_name, e)
print(msg % args)
function_details = {}
return function_details
def get_policy(client, function_name):
try:
function_policy = client.get_policy(FunctionName=function_name)
except Exception as e:
msg = 'Failed to retrieve function policy for %s. Error: "%s"'
args = (function_name, e)
print(msg % args)
function_policy = {}
else:
function_policy = json.loads(function_policy['Policy'])
return function_policy
def main():
session = get_session()
all_data = {}
for region in get_all_regions(session):
all_data[region] = {}
client = session.client('lambda', region_name=region)
iterator = yield_handling_errors(get_lambda_functions_for_region, client)
for lambda_function in iterator:
function_name = lambda_function['FunctionName']
print('Region: %s / Lambda function: %s' % (region, function_name))
function_details = get_function(client, function_name)
function_policy = get_policy(client, function_name)
all_data[region][function_name] = {}
all_data[region][function_name]['main'] = lambda_function
all_data[region][function_name]['details'] = function_details
all_data[region][function_name]['policy'] = function_policy
if not all_data[region]:
print('Region %s / No Lambda functions' % region)
continue
os.makedirs('output', exist_ok=True)
json_writer('output/lambda-functions.json', all_data)
json_printer(all_data)
if __name__ == '__main__':
main()