In my previous article, I demonstrated a recursive loop scenario with AWS Lambda and showcased the configurations that help detect and prevent such loops in an event-driven architecture. For that demonstration, I created an SQS queue and a Lambda function using the AWS Console.
I recommend reading the previous article to gain context for this topic. You can find this article on my content page.
In this article, I will recreate the same recursive loop scenario using AWS SAM (Serverless Application Model). This means I will define and deploy the SQS queue and Lambda function using an Infrastructure as Code (IaC) approach. The template.yaml file will specify the resources to be created.
Before diving into the implementation, I will share a recap of what a Lambda recursive loop is and review an example of an event-driven architecture that could trigger this scenario.
Let's look at the architecture diagram!
What is Lambda Recursive Loop?
A Lambda recursive loop refers to a scenario where a Lambda function is invoked recursively, creating a kind of infinite loop. It's similar to a do-while loop without an exit condition.
In an event-driven architecture, this situation can arise when services like SQS and Lambda unintentionally invoke each other in a loop.
For example, consider the following scenario:
- A message is published to an SQS queue.
- The SQS queue is configured with an event trigger to invoke a Lambda function for processing.
- The Lambda function then publishes the message back to the same SQS queue (intentionally or due to a design flaw or code bug). This creates a loop where the message continuously cycles between the queue and the Lambda function.
If left unchecked, a recursive loop can:
- Consume your account's allocated concurrency, leading to throttling and timeouts.
- Drive up costs significantly.
- Negatively impact other critical application functions by monopolizing available resources.
When Was Support Announced?
AWS announced support for Lambda recursive loop configuration in July 2023. This feature enables you to detect and prevent recursive loops in your Lambda functions. By default, this configuration is enabled for all accounts, ensuring Lambda functions automatically detect and stop infinite loops.
Supported Services (as of January 2025)
The following services are supported by the Lambda recursive loop configuration feature:
- Amazon Simple Queue Service (SQS)
- Amazon Simple Notification Service (SNS)
- Amazon Simple Storage Service (S3)
Key Points to Consider
Configuration Default
The Lambda recursive loop configuration is enabled by default.
If your business use case requires disabling this feature, ensure you have alternative guardrails in place, such as:
- Budget monitoring
- Function invocation monitoring
- CloudWatch alarms
Detection and Handling
With this feature, Lambda detects recursive loops from the same event and stops invocation after it exceeds 16 attempts.
Notifications and Metrics
When a recursive loop is detected, AWS sends a notification to your Account Health Dashboard under the "Other Notifications" tab.
An email is also sent to inform you of the detection. This email:
- Is sent once in 24 hours.
- May take up to 3 hours to arrive.
The CloudWatch metric RecursiveInvocationsDropped records the number of dropped recursive invocations, enabling you to monitor and analyze these occurrences.
Create SQS Queue and Lambda using AWS SAM
To create the recursive loop scenario, I need to create a SQS queue and a lambda function. SQS queue will be a standard queue and I will name it contactQueue. Contact information, including phone numbers and names in JSON format, will be published to this queue. Once a message is published, it will trigger a Lambda function for further processing.
The use case for the Lambda function is to publish the contact information to another queue for final processing. However, in this scenario, I will intentionally publish the message back to the contactQueue to simulate a recursive loop.
Since I am using AWS SAM to create both the SQS queue and the Lambda function, let's review the template.yaml that defines these resources.
Reviewing the template.yaml for AWS SAM
This template.yaml defines the resources needed to create the scenario:
The template.yaml file includes code to create SQS queue, lambda function and event source mapping.
Build and Validate
Since I am using AWS SAM for this function, letβs begin by building the function. Use the following command to build the function:
Command: sam build
Once the build is successful, you can invoke the function locally using the sam local invoke command:
Command: sam local invoke
For example:
sam local invoke lambdatosqsFn
When executed without any event json, you should expect the following output if you invoke it at this point:
Deploying to AWS Cloud
After validating the function locally, you can deploy it to the AWS Cloud using the sam deploy command:
Command: sam deploy
If you login to AWS console, you will be able to see the resources created.
SQS Queue
Lambda Function
This concise workflow demonstrates how to create, validate, and deploy Lambda functions using AWS SAM. By leveraging AWS SAM, you can streamline the development process and ensure your functions are tested thoroughly before deployment.
Trigger the Event by publishing a msg to SQS Queue using AWS CLI
The queue and lambda function configuration is now competed. To validate this, I will publish a message to the contactQueue.
This will trigger invocation of lambda. Lambda publishes back to SQS; hence it becomes recursive loop.
I am using AWS CLI to publish the message to the contactQueue.
Monitor and Notifications
Once a recursive loop is detected, you will be notified through the AWS Health Dashboard and via email.
Health Dashboard Notification: The notification can be reviewed under the "Other Notifications" tab. An example is provided below.
Email Notification: An email will be sent to the account owner, notifying them that a recursive loop has been detected. An example of this email is shown below.
In addition to these default notifications, you can set up custom monitoring by observing the Lambda function invocations, specifically the RecursiveInvocationsDropped metric.
Cleanup - Delete the Lambda function & SQS Queue
Once you are done with this exercise, ensure you delete the Lambda function to avoid unnecessary resource usage. Additionally, delete the SQS Queue. If you created any new roles during the process, remember to delete that as well.
Since these resources were build and deployed using AWS SAM, these can be deleted using AWS SAM command.
Command: sam delete
You can also delete the resources via AWS Console.
Conclusion
In this article, I demonstrated how to validate a Lambda Recursive Loop Configuration using AWS SAM. I used a SQS Queueand Lambda function event mapping to create a recursive invocation scenario. The entire configuration and validation steps, including the creation and setup of the SQS queue and Lambda function, were done using the AWS SAM.
I hope you found this article both helpful and informative!
Thank you for reading!
Watch the video here:
https://www.youtube.com/watch?v=BLGJlbKexsI
π’πΎππΎππ½ β¬π½πΆππΎπΆ
πππ ππ¦π³π΅πͺπ§πͺπ¦π₯ ππ°ππΆπ΅πͺπ°π― ππ³π€π©πͺπ΅π¦π€π΅ & ππ¦π·π¦ππ°π±π¦π³ ππ΄π΄π°π€πͺπ’π΅π¦
πππ°πΆπ₯ ππ¦π€π©π―π°ππ°π¨πΊ ππ―π΅π©πΆπ΄πͺπ’π΄π΅
Top comments (0)