[Update] Lambda's Recursive Loop Detection Now Supports EventBridge Custom Event Bus, So I Tried It Out

[Update] Lambda's Recursive Loop Detection Now Supports EventBridge Custom Event Bus, So I Tried It Out

Lambda recursive loop detection now supports EventBridge Custom Event Bus. In my test with a real loop, it stopped on the new Custom Event Bus but not on the older Classic or default event bus.
2026.10.28

This page has been translated by machine translation. View original

Introduction

On October 14, 2026, Lambda recursive loop detection added support for EventBridge custom event buses.

https://aws.amazon.com/about-aws/whats-new/2026/10/lambda-recursive-loops-eventbridge-custom/

In a setup where a Lambda function that receives an event publishes an event to the same event bus, a configuration or code mistake can cause the function to be invoked over and over. Until now, EventBridge was not covered by detection, so charges kept accruing until you noticed, and there was a risk of using up the account's concurrency.

With this update, such loops are stopped automatically and a notification is sent.

In this article, we will actually create a Lambda function loop with a Custom Event Bus and verify whether it really stops!

Please note that existing setups built with rules and targets are not covered. The verification was done in the Tokyo region.

The target is the new Custom Event Bus

EventBridge currently has two kinds of custom event buses[1].

  • Custom Event Bus: The new event bus, where subscribers filter events and deliver them to targets. The CloudFormation resource type is AWS::EventsV2::EventBus
  • Custom Event Bus - Classic: The conventional event bus, configured with rules and targets. The CloudFormation resource type is AWS::Events::EventBus

Hereafter, I will call the former "Custom Event Bus" and the latter "Classic".

By the way, Lambda recursive loop detection covers SQS, S3, SNS, Custom Event Bus, and invocations between Lambda functions[2].

For how it works and the background so far, please refer to the following article.

https://dev.classmethod.jp/articles/detecting-and-stopping-recursive-loops-in-aws-lambda-functions/

Test environment

I created a loop in which a Custom Event Bus subscriber invokes a Lambda function, and that function publishes an event to the same bus with PutEvents.

Diagram of the test setup

The subscriber's InvocationType can be either asynchronous EVENT or synchronous REQUEST_RESPONSE. This time I prepared both and separated them into different loops using a detail-type filter.

The Lambda function code is as follows. The event's detail carries a hop count, and the function stops by itself after 30 hops.

import json, os
import boto3

events = boto3.client("eventbridgev2")

def handler(event, context):
    hop = event["detail"]["hop"]
    print(json.dumps({
        "hop": hop,
        "trace": os.environ.get("_X_AMZN_TRACE_ID"),
        "boto3": boto3.__version__,
    }))
    # Safety measure in case detection doesn't occur
    if hop >= int(os.environ["MAX_HOP"]):
        return
    events.put_events(
        EventBusArn=os.environ["BUS_ARN"],
        Entries=[{
            "Source": "blog.recursion",
            "DetailType": os.environ["DETAIL_TYPE"],
            "Detail": json.dumps({"hop": hop + 1}),
        }],
    )

In boto3, the Custom Event Bus API is a separate client called eventbridgev2.

boto3 v1.42.97, which is bundled with the Python 3.13 runtime, did not have this client. eventbridgev2 was added in botocore v1.43.102[3], so I bundled boto3 v1.43.118 as a Lambda layer.

When events are published with PutEvents, the Lambda function receives an envelope in the same shape as Classic as is. If you set the subscriber's MaxBatchSize to 1, it arrives as a single object rather than an array[4].

CloudFormation template
AWSTemplateFormatVersion: "2010-09-09"
Description: Lambda recursive loop detection test with EventBridge Custom Event Bus

Parameters:
  Boto3LayerArn:
    Type: String

