in src/main/python/QueryResultsHandler/query_results_handler.py [0:0]
def lambda_handler(event, context):
LOGGER.debug(event)
current_query_state = event['detail']['currentState']
if current_query_state == 'FAILED':
raise RuntimeError('Athena Query is {}'.format(current_query_state))
if current_query_state != 'SUCCEEDED':
#TODO: send alert by sns
LOGGER.info('athena query state: %s' % current_query_state)
return
query_execution_id = event['detail']['queryExecutionId']
output_location = get_athena_query_result_location(query_execution_id)
LOGGER.info(output_location)
url_parse_result = urlparse(output_location, scheme='s3')
bucket_name, object_name = url_parse_result.netloc, url_parse_result.path.lstrip('/')
presigned_url = create_presigned_url(bucket_name, object_name, expiration=DOWNLOAD_URL_TTL)
LOGGER.info('presigned_url: %s' % presigned_url)
try:
record = get_user_id_by_query_id(DDB_TABLE_NAME, query_execution_id)
except Exception as ex:
raise ex
else:
# send email to requester
record['link'] = presigned_url
user_id = record.get('user_id', EMAIL_FROM_ADDRESS)
html = gen_html(record)
subject = '''Athena Query Results is ready'''
send_email(EMAIL_FROM_ADDRESS, [user_id], subject, html)
try:
update_query_status(DDB_TABLE_NAME, user_id, query_execution_id, current_query_state)
except Exception as ex:
LOGGER.error(ex)
LOGGER.info("end")