in sample-apps/data-processor/function/lambda_function.py [0:0]
def process_events(end_time, service):
start_time = end_time - timedelta(minutes=60)
records = []
# call CloudTrail
responses = get_events(service, start_time, end_time)
records = [{k: event.get(k) for k in fields} for response in responses for event in response.get('Events')]
# format timeseries dataframe
df = pd.DataFrame(records)
if len(df) == 0:
return '404 no events found for service {}'.format(service)
df['EventTime'] = pd.to_datetime(df['EventTime'], utc=True)
timeseries = df.set_index('EventTime')
timeseries.sort_index(inplace=True)
# add measure column
timeseries['Calls'] = 1
# save and upload
now = pd.Timestamp.utcnow()
report_file = '{}-timeseries-{}.csv'.format(service,now.strftime(ymdhms))
report_path = '{}/{}'.format(dir,report_file)
report_key = 'timeseries/{}/{}/{}'.format(start_time.strftime(ymd), start_time.strftime(hm), report_file)
timeseries['Date'] = timeseries.index.strftime(ymdhms)
timeseries.fillna('none').to_csv(report_path, index_label='EventTime')
bucket = os.environ.get('bucket')
if bucket:
upload(bucket, report_key, report_path)
return 's3://{}/{}'.format(bucket,report_key)
return '200 success'