Last active
January 31, 2023 16:20
-
-
Save srcecde/cc12878b4d23bba2392802d3aa10d2af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import boto3 | |
def lambda_handler(event, context): | |
region_name = os.environ['AWS_REGION'] | |
if event: | |
sqs = boto3.resource('sqs', region_name=region_name) | |
queue_name = event['Records'][0]['eventSourceARN'].split(':')[-1] | |
queue = sqs.get_queue_by_name(QueueName=queue_name) | |
failed_flag = False | |
messages_to_delete = [] | |
for record in event['Records']: | |
try: | |
body = record['body'] | |
# process message | |
messages_to_delete.append({ | |
'Id': record['messageId'], | |
'ReceiptHandle': record['receiptHandle'] | |
}) | |
except RuntimeError as e: | |
failed_flag =True | |
if messages_to_delete: | |
delete_response = queue.delete_messages( | |
Entries=messages_to_delete) | |
if failed_flag: | |
raise RuntimeError('Failed to process messages') | |
return { | |
'statusCode': 200, | |
'body': json.dumps('Messages processed successfully!') | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment