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.
Please Note
- 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.
Read more about this in documentation:
https://docs.aws.amazon.com/lambda/latest/dg/invocation-recursion.html