Resources:
  Bus:
    Type: AWS::EventsV2::EventBus
    Properties:
      Name: recursion-test-v2

  FailureQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: recursion-test-v2-failure

  FunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
        - PolicyName: put-events
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action: events:PutEvents
                Resource: !Ref Bus
              - Effect: Allow
                Action: sqs:SendMessage
                Resource: !GetAtt FailureQueue.Arn

  # サブスクライバーが Lambda を呼び出すためのロール
  DeliveryRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: events.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: deliver
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action: lambda:InvokeFunction
                Resource:
                  - !GetAtt AsyncFunction.Arn
                  - !GetAtt SyncFunction.Arn
              - Effect: Allow
                Action: sqs:SendMessage
                Resource: !GetAtt FailureQueue.Arn

  AsyncFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: recursion-test-v2-async
      Runtime: python3.13
      Handler: index.handler
      Role: !GetAtt FunctionRole.Arn
      Layers:
        - !Ref Boto3LayerArn
      Environment:
        Variables:
          BUS_ARN: !Ref Bus
          DETAIL_TYPE: async
          MAX_HOP: "30"
      Code:
        ZipFile: |
          import json, os
          import boto3

          events = boto3.client("eventbridgev2")

          def handler(event, context):
              hop = event["detail"]["hop"]
              print(json.dumps({
                  "hop": hop,
                  "trace": os.environ.get("_X_AMZN_TRACE_ID"),
                  "boto3": boto3.__version__,
              }))
              # 検出されなかった場合の安全策
              if hop >= int(os.environ["MAX_HOP"]):
                  return
              events.put_events(
                  EventBusArn=os.environ["BUS_ARN"],
                  Entries=[{
                      "Source": "blog.recursion",
                      "DetailType": os.environ["DETAIL_TYPE"],
                      "Detail": json.dumps({"hop": hop + 1}),
                  }],
              )

  SyncFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: recursion-test-v2-sync
      Runtime: python3.13
      Handler: index.handler
      Role: !GetAtt FunctionRole.Arn
      Layers:
        - !Ref Boto3LayerArn
      Environment:
        Variables:
          BUS_ARN: !Ref Bus
          DETAIL_TYPE: sync
          MAX_HOP: "30"
      Code:
        ZipFile: |
          import json, os
          import boto3

          events = boto3.client("eventbridgev2")

          def handler(event, context):
              hop = event["detail"]["hop"]
              print(json.dumps({
                  "hop": hop,
                  "trace": os.environ.get("_X_AMZN_TRACE_ID"),
                  "boto3": boto3.__version__,
              }))
              # 検出されなかった場合の安全策
              if hop >= int(os.environ["MAX_HOP"]):
                  return
              events.put_events(
                  EventBusArn=os.environ["BUS_ARN"],
                  Entries=[{
                      "Source": "blog.recursion",
                      "DetailType": os.environ["DETAIL_TYPE"],
                      "Detail": json.dumps({"hop": hop + 1}),
                  }],
              )

  AsyncInvokeConfig:
    Type: AWS::Lambda::EventInvokeConfig
    Properties:
      FunctionName: !Ref AsyncFunction
      Qualifier: $LATEST
      MaximumRetryAttempts: 0
      DestinationConfig:
        OnFailure:
          Destination: !GetAtt FailureQueue.Arn

  AsyncSubscriber:
    Type: AWS::EventsV2::Subscriber
    Properties:
      Name: recursion-test-async
      EventBusArn: !Ref Bus
      FilterConfiguration:
        Filters:
          - Scope: DATA
            Pattern: '{"detail-type":["async"]}'
      BatchConfiguration:
        MaxBatchSize: 1
      InvokeConfiguration:
        TargetArn: !GetAtt AsyncFunction.Arn
        RoleArn: !GetAtt DeliveryRole.Arn
        LambdaParameters:
          InvocationType: EVENT
      OnFailureConfiguration:
        Arn: !GetAtt FailureQueue.Arn

  SyncSubscriber:
    Type: AWS::EventsV2::Subscriber
    Properties:
      Name: recursion-test-sync
      EventBusArn: !Ref Bus
      FilterConfiguration:
        Filters:
          - Scope: DATA
            Pattern: '{"detail-type":["sync"]}'
      BatchConfiguration:
        MaxBatchSize: 1
      InvokeConfiguration:
        TargetArn: !GetAtt SyncFunction.Arn
        RoleArn: !GetAtt DeliveryRole.Arn
        LambdaParameters:
          InvocationType: REQUEST_RESPONSE
      RetryPolicy:
        MaxRetryAttempts: 2
        MaxEventAgeInSeconds: 600
      OnFailureConfiguration:
        Arn: !GetAtt FailureQueue.Arn

Outputs:
  BusArn:
    Value: !Ref Bus

The first event was published from my local, newer boto3 with the following code.

import json
import boto3

client = boto3.client("eventbridgev2")
client.put_events(
    EventBusArn="<スタックの出力 BusArn>",
    Entries=[{
        "Source": "blog.recursion",
        "DetailType": "async",  # Use "sync" to try the synchronous loop
        "Detail": json.dumps({"hop": 1}),
    }],
)

Looping with asynchronous invocation

When I published the first event to the subscriber with InvocationType set to EVENT, the loop stopped after the execution at hop 16!

13:46:09.601 hop=1  Lineage=1:efad5874:0
13:46:10.015 hop=2  Lineage=2:efad5874:1
...
13:46:11.262 hop=15 Lineage=15:efad5874:14
13:46:11.363 hop=16 Lineage=16:efad5874:15

The times in the logs in this article are in UTC.

The Lineage value in _X_AMZN_TRACE_ID increased by 1 with each invocation.

The stopped event had arrived in the SQS queue that I set as the Lambda function's failure destination.

{
  "version": "1.0",
  "timestamp": "2026-10-27T13:46:11.572Z",
  "requestContext": {
    "requestId": "79c455e8-d3a3-4932-8ed3-161b9662ab58",
    "functionArn": "arn:aws:lambda:ap-northeast-1:123456789012:function:recursion-test-v2-async:$LATEST",
    "condition": "RecursiveInvocation",
    "approximateInvokeCount": 0
  },
  "requestPayload": {
    "detail-type": "async",
    "source": "blog.recursion",
    "detail": { "hop": 17 }
  },
  "responseContext": { "statusCode": 400 }
}

condition is RecursiveInvocation and approximateInvokeCount is 0. This shows that the 17th invocation was stopped before the function code was executed.

