Skip to main content

Amazon SQS

Trigger AWS Lambda from SQS

SQS to AWS Lambda diagram

In this pattern, we will walk through how to trigger AWS Lambda from messages placed in an SQS queue.

Packages Required

install

dotnet add package Amazon.Lambda.SQSEvents

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.

Function.cs

public SQSBatchResponse FunctionHandler(SQSEvent sqsEvent, ILambdaContext context)
{
var batchResponse = new SQSBatchResponse();
foreach (var record in sqsEvent.Records)
{
try
{
var payload = JsonSerializer.Deserialize<MessageObject>(record.Body);
}
catch (Exception)
{
batchResponse.BatchItemFailures.Add(new SQSBatchResponse.BatchItemFailure()
{
ItemIdentifier = record.MessageId
});
}
}
return batchResponse;
}

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.