def lambda_handler()

in apps/cloudwatch-dashboard/lambdas/process-snapshot/lambda_function.py [0:0]


def lambda_handler(event, context):
    print(json.dumps(event))
    
    # Get the object bucket and key from the event:
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(
        event['Records'][0]['s3']['object']['key'], 
        encoding='utf-8'
    )

    dashboard_name = key.split('/')[-1]
    dashboard_type = dashboard_name.split('-')[-2]
    model_name = None
    scheduler_name = None
    if dashboard_type == 'Scheduler':
        scheduler_name = '-'.join(dashboard_name[38:].split('-')[:-2])
    elif dashboard_type == 'ModelEvaluation':
        model_name = '-'.join(dashboard_name[34:].split('-')[:-2])
        
    snapshot_fname = os.path.join('/tmp', dashboard_name)
    s3_client.download_file(bucket, key, snapshot_fname)
    with open(snapshot_fname, 'rb') as f:
        snapshot_content = f.read()
    
    email_snapshot(
        scheduler_name=scheduler_name,
        model_name=model_name,
        email=os.getenv('TargetEmail'),
        ses_region=os.getenv('SESRegion'),
        snapshot_content=snapshot_content
    )
    
    return {
        'statusCode': 200
    }