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
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
-
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
-
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mc6c7yc0ccvjmm7q3ew.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zd1z04aop082521kwqpj.png)
### 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)