AWS Lambda - How to stop retries when there is a failure
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
AWS Lambda is a serverless compute service that allows you to run code without provisioning or managing servers. When invoking a Lambda function, particularly in event-driven architectures, it's essential to consider scenarios where the function execution may fail. By default, AWS Lambda retries failed invocations several times, which is beneficial in transient failure scenarios. However, there are use cases where you may want to stop retries altogether, such as when failures are due to persistent issues or when idempotency is a concern.
Below, we explore how to manage and stop retries in AWS Lambda through various mechanisms.
Understanding AWS Lambda Retries
By default, AWS Lambda retries function execution when an invocation fails. The number of retry attempts and the behavior can depend on the invocation type:
- Synchronous Invocation: When a Lambda function is called synchronously, AWS Lambda does not retry the execution if it fails. It immediately returns the error to the caller.
- Asynchronous Invocation: When a function is invoked asynchronously, AWS Lambda retries two more times, with delay pauses in between attempts. This retry mechanism is particularly useful when dealing with transient failures, such as network glitches.
- Event Source Mappings: For triggers from services like Amazon Kinesis, AWS SQS, or DynamoDB Streams, Lambda continually polls these services and retries indefinitely until successful processing occurs, or the data expires according to the event source configuration.
Methods to Stop Retries in AWS Lambda
If you need to control or stop Lambda retries due to specific application requirements, AWS provides several mechanisms:
1. AWS Step Functions
Using AWS Step Functions, you can implement complex retry logic or even stop retries completely. A Step Functions state machine allows you to specify retry behavior and handle errors explicitly in a workflow configuration. Here's an example of how to stop retries using AWS Step Functions:
- Configuration: Set an environment variable (e.g.,
RETRY_COUNT). - Inside Function: Introduce logic to increment this count and decide whether to re-throw an error or suppress it.