A value of 1 was recorded in the CloudWatch RecursiveInvocationsDropped metric.

Graph of the RecursiveInvocationsDropped metric

Looping with synchronous invocation

When InvocationType is REQUEST_RESPONSE, it likewise stopped at hop 16.

With synchronous invocation, a stopped invocation counts as a delivery failure for the subscriber. Because I had set the subscriber's retry count to 2, RecursiveInvocationsDropped recorded 3, including the retries.

When the retries were exhausted, the following record arrived at the subscriber's failure destination.

{
  "id": "53f6f859-6014-44fb-957a-c3039c3574ac",
  "version": "1.0",
  "busArn": "arn:aws:events:ap-northeast-1:123456789012:event-busv2/recursion-test-v2/3kzoowo52jd5vtgpg3ymwjmid",
  "subscriberArn": "arn:aws:events:ap-northeast-1:123456789012:subscriber/recursion-test-sync/39sr6y1xwjwmvr0j2zbk5q17h",
  "targetArn": "arn:aws:lambda:ap-northeast-1:123456789012:function:recursion-test-v2-sync",
  "errorCode": "CUSTOMER_VALIDATION",
  "errorMessage": "The invocation request exceeded maximum recursion depth.",
  "exhaustedRetryCondition": "MaximumRetryAttempts",
  "retryAttempts": 2,
  "failedMessages": [
    {
      "eventId": "313ed728-b42f-4e1a-b040-46accfed0a98",
      "eventGroupId": "b09c76a7-b64e-4f37-aaf0-6e79333d29bd",
      "deduplicationId": "b09c76a7-b64e-4f37-aaf0-6e79333d29bd",
      "timestamp": "2026-10-27T13:46:11.503Z",
      "targetRequestId": "b9537eb9-5735-4449-b8cc-18c42d6c9363"
    }
  ]
}

Unlike the asynchronous case, this does not include the event body, and only the event ID is recorded.

You can tell from the errorMessage that it was stopped by recursive loop detection. Since the same event is sent again on retry, it is stopped again. If you use synchronous invocation, it might be worth reconsidering the retry count.

Classic and the default event bus do not stop

For comparison, I created the same loop with a Classic event bus and the default event bus. Both were configured with rules and targets, and the Lambda function published to the same bus using PutEvents of the events client.

As a result, in both cases the loop did not stop until the safety measure at hop 30.

13:34:50.561 hop=1  Lineage=1:f7dc5e10:0
...
13:34:54.601 hop=29 Lineage=29:f7dc5e10:28
13:34:54.718 hop=30 Lineage=30:f7dc5e10:29

The Lineage value increases up to 29, so the trace header is propagated even with Classic. The reason it still doesn't stop is, I speculate, that Classic is not included in detection.

Just to be sure, I also created an SQS loop in the same account, and it stopped at the 16th invocation, so it doesn't seem to be a problem with the account's settings.

By the way, in the SQS loop I had not set a DLQ on the source queue, so the stopped messages kept being retried from SQS. RecursiveInvocationsDropped keeps being recorded every minute, so be careful if you try this with SQS.

When recursive loops are allowed

I ran put-function-recursion-config on the asynchronous function to allow recursive loops.

aws lambda put-function-recursion-config \
  --function-name recursion-test-v2-async \
  --recursive-loop Allow

In the console, you can change this from "Concurrency and recursion detection" on the "Configuration" tab of the Lambda function.

Edit screen for recursive loop detection

When I published the first event with recursive loops allowed, the loop continued past 16 iterations and stopped at the safety measure of hop 30. With Custom Event Bus too, the per-function setting works the same way as with other services.

Health Dashboard notification

About 3 hours after the loop was stopped, an AWS_LAMBDA_RUNAWAY_TERMINATION_NOTIFICATION event arrived in the Health Dashboard. (According to the documentation, it can take up to 3.5 hours.)

Affected resources in the Health Dashboard

Two events arrived.

Time received (JST) Target functions
2026-10-28 01:40 recursion-test-sqs
2026-10-28 01:50 recursion-test-v2-async, recursion-test-v2-sync

The event description stated that the recursive loop was stopped, and that if the loop is intentional, recursive loop detection can be disabled.

AWS Lambda has detected that one or more Lambda functions in your AWS Account are being invoked in a recursive loop with other AWS resources. To prevent unexpected charges from being billed to your AWS account, Lambda has stopped the recursive invocations listed in the 'Affected resources' tab.

Conclusion

In conclusion, with the new Custom Event Bus, the Lambda function loop stopped at the 16th invocation, but with the conventional Custom Event Bus - Classic and the default event bus, it did not stop.

Existing setups built with Classic rules, and loops that include services that are not covered, such as DynamoDB Streams, will still not be stopped. Be prepared with CloudWatch alarms on Lambda concurrency and invocation counts, for example.

脚注
  1. Amazon EventBridge event bus (Custom) - Amazon EventBridge ↩︎

  2. Use Lambda recursive loop detection to prevent infinite loops - AWS Lambda ↩︎

  3. botocore CHANGELOG 1.43.102 ↩︎

  4. Lambda function target - Amazon EventBridge ↩︎

Share this article