-
-
Save zdotfive/e054bc587103101febf825af88c2eaae to your computer and use it in GitHub Desktop.
This file contains hidden or 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