-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-trig
54 lines (46 loc) · 1.75 KB
/
s3-trig
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
import json
import boto3
import os
# Initialize SQS client
sqs_client = boto3.client('sqs')
# Get the DLQ URL from environment variables (set this in the Lambda configuration)
DLQ_URL = os.environ['DLQ_URL']
def lambda_handler(event, context):
try:
# Get the bucket name and object key from the event
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_key = event['Records'][0]['s3']['object']['key']
# Process the file (this is where your logic would go)
# For example, let's just log the file details
print(f"New file uploaded: {bucket_name}/{object_key}")
# Simulate file processing success
process_success = True
# If processing was successful, send a success message to the DLQ
if process_success:
message = {
'status': 'success',
'bucket_name': bucket_name,
'object_key': object_key
}
send_message_to_dlq(message)
except Exception as e:
# If there's an error, send a failure message to the DLQ
error_message = {
'status': 'error',
'error': str(e),
'bucket_name': bucket_name,
'object_key': object_key
}
send_message_to_dlq(error_message)
raise e # Re-raise the exception after logging it to DLQ
def send_message_to_dlq(message):
try:
# Send the message to the DLQ
response = sqs_client.send_message(
QueueUrl=DLQ_URL,
MessageBody=json.dumps(message)
)
print(f"Message sent to DLQ: {response['MessageId']}")
except Exception as e:
print(f"Failed to send message to DLQ: {str(e)}")
raise e