Prevent Unintended AWS Charges Due to Lambda Recursion

Scenario:-

While working with AWS lambda functions one needs to be really careful in case the lambda function is invoking same service or some other service. Suppose you have written a lambda function which calls another lambda function, but by mistake you called the same lambda function then in that case the recursion condition arrive which can result a huge bill for your AWS account.

Recursion with SQS & SNS:-

  • In case of a SNS service triggering a lambda function which further triggers the another SNS service. So in this scenario if the subscription of the first SNS is same as the second SNS service then it creates situation of recursion. So we need to give the different subscriptions for the different SNS service.

  • Another recursion scenario can be considered in case of a SQS service. In the above diagram we are using a lambda function which is getting triggered by the SNS service, the Lambda function then triggers the SQS service. If the SQS service again triggers the SNS service then it creates the situation of recursion.

Recursion with S3 Bucket:-

  • Suppose for a use case where you are storing your objects into a S3 bucket & you want to store the logs for every object inserting into the bucket to an another S3 bucket.

  • In that case you need to configure a lambda function which gets automatically triggered when an object is being put into the S3 bucket, which further store the logs into an another S3 bucket. If by mistake you point the lambda function to the first S3 bucket only then it arise condition of recursion. The above diagram represents the same.

Recursion Detection In AWS:-

  • AWS currently supports the AWS SNS , SQS service & loops in Lambda with the detection of recursion. So if there's any recursion occur then it gets stopped automatically.

  • The lambda function gets triggered by an event. The AWS use the X-RAY service to detect the recursion. It checks for the events which are triggering the lambda function.

  • If the same event is triggering the lambda function again & again it waits for the 16 number of trigger counts after the it automatically stops the lambda function.

  • In case of S3 Bucket, Dynamo DB AWS can not detect it automatically. So in these cases you need to opt for other options.

Avoid Recursion Steps In AWS:-

  • Lambda send the metrics to cloud watch, from there you can monitor these metrics.

  • Under the cloud watch Metrics page you can monitor for concurrency metrics, which can be helpful in recursion detection.

  • Keep your lambda function SDK's to the latest version as only the latest sdk supports the recursion detection in case of SNS & SQS.

  • By setting a billing alarms threshold you can detect any unwanted resource or recursion charging.

Summary:-

Working with AWS Lambda functions is complex & to avoid recursion scenarios, particularly when invoking the same service, which can lead to unexpected billing. While AWS supports recursion detection in SNS and SQS, it's crucial to monitor for such occurrences manually, utilizing CloudWatch metrics and setting billing alarms. Keeping Lambda SDKs up-to-date and implementing proper event handling strategies are essential measures to mitigate recursion risks in AWS environments.
In the end recursion must be avoided or detected as soon as possible otherwise it cost you moment by moment.