AWS SQS event redrive Lambda
This repository contains one simple AWS Lambda function in Python to redrive AWS SQS events from source queue to destination queue. This script can be triggered periodically via cloudwatch as well if source queue contains too many events and you dont want to redrive them altogether.
- batch_size can be passed as input which will move only X events in one execution cycle. Default batch size is 10 if no value is passed.
- This script ensures that if same event is getting moved into DLQ again and again, it wont get retried more than 3 times.
Disclaimer: This script adds one additional field 'RetryCounter' to keep track of number of retries in SQS's MessageAttributes. In 99.9% of cases, ideally this shouldn't cause any break in consumer code, because this is just an additional field added in attributes (NOT in body), but please confirm the impact once before usage. None of the existing data of SQS event is touched upon.
Setup
- Create/Open AWS account
- Go to Lambda service
- Click on create new Lambda
- Give a suitable name to your lambda function.
- Choose python 3.6 or above as 'Runtime'
- Click on 'Create Function'
- Copy and paste the code present in 'SQSEventsRedrive.py' file in this repository in the Lambda.
- Click on 'Deploy' to deploy this code in lambda server.
- Click on Test and provide the input to the lambda function as explained in below section and submit the request.
- You should see the data getting moved from source to destination queue.
- In case of any issues, you can check the logs in cloudwatch by clicking on Existing lambda main page -> Monitoring -> Cloudwatch Logs -> see recent file and recent logs there.
Sample Input
{
"source_queue_url" : "AWS source sqs queue URL",
"destination_queue_url" : "AWS destination sqs queue URL",
"batch_size": 10
}
Note:
- Only 'batch_size' is optional here. queue URLs are mandatory fields.
- Resource URLs are expected in the input, not the ARNs.