Amazon SQS
Trigger AWS Lambda from SQS
In this pattern, we will walk through how to trigger AWS Lambda from messages placed in an SQS queue.
Packages Required
Function Code
The SQS to Lambda integration is what is known as a poll based invoke. The Lambda service will poll SQS on your behalf and pass batches of messages into Lambda.
The SQSEvent
object contains an array of SQSMessage
objects. The number of messages per batch is configured at the event source.
You can also return an SQSBatchResponse
object to handle a situation in which some of the messages in a batch fail. An SQS Handler will also work with a void
or _Task
_ return type.
Best Practices
- Ensure errors are handled. If the Lambda function fails all messages will go back on to the SQS queue. Try/catch and return an
SQSBatchResponse
object to handle any partial batch failures.