io import json def lambda_handler(event, context): records = [process_record(r) for r in event['records']] return {'records': records } def process_record(record): record_id = record['recordId'] data = base64.b64decode(record['data']) iodata = io.BytesIO(data) with gzip.GzipFile(fileobj=iodata, mode='r') as f: data = json.loads(f.read()) processed_data = process_data(data) + '\n' return { 'data': base64.b64encode(processed_data.encode('utf-8')).decode('utf-8'), 'result': 'Ok', 'recordId': record_id } def process_data(data): return '\n'.join([format_log_event(json) for json in data['logEvents']]) def format_log_event(j): return json.dumps({'timestamp': j['timestamp'], 'message': j['message']})