DEV Community

Cover image for Testing AWS Lambda Recursive Loop Detection
Pubudu Jayawardana for AWS Community Builders

Posted on • Originally published at Medium

Testing AWS Lambda Recursive Loop Detection

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

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.

    Lambda function invoked only 16 times

  3. Then, you can see the message was sent to the DLQ.

  4. 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.

    Email from AWS notifying the loop detected

Please Note

  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.

Read more about this in documentation:

https://docs.aws.amazon.com/lambda/latest/dg/invocation-recursion.html

Top comments (0)