Skip to content

Instantly share code, notes, and snippets.

@srcecde
Last active January 31, 2023 16:20
Show Gist options
  • Save srcecde/cc12878b4d23bba2392802d3aa10d2af to your computer and use it in GitHub Desktop.
Save srcecde/cc12878b4d23bba2392802d3aa10d2af to your computer and use it in GitHub Desktop.
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