
Testing AWS Lambda Recursive Loop Detection
·2 mins·
0
·
AWS
Serverless
Lambda
Recursion
Table of Contents
Recently, AWS Lambda has introduced a new feature to detect and stop recursive loops in Lambda functions most probably due to mis-configuration. This is definitely a great feature to save some thousands of dollars.
I created a simple Serverless application that you can deploy into your AWS environment and test this new feature. This is built in CDK and Python.
Architecture #

Set up #
This is a CDK project implemented with Python. So, you need CDK and Python installed in your local environment.
- Clone the repository: https://github.com/pubudusj/lambda-recursion-test
- Go into the cloned directory.
- To manually create a virtualenv on MacOS and Linux:
$ python3 -m venv .venv
- After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
$ source .venv/bin/activate
- If you are a Windows platform, you would activate the virtualenv using:
% .venv\Scripts\activate.bat
- Once the virtualenv is activated, you can install the required dependencies.
$ pip install -r requirements.txt
- Then, deploy the application:
$ cdk deploy
- Once the stack is created, note the
SQSQueue
andLambdaFunction
values.
How to Test #
- Add any message into the SQS queue.
- This will trigger the Lambda function.
- Within the Lambda execution, it sends the same message into the same SQS queue. This creates an endless loop.
Result #
- Before this new feature, this loop will continue as much as possible and will cost you a lot.
- Now, this loop will end after 16 Lambda executions.

- Then, you can see the message was sent to the DLQ.
- You also receive the below email from AWS notifying the loop detection. Please note that this email will be sent once per 24 hours per function.
Images: Email from AWS notifying the loop detected
Important to know #
- As at now, only SQS and SNS support this loop detection.
- To support this feature, your SDK must be in a certain version or higher. Refer the documentation here for more information
- You can turn off this feature if you want (but why?) by contacting AWS support.
AWS documentation #
https://docs.aws.amazon.com/lambda/latest/dg/invocation-recursion.html
Related
Load Data From S3 to Postgres using Step Functions
·5 mins·
0
AWS
Serverless
Lambda
Step Functions
Postgres
In this post, I discuss how we can use Step Functions Distributed Maps to parallelise the csv data loading to a database.
AWS Lambda Concurrency when SQS FIFO Queue as Trigger
·4 mins·
1
AWS
Serverless
Lambda
SQS
Concurrency
In this blog, it is discussed how Lambda concurrency is determined when a SQS FIFO queue is configured as the trigger.
Self healing Serverless App with Lambda Destinations and EventBridge
·6 mins·
0
AWS
Lambda
Serverless
EventBridge
EventBridge Pipe
SQS
In this blog post, explains how a Lambda based Serverless application reacts to the errors and attempts to re-drive messages to the origin in a controlled manner.