Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does SNS retry a message to Lambda, and can I force it?

I've set up a Lambda like so:

exports.handler = async (event) => {
  throw new Error('I AM SAD');
};

It can be invoked both by an SNS topic and from API Gateway.

I've also setup an SNS topic with two subscriptions; one sends messages directly to the Lambda (using Lamba protocol); the other sends it through API Gateway (HTTPS). I would like SNS to retry sending the message when the lambda fails. But it only seems work through API Gateway. When the Lambda is invoked directly, SNS only tries once (and reports SUCCESS).

I know there is some sort of retry policy even for SNS -> Lambda but when does this trigger? For SNS -> HTTP it seems to trigger when it receives a HTTP error code.

From Amazon SNS Dead-Letter Queues - Amazon Simple Notification Service:

Client-side errors can happen when Amazon SNS has stale subscription metadata. These errors commonly occur when an owner deletes the endpoint (for example, a Lambda function subscribed to an Amazon SNS topic) or when an owner changes the policy attached to the subscribed endpoint in a way that prevents Amazon SNS from delivering messages to the endpoint. Amazon SNS doesn't retry the message delivery that fails as a result of a client-side error.

Is a failed lambda (as the one above) considered a client-side error? Can I force a server-side error?

like image 381
Christian Eriksson Avatar asked Dec 05 '22 08:12

Christian Eriksson


1 Answers

An exception thrown by a Lambda function is not a client-side error. Those occur due to misconfiguration, not exceptions in the function code.

SNS only tries once (and reports SUCCESS).

This is as designed. SNS successfully passed a request to the Lambda service to asynchronously invoke your Lambda function with the message payload you published to SNS.

When SNS invokes a Lambda function, it delivers that request to the Lambda service only once. The only time SNS will retry is if it fails to contact the Lambda service, or the request is unauthorized, which would happen if an outage inside Lambda or SNS or between the services prevented them from communicating, or if permissions are not correct.

Amazon SNS invokes your function asynchronously with an event that contains a message and metadata.

...

For asynchronous invocation, Lambda queues the message and handles retries. If Amazon SNS is unable to reach Lambda or the message is rejected, Amazon SNS retries at increasing intervals over several hours. For details, see Reliability in the Amazon SNS FAQ.

https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html

Once the invoke request has been handed off, SNS has no further involvement with the event.

It is the Lambda service that retries failed invocations.

Several AWS services, such as Amazon Simple Storage Service (Amazon S3) and Amazon Simple Notification Service (Amazon SNS), invoke functions asynchronously to process events. When you invoke a function asynchronously, you don't wait for a response from the function code. You hand off the event to Lambda and Lambda handles the rest. You can configure how Lambda handles errors, and can send invocation records to a downstream resource to chain together components of your application.

https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html

By default, you should see logs that indicate your exceptiom-throwing function was invoked by Lambda 3 times in total, after a 1 minute and then a 2 minute retry.

like image 188
Michael - sqlbot Avatar answered Dec 09 '22 16:12

Michael - sqlbot