-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlambda_function.py
79 lines (59 loc) · 2.31 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import boto3
from time import sleep
# Initialize the Athena client
athena_client = boto3.client('athena')
def lambda_handler(event, context):
print(event)
def athena_query_handler(event):
# Fetch parameters for the new fields
# Extracting the SQL query
query = event['requestBody']['content']['application/json']['properties'][0]['value']
print("the received QUERY:", query)
s3_output = 's3://athena-destination-store-alias' # Replace with your S3 bucket
# Execute the query and wait for completion
execution_id = execute_athena_query(query, s3_output)
result = get_query_results(execution_id)
return result
def execute_athena_query(query, s3_output):
response = athena_client.start_query_execution(
QueryString=query,
ResultConfiguration={'OutputLocation': s3_output}
)
return response['QueryExecutionId']
def check_query_status(execution_id):
response = athena_client.get_query_execution(QueryExecutionId=execution_id)
return response['QueryExecution']['Status']['State']
def get_query_results(execution_id):
while True:
status = check_query_status(execution_id)
if status in ['SUCCEEDED', 'FAILED', 'CANCELLED']:
break
sleep(1) # Polling interval
if status == 'SUCCEEDED':
return athena_client.get_query_results(QueryExecutionId=execution_id)
else:
raise Exception(f"Query failed with status '{status}'")
action_group = event.get('actionGroup')
api_path = event.get('apiPath')
print("api_path: ", api_path)
result = ''
response_code = 200
if api_path == '/athenaQuery':
result = athena_query_handler(event)
else:
response_code = 404
result = {"error": f"Unrecognized api path: {action_group}::{api_path}"}
response_body = {
'application/json': {
'body': result
}
}
action_response = {
'actionGroup': action_group,
'apiPath': api_path,
'httpMethod': event.get('httpMethod'),
'httpStatusCode': response_code,
'responseBody': response_body
}
api_response = {'messageVersion': '1.0', 'response': action_response}
return api_response