Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQS deleting automatically messages after receiving them by Lambda

I have an SQS that triggers a Lambda function. The Lambda function is just receiving the messsage and putting it in a DynamoDB.

It works fine, but the problem is that i noted that the message is deleted from the SQS without the need to add delete() statement in my code.

But in the code it's clearly mentionned that the message should be manually deleted by the consumer otherwise it wil be putted again in the SQS.

What's going on here ?

I want to deal with situation where there will be a problem with the process and in that case the message should reappear again in the SQS so another Lambda can try to process it.

Here is my Lambda code :

import json
import time
import boto3

def lambda_handler(event, context):

message_id = event['Records'][0]['messageId']
message_receipt_handle = event['Records'][0]['receiptHandle']
message_body = event['Records'][0]['body']
print('Message received :')
print(message_body)
print('Processing message ...')

dynamo_db = boto3.client('dynamodb')
response_db = dynamo_db.put_item(
TableName='sqs-test-sbx',
Item={
'id': {
'S': message_id,
},
'Message': {
'S': message_body,
}
}
)
print('dynamodb response :')
print(response_db)

# Simulate a proceesing ...
time.sleep(10)

print('Message processed')

return {
'statusCode': 200,
'message_id': message_id,
'message_body': message_body,
'event': json.dumps(event)
}
like image 958
user1297406 Avatar asked Aug 12 '19 11:08

user1297406


People also ask

Does Lambda delete SQS message automatically?

Example Amazon SQS message event (FIFO queue)If your function successfully processes the batch, Lambda deletes the messages from the queue. By default, if your function encounters an error while processing a batch, all messages in that batch become visible in the queue again.

Why are my SQS messages being deleted?

Messages in Amazon SQS have a limited amount of time to be received and processed before they are automatically deleted by SQS. If a message is not processed before the message retention period has passed, it will be permanently deleted.

How does Lambda delete SQS message?

If a Lambda function throws an error, the Lambda service continues to process the failed message until: The message is processed without any error from the function, and the service deletes the message from the queue. The Message retention period is reached and SQS deletes the message from the queue.

Can SQS lose messages?

An SQS Queue can also be configured with a Message Retention Period in seconds. This value specifies how long a message can stay on a queue before it is automatically deleted, regardless of its processing status. The retention period can be set between 60 seconds and 14 days, with the default at 4 days.


1 Answers

That is normal behavior, when you trigger the lambda directly from SQS

https://docs.aws.amazon.com/en_gb/lambda/latest/dg/with-sqs.html

When your function successfully processes a batch, Lambda deletes its messages from the queue.

You need to delete the message, when you fetch the messages by your own from SQS for instancde from a EC2 instance.

like image 64
Thomas Avatar answered Dec 04 '22 19:12

Thomas