forked from ayush9818/Optiver-Trading-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.py
46 lines (36 loc) · 1.46 KB
/
lambda.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
import json
import base64
import urllib3
import os
# Initialize the PoolManager for making HTTP requests
http = urllib3.PoolManager()
def lambda_handler(event, context):
"""
AWS Lambda handler function to process Kinesis stream records and send them to an ingest API.
Args:
event (dict): The event data containing records from the Kinesis stream.
context (object): The context in which the Lambda function is called.
Returns:
dict: A response dictionary with status code and message.
"""
for record in event["Records"]:
# Decode the base64 encoded Kinesis data
payload = base64.b64decode(record["kinesis"]["data"]).decode("utf-8")
data = json.loads(payload)
print(f"Decoded payload: {data}")
# Get the ingest API URL from environment variables
ingest_api_url = os.getenv("INGEST_API_URL")
print(ingest_api_url)
headers = {"Content-Type": "application/json"}
# Make a POST request to the ingest API
response = http.request(
"POST", ingest_api_url, body=json.dumps(data), headers=headers
)
# Print the status of the response
print(f"Status: {response.status}")
if response.status == 200:
print("Data ingested successfully.")
else:
print(f"Failed to ingest data: {response.data}")
# Return a success response
return {"statusCode": 200, "body": json.dumps("Process completed")}