最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

amazon web services - How to auto detect Lambda timeout to invoke another lambda? - Stack Overflow

programmeradmin6浏览0评论

I'm working with AWS lambdas, and I need to exec a code in case of my lambda reach the 15min timeout.

I'm trying to create an EventBridge rule that detects the timeout and triggers another lambda with the code, but I cannot find the way to make it work. This is the format of the rule I'm creating:

{
  "source": ["aws.lambda"],
  "detail-type": ["Lambda Function Invocation Result - Failure"],
  "detail": {
    "functionName": ["lambda-function-name"],
    "errorType": ["TimeoutError"]
  }
}

The lambda that I want to track is async called by another lambda, which is called by an API event. So this is the flow:

API Gateway call -> Lambda Coordinator -> Lambda Executor.

This lambda executor is the one that I need to track and get the timeout.

I have tried several ways, but the rule is never triggered.

I'm working with AWS lambdas, and I need to exec a code in case of my lambda reach the 15min timeout.

I'm trying to create an EventBridge rule that detects the timeout and triggers another lambda with the code, but I cannot find the way to make it work. This is the format of the rule I'm creating:

{
  "source": ["aws.lambda"],
  "detail-type": ["Lambda Function Invocation Result - Failure"],
  "detail": {
    "functionName": ["lambda-function-name"],
    "errorType": ["TimeoutError"]
  }
}

The lambda that I want to track is async called by another lambda, which is called by an API event. So this is the flow:

API Gateway call -> Lambda Coordinator -> Lambda Executor.

This lambda executor is the one that I need to track and get the timeout.

I have tried several ways, but the rule is never triggered.

Share Improve this question edited Mar 23 at 6:16 John Rotenstein 271k28 gold badges447 silver badges531 bronze badges Recognized by AWS Collective asked Mar 22 at 12:21 JGrimaJGrima 257 bronze badges 4
  • Why would you expect an EventBridge event to fire? Have you removed the detail filter and forwarded all events e.g. to cloudwatch and inspected what gets events and what does not? What are you trying to achieve in the first place? Just monitoring or do you also want to e.g. retry the invocation in which case you would also need to somehow get access to the payload. You could set up a cloudwatch logs filter to filter for the timeout logs and go from there. – luk2302 Commented Mar 22 at 13:39
  • I'm just trying to launch another lambda to clean every entry added from the original lambda, because if we get a timeout, it means that the job is not completely done, and it can't be half-done so I need to clean everything. I don't need to access to the payload, just invoke another lambda @luk2302 – JGrima Commented Mar 22 at 18:36
  • Sounds like you do need access to the original payload, otherwise how would you know which job to cleanup? – luk2302 Commented Mar 22 at 20:30
  • Note that async Lambda function timeouts will be retried twice by default, which might help you, or you can reconfigure the MaximumRetryAttempts so that the failed request goes to a DLQ after N attempts, and you can then react to those messages appearing in the DLQ. Also be aware that the Lambda function's context has get_remaining_time_in_millis() method. This can be used to determine the remaining execution time for the function (in milliseconds). – jarmod Commented Mar 23 at 0:26
Add a comment  | 

1 Answer 1

Reset to default 1

EventBridge rules won’t fire when a Lambda times out on an async invocation because async Lambda invocations don’t emit detailed failure events like TimeoutError to EventBridge.

Instead of trying to detect the timeout externally, wrap your Lambda call in a Step Function.

  1. Start Execution of the state machine

  2. The first state is "Invoke Executor"

    • It calls the LambdaExecutor function

    • It has timeout of 850/870 seconds. This timeout is a safe buffer as 900 secs might not cleanly fail.

  3. If LambdaExecutor completes successfully the workflow ends successfully

  4. If LambdaExecutor fails or times out

    • The execution transitions to "Fallback Lambda"

    • This function handles error cases like cleanup, alerting or retry logic.

  5. Once Fallback Lambda finishes the workflow ends (with a failure or handled outcome)

发布评论

评论列表(0)

  1. 暂无评论