Skip to content

Commit

Permalink
Emphasize AWS-side configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bartpio committed Feb 3, 2023
1 parent 4dd360a commit accd490
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,14 @@ protected override void ConfigureServices(IServiceCollection services, IExecutio

### Handling SQS messages with partial batch responses

The [SQS Event Source](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html) can be configured to send batches of more than one message to the Lambda. When a class that derives from `EventFunction<SQSEvent>` is used as the Lambda entry point, any exceptions thrown from the handler will propagate to the Lambda runtime, causing the entire batch that's being processed by that Lambda invocation to fail. All of the messages in the failed batch will be retried (subject to SQS configuration).
The [SQS Event Source](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html) can be configured to send batches of more than one message to the Lambda. When a class that derives from `EventFunction<SQSEvent>` is used as the Lambda entry point, any exceptions thrown from the message handler will propagate to the Lambda runtime, causing the entire batch that's being processed by that Lambda invocation to fail. All of the messages in the failed batch will be retried (subject to SQS configuration).

As an alternative to this default behavior, [partial batch support](https://aws.amazon.com/about-aws/whats-new/2021/11/aws-lambda-partial-batch-response-sqs-event-source/) can be enabled by deriving the entry point class from `RequestResponseFunction<SQSEvent, SQSBatchResponse>` instead, and configuring the SQS Event Source to look in the Lambda response body for batch item failure information. The Event Source configuration can be done using [CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html#cfn-lambda-eventsourcemapping-functionresponsetypes), or by turning on "Report batch item failures" in the AWS console (trigger configuration), or by some other means such as the AWS CLI.
As an alternative to this default behavior, [partial batch support](https://aws.amazon.com/about-aws/whats-new/2021/11/aws-lambda-partial-batch-response-sqs-event-source/) can be enabled by deriving the entry point class from `RequestResponseFunction<SQSEvent, SQSBatchResponse>` instead, and configuring the SQS Event Source to look in the Lambda response body for batch item failure information. When partial batch support is enabled, exceptions thrown from the handler are caught, and failed messages are reported to Lambda in the response payload. Only failed messages will be retried (subject to SQS configuration).

The Event Source configuration can be done using [CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html#cfn-lambda-eventsourcemapping-functionresponsetypes), or by turning on "Report batch item failures" in the AWS console (trigger configuration), or by some other means such as the AWS CLI.

Configuring the SQS Event Source is critical to partial batch support working properly. If this step is omitted, yet an entry point class deriving from `RequestResponseFunction<SQSEvent, SQSBatchResponse>` is used, exceptions thrown from the message handler will be logged but then ignored (not retried).

When partial batch support is enabled, exceptions thrown from the handler are caught, and failed messages are reported to Lambda in the response payload. Only failed messages will be retried (subject to SQS configuration).

# Creating a new function

Expand Down
10 changes: 10 additions & 0 deletions samples/SqsBatchResponseFunction/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@

namespace SqsBatchResponseFunction;

/// <summary>
/// Deriving the entry point class from <see cref="RequestResponseFunction{SQSEvent, SQSBatchResponse}" />,
/// rather than <see cref="EventFunction{SQSEvent}" />, facilitates partial batch support by catching
/// exceptions thrown from the <see cref="IMessageHandler{TMessage}" />, and reporting them to Lambda in
/// the response payload.
/// </summary>
/// <remarks>
/// Configuring the SQS Event Source (on the AWS side) is critical to partial batch support working properly.
/// Please refer to the README for further information.
/// </remarks>
public class Function : RequestResponseFunction<SQSEvent, SQSBatchResponse>
{
protected override void Configure(IConfigurationBuilder builder)
Expand Down

0 comments on commit accd490

Please sign in to comment.