SNS 何时重试向 Lambda 发送消息,我可以强制执行吗?
When does SNS retry a message to Lambda, and can I force it?
我已经像这样设置了一个 Lambda:
exports.handler = async (event) => {
throw new Error('I AM SAD');
};
它可以被 SNS 主题和 API 网关调用。
我还设置了一个有两个订阅的 SNS 主题;一个直接向 Lambda 发送消息(使用 Lamba 协议);另一个通过 API 网关 (HTTPS) 发送。我希望 SNS 在 lambda 失败时重试发送消息。但它似乎只能通过 API 网关工作。当直接调用 Lambda 时,SNS 只尝试一次(并报告 SUCCESS)。
我知道即使对于 SNS -> Lambda 也有某种重试策略,但是什么时候触发?对于 SNS -> HTTP,它似乎在收到 HTTP 错误代码时触发。
来自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.
失败的 lambda(如上所示)是否被视为客户端错误?我可以强制服务器端错误吗?
Lambda 函数抛出的异常不是客户端错误。那些是由于配置错误而发生的,而不是功能代码中的异常。
SNS only tries once (and reports SUCCESS).
这是设计好的。 SNS 已成功将请求传递给 Lambda 服务,以使用您发布到 SNS 的消息负载异步调用您的 Lambda 函数。
当 SNS 调用 Lambda 函数时,它只将 请求 传递给 Lambda 服务一次。 SNS 唯一会重试的情况是它无法联系 Lambda 服务,或者请求未经授权,如果 Lambda 或 SNS 内部或服务之间的中断导致它们无法通信,或者权限不正确,就会发生这种情况。
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.
一旦调用请求被移交,SNS 就不再参与该事件。
重试失败调用的是 Lambda 服务。
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
默认情况下,您应该会看到表明您的异常抛出函数被 Lambda 调用了 3 次的日志,先是 1 分钟,然后是 2 分钟重试。
我已经像这样设置了一个 Lambda:
exports.handler = async (event) => {
throw new Error('I AM SAD');
};
它可以被 SNS 主题和 API 网关调用。
我还设置了一个有两个订阅的 SNS 主题;一个直接向 Lambda 发送消息(使用 Lamba 协议);另一个通过 API 网关 (HTTPS) 发送。我希望 SNS 在 lambda 失败时重试发送消息。但它似乎只能通过 API 网关工作。当直接调用 Lambda 时,SNS 只尝试一次(并报告 SUCCESS)。
我知道即使对于 SNS -> Lambda 也有某种重试策略,但是什么时候触发?对于 SNS -> HTTP,它似乎在收到 HTTP 错误代码时触发。
来自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.
失败的 lambda(如上所示)是否被视为客户端错误?我可以强制服务器端错误吗?
Lambda 函数抛出的异常不是客户端错误。那些是由于配置错误而发生的,而不是功能代码中的异常。
SNS only tries once (and reports SUCCESS).
这是设计好的。 SNS 已成功将请求传递给 Lambda 服务,以使用您发布到 SNS 的消息负载异步调用您的 Lambda 函数。
当 SNS 调用 Lambda 函数时,它只将 请求 传递给 Lambda 服务一次。 SNS 唯一会重试的情况是它无法联系 Lambda 服务,或者请求未经授权,如果 Lambda 或 SNS 内部或服务之间的中断导致它们无法通信,或者权限不正确,就会发生这种情况。
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.
一旦调用请求被移交,SNS 就不再参与该事件。
重试失败调用的是 Lambda 服务。
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
默认情况下,您应该会看到表明您的异常抛出函数被 Lambda 调用了 3 次的日志,先是 1 分钟,然后是 2 分钟重试。