MCPcopy Create free account
hub / github.com/aws-samples/serverless-samples / lambda_handler

Function lambda_handler

apigw-log-analytic/src/app.py:45–98  ·  view source on GitHub ↗
(event, context)

Source from the content-addressed store, hash-verified

43
44
45def lambda_handler(event, context):
46
47 print(event)
48 output = []
49
50 for record in event['records']:
51 print(record['recordId'])
52 # Decode from base64 to binary
53 binary_payload = base64.b64decode(record['data'])
54 # print("Base64 decoded payload is " + str(payload))
55
56 # Decode binary to string and convert to dictionary
57 payload_dict = ast.literal_eval(binary_payload.decode("UTF-8"))
58 # Add customer name to payload via API Key Id lookup
59
60 print(payload_dict)
61
62 # Add customer name to payload via API Key Id lookup if key exists
63 if 'identityApiKeyId' in payload_dict:
64 payload_dict['identity.apiKeyName'] = customer_mapping.get(payload_dict['identityApiKeyId'], '-')
65 usage_plan_lookup = payload_dict['apiId'] + ':' + payload_dict['stage'] + ":" + payload_dict['identityApiKeyId']
66 payload_dict['identity.usagePlanName'] = usage_plan_mapping.get(usage_plan_lookup, '-')
67 else:
68 payload_dict['identity.apiKeyName'] = '-'
69
70
71
72
73 # Input timestamp string
74 requestTime = payload_dict['requestTime']
75 datetime_obj = datetime.strptime(requestTime, "%d/%b/%Y:%H:%M:%S %z")
76
77 # Convert it to the desired format
78 formatted_date = datetime_obj.strftime("%Y-%m-%dT%H:%M:%S")
79 payload_dict['requestTime'] = formatted_date
80
81
82
83 # Encode back to binary, adding a newline to the end of the string
84 print("Updated dict payload is " + str(payload_dict))
85 new_payload_string = json.dumps(payload_dict) + '\n'
86 new_payload_binary = new_payload_string.encode('utf-8')
87
88 #print(new_payload_binary)
89 output_record = {
90 'recordId': record['recordId'],
91 'result': 'Ok',
92 'data': base64.b64encode(new_payload_binary)
93 }
94 output.append(output_record)
95
96 print('Successfully processed {} records.'.format(len(event['records'])))
97
98 return {'records': output}
99
100
101

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected