AWS SQS - How to process one message at a time? - amazon-web-services

I have a FIFO Queue in the AWS SQS, which is trigger's Lambda function.
I want to process each messages in Lambda function without parallel execution (one message at a time)
For example: If I have a message A, B, C in the queue. My lambda should complete A, then start B etc.,
My current config of the FIFO queue is
Message retention period: 4 Days
Default visibility timeout: 1 Hour 30 Minutes
Delivery delay: 0 sec
Receive message wait time: 0 Second

set up another lamba(add/setup trigger to invoke this lamba for every one minute using Amazon event brige rule).
In the lamba funtion using receivemessage api call( https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html) you can fetch desired number of messages from sqs queue (max 10 messages) and delete after processing each message.

Related

AWS SQS FIFO queue behaviour with AWS Lambda reserved concurrency

So I have 10 message groups in FIFO queue having 2 messages per group and I have also reserved lambda concurrency set to 5. (Lambda completes execution in 1 min and SQS visibility timeout set to 2 mins)
when all 20 messages are pushed to queue, SQS inflight messages gets set to 10 and then after the execution time, 5 messages gets processed successfully and other 5 moves to DLQ.
And then the next executions inflight messages gets set to 5 (as reserved lambda concurrency set to 5.) and processes as expected (This should be the expected behaviour right?)
Any particular reason why this is happening?

Do we lose messages, if limit in SQS is exceeded?

In AWS SQS console,
I have created Standard SQS Queue and configured as following:
Message retention period: 4 hours
Default visibility timeout: 1 hour
Receive message wait time: 0 seconds
Delivery Delay: 0 seconds
Poll settings as following:
Polling Duration: 60 seconds
Maximum message count: 500
But, What if the count of messages sent to the queue is 1500?
There's a lambda that's processing the messages every half an hour and deleting the (read) SQS messages.
Will other 1000 messages get lost or will they get into SQS whenever another messages in SQS are getting deleted?
From docs:
A single Amazon SQS message queue can contain an unlimited number of messages.
So they will not get deleted from your SQS. Instead they will be send to your lambda as a second batch.

Will an SQS delay apply at the queue level or message group level of a FIFO queue?

An AWS SQS FIFO queue has a batch setting of 1 and a delay of 1 second. Every item received is associated with a MessageGroup.
All at once the queue receives 30 messages across 10 different message groups with each message group accounting for 3 messages...
Will the delay of one second apply at the queue level i.e. the 30 messages will take an elapsed time of 30 seconds to deliver?
Or will the queue spin up 10 consumers, one for each message group, emptying the queue in 3 seconds?
Will the delay of one second apply at the queue level i.e. the 30 messages will take an elapsed time of 30 seconds to deliver?
For FIFO, the delay is applied at queue level:
FIFO queues don't support per-message delays, only per-queue delays. If your application sets the same value of the DelaySeconds parameter on each message, you must modify your application to remove the per-message delay and set DelaySeconds on the entire queue instead.
Number of comsumer lambdas working in parallel does not need to be 10 as written bellow:
In SQS FIFO queues, using more than one MessageGroupId enables Lambda to scale up and process more items in the queue using a greater concurrency limit. Total concurrency is equal to or less than the number of unique MessageGroupIds in the SQS FIFO queue.
Thus you can have still fewer consumer lambdas than groups. But in ideal situation you would have 10 lambdas working in parallel. How it works with lambda is explained in the following AWS blog post:
New for AWS Lambda – SQS FIFO as an event source

Does SQS trigger lambda in an async / sync manner?

Will a standard SQS that I configured to invoke a lambda when it receives message invoke "many lambdas" or only 1 lambda at a time?
From Using AWS Lambda with Amazon SQS:
Lambda polls the queue and invokes your function synchronously with an event that contains queue messages.
It will invoke as many as required, depending on your reserved concurrency limits:
Lambda increases the number of processes that are reading batches by up to 60 more instances per minute. The maximum number of batches that can be processed simultaneously by an event source mapping is 1000.

SQS Lambda Integration - Lambda does not process the queue message

Currently I'm using SQS - Lambda integration
The concurrency for Lambda is available. SQS batch is set to 1 record, 0 delay.
Visibility timeout for SQS is 15 Minutes, Lambda max exec time is 15 Minutes
I would notice that sometimes SQS Messages are stuck in-flight without being processed by any Lambda at all ( They fall into the dead letter queue after 15 minutes, CloudWatch show no Lambda being invoked with the message )
Has anyone faced the same issue?
I run Lambda inside VPC, if that matters
The Lambda backend polls SQS on your behalf and invokes a Lambda function if a message is returned. If the invocation succeeds the message will be deleted if however the function fails the message will be returned to the queue (or DLQ depending on your redrive policy) after the visibility timeout has expired. Check this blog post.
Check if you can see any error metrics for the function in Cloudwatch. Your Lambda function might be failing before it gets a chance to run any code. When this happens there's an error metric but no invocation metric/logs and it's most likely due to an incorrect permission.