Skip to main content
Testing AWS Lambda Recursive Loop Detection

Testing AWS Lambda Recursive Loop Detection

·2 mins· loading ·
AWS Serverless Lambda Recursion
Pubudu Jayawardana
Author
Pubudu Jayawardana
Cloud Engineer, AWS Community Builder

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
#

Images: Recursion between SQS and Lambda
Images: Recursion between SQS and Lambda

Set up
#

This is a CDK project implemented with Python. So, you need CDK and Python installed in your local environment.

  1. Clone the repository: https://github.com/pubudusj/lambda-recursion-test
  2. Go into the cloned directory.
  3. To manually create a virtualenv on MacOS and Linux:
      $ python3 -m venv .venv
    
  4. After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
      $ source .venv/bin/activate
    
  5. If you are a Windows platform, you would activate the virtualenv using:
      % .venv\Scripts\activate.bat
    
  6. Once the virtualenv is activated, you can install the required dependencies.
      $ pip install -r requirements.txt
    
  7. Then, deploy the application:
      $ cdk deploy
    
  8. Once the stack is created, note the SQSQueue and LambdaFunction values.

How to Test
#

  1. Add any message into the SQS queue.
  2. This will trigger the Lambda function.
  3. Within the Lambda execution, it sends the same message into the same SQS queue. This creates an endless loop.

Result
#

  1. Before this new feature, this loop will continue as much as possible and will cost you a lot.
  2. Now, this loop will end after 16 Lambda executions.
Images: Lambda function invoked only 16 times
Images: Lambda function invoked only 16 times
  1. Then, you can see the message was sent to the DLQ.
  2. 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
    Images: Email from AWS notifying the loop detected

Important to know
#

  1. As at now, only SQS and SNS support this loop detection.
  2. To support this feature, your SDK must be in a certain version or higher. Refer the documentation here for more information
  3. 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· loading
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· loading
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· loading
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.