17

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)
}
user1297406
  • 1,241
  • 1
  • 18
  • 36

1 Answers1

30

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.

Thomas
  • 1,058
  • 8
  • 15
  • 4
    If your Lambda function errors/throws, the message reappears in the queue – jogold Aug 12 '19 at 12:09
  • 1
    Thank you it was that, i tried to raise an exception and yes the message reappeared in the queue – user1297406 Aug 12 '19 at 12:19
  • 2
    Thank you, this distinction in behavior between lambda and non-lambda invocation is not very clear in the documentation. – Justin Dec 08 '21 at 16:27
  • If you use lambda trigger and successfully receive messages, they get deleted automatically, if you ec2 or other servers, you have to delete them manually. – Alex Mar 15 '23 at 10:48