|
| 1 | +import http.client |
1 | 2 | from time import time |
2 | 3 | from boto3 import resource |
| 4 | +import json |
| 5 | +import uuid |
| 6 | +import os |
3 | 7 |
|
4 | 8 |
|
5 | | -def lambda_handler(_event, _context): |
6 | | - source_bucket_name = _event.get("Records")[0].get("s3").get("bucket").get("name") |
7 | | - dest_bucket_name = source_bucket_name.replace("source", "destination") |
8 | | - output_bucket = resource('s3').Bucket(dest_bucket_name) |
| 9 | +def lambda_handler(event, context): |
| 10 | + if "Records" in event and isinstance(event["Records"], list) and len(event["Records"]) > 0: |
| 11 | + response_list = [] |
9 | 12 |
|
10 | | - # Write some placeholder bytestring data to a file in the bucket, |
11 | | - # so we can test that the lambda writes to the correct output bucket. |
12 | | - filename = f"output_report_{time()}.txt" |
13 | | - data = (b'Test file to see if the lambda writes to the correct s3 bucket. ' |
14 | | - b'If our AWS bill skyrockets, this file has been written to the wrong bucket!') |
| 13 | + for obj in event["Records"]: |
| 14 | + s3_record = obj["s3"] |
| 15 | + if "bucket" in s3_record and isinstance(s3_record["bucket"], dict): |
| 16 | + source_bucket_name = s3_record["bucket"].get("name") |
15 | 17 |
|
16 | | - output_bucket.put_object(Body=data, Key=filename) |
| 18 | + if "object" in s3_record and isinstance(s3_record["object"], dict): |
| 19 | + dest_bucket_name = source_bucket_name.replace("source", "destination") |
| 20 | + output_bucket = resource('s3').Bucket(dest_bucket_name) |
| 21 | + api_gateway_url = os.getenv("SERVICE_DOMAIN_NAME") |
| 22 | + object_path = s3_record["object"] |
17 | 23 |
|
18 | | - return { |
19 | | - 'statusCode': 200 |
20 | | - } |
| 24 | + try: |
| 25 | + headers = { |
| 26 | + "Content-Type": "application/json", |
| 27 | + "User-Agent": "batch-processing-lambda" |
| 28 | + } |
| 29 | + |
| 30 | + request_body = json.dumps(object_path) |
| 31 | + filename = f"output_report_{time()}.txt" |
| 32 | + request_id = str(uuid.uuid4()) |
| 33 | + |
| 34 | + payload_for_output_bucket = { |
| 35 | + "timestamp": filename, |
| 36 | + "level": request_body, |
| 37 | + "request": { |
| 38 | + "x-request-id": request_id, |
| 39 | + "x-correlation-id": request_id |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + payload_for_api_gateway = { |
| 44 | + "id": request_id, |
| 45 | + "message": request_body |
| 46 | + } |
| 47 | + |
| 48 | + payload_bytes_output_bucket = json.dumps(payload_for_output_bucket).encode('utf-8') |
| 49 | + payload_bytes_api_gateway = json.dumps(payload_for_api_gateway).encode('utf-8') |
| 50 | + |
| 51 | + connection = http.client.HTTPSConnection(api_gateway_url) |
| 52 | + connection.request("POST", "/", payload_bytes_api_gateway, headers=headers) |
| 53 | + |
| 54 | + response = connection.getresponse() |
| 55 | + json_data = json.loads(response.read().decode('utf-8')) |
| 56 | + connection.close() |
| 57 | + |
| 58 | + response_object = { |
| 59 | + 'statusCode': response.status, |
| 60 | + 'json': json_data, |
| 61 | + 'file': filename |
| 62 | + } |
| 63 | + |
| 64 | + if response.status in [200, 201]: |
| 65 | + response_object.update({ |
| 66 | + 'body': 'Successfully sent JSON data to the API Gateway.' |
| 67 | + }) |
| 68 | + else: |
| 69 | + response_object.update({ |
| 70 | + 'body': response.reason |
| 71 | + }) |
| 72 | + output_bucket.put_object(Body=payload_bytes_output_bucket, Key="body") |
| 73 | + |
| 74 | + response_list.append(response_object) |
| 75 | + |
| 76 | + except Exception as e: |
| 77 | + output_bucket.put_object(Body=payload_bytes_output_bucket, Key="body") |
| 78 | + response_list.append({ |
| 79 | + 'statusCode': 500, |
| 80 | + 'body': 'internal server error', |
| 81 | + 'message': str(e) |
| 82 | + }) |
| 83 | + return response_list |
| 84 | + else: |
| 85 | + return { |
| 86 | + 'statusCode': 400, |
| 87 | + 'body': 'Invalid event structure.' |
| 88 | + } |
0 commit comments