Skip to content

Commit 88e04f0

Browse files
authored
fix(lambda): allow retryAttempts = -1 for infinite retries in EventSourceMapping (#34009)
### Issue # (if applicable) Fixes #34007 This PR addresses an inconsistency in the validation logic for the `retryAttempts` property of Lambda `EventSourceMapping`. Previously, although the documentation indicated that `-1` signifies infinite retries (and is the default), explicitly setting `retryAttempts: -1` would cause a `ValidationError` because the validation logic enforced a lower bound of 0. This change updates the validation in `aws-cdk-lib/aws-lambda/lib/event-source-mapping.ts` to correctly allow `-1` as a valid value, aligning the implementation with the documented behavior. Additionally, this PR includes: * Updates to the TSDoc for `retryAttempts` in `aws-cdk-lib/aws-lambda-event-sources/lib/stream.ts` to improve clarity regarding the valid range (`-1` or `0` to `10000`). * Updates to the unit tests in `aws-cdk-lib/aws-lambda/test/event-source-mapping.test.ts` to verify the corrected behavior. Affected modules: `aws-lambda`, `aws-lambda-event-sources` ### Reason for this change ### Description of changes ### Describe any new or updated permissions being added ### Description of how you validated changes ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0923b5e commit 88e04f0

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

packages/aws-cdk-lib/aws-lambda-event-sources/lib/stream.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,13 @@ export interface StreamEventSourceProps extends BaseStreamEventSourceProps {
9999
readonly maxRecordAge?: Duration;
100100

101101
/**
102-
* Maximum number of retry attempts
103-
* Valid Range:
104-
* * Minimum value of 0
105-
* * Maximum value of 10000
102+
* Maximum number of retry attempts.
106103
*
107-
* The default value is -1, which sets the maximum number of retries to infinite.
108-
* When MaximumRetryAttempts is infinite, Lambda retries failed records until
109-
* the record expires in the event source.
104+
* Set to -1 for infinite retries (until the record expires in the event source).
110105
*
111-
* @default -1
106+
* Valid Range: -1 (infinite) or 0 to 10000
107+
*
108+
* @default -1 (infinite retries)
112109
*/
113110
readonly retryAttempts?: number;
114111

packages/aws-cdk-lib/aws-lambda-event-sources/test/dynamo.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,9 @@ describe('DynamoEventSource', () => {
587587
// THEN
588588
expect(() =>
589589
fn.addEventSource(new sources.DynamoEventSource(table, {
590-
retryAttempts: -1,
590+
retryAttempts: -2, // Test an invalid negative value
591591
startingPosition: lambda.StartingPosition.LATEST,
592-
}))).toThrow(/retryAttempts must be between 0 and 10000 inclusive, got -1/);
592+
}))).toThrow(/retryAttempts must be -1 \(for infinite\) or between 0 and 10000 inclusive, got -2/);
593593
});
594594

595595
test('fails if retryAttempts > 10000', () => {
@@ -609,7 +609,7 @@ describe('DynamoEventSource', () => {
609609
fn.addEventSource(new sources.DynamoEventSource(table, {
610610
retryAttempts: 10001,
611611
startingPosition: lambda.StartingPosition.LATEST,
612-
}))).toThrow(/retryAttempts must be between 0 and 10000 inclusive, got 10001/);
612+
}))).toThrow(/retryAttempts must be -1 \(for infinite\) or between 0 and 10000 inclusive, got 10001/);
613613
});
614614

615615
test('specific bisectBatchOnFunctionError', () => {

packages/aws-cdk-lib/aws-lambda/lib/event-source-mapping.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,9 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp
451451
}
452452

453453
props.retryAttempts !== undefined && cdk.withResolved(props.retryAttempts, (attempts) => {
454-
if (attempts < 0 || attempts > 10000) {
455-
throw new ValidationError(`retryAttempts must be between 0 and 10000 inclusive, got ${attempts}`, this);
454+
// Allow -1 for infinite retries, otherwise validate the 0-10000 range
455+
if (!(attempts === -1 || (attempts >= 0 && attempts <= 10000))) {
456+
throw new ValidationError(`retryAttempts must be -1 (for infinite) or between 0 and 10000 inclusive, got ${attempts}`, this);
456457
}
457458
});
458459

packages/aws-cdk-lib/aws-lambda/test/event-source-mapping.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,20 @@ describe('event source mapping', () => {
9797
})).toThrow(/maxRecordAge must be between 60 seconds and 7 days inclusive/);
9898
});
9999

100-
test('throws if retryAttempts is negative', () => {
100+
test('accepts retryAttempts = -1 for infinite retries', () => {
101101
expect(() => new EventSourceMapping(stack, 'test', {
102102
target: fn,
103103
eventSourceArn: '',
104104
retryAttempts: -1,
105-
})).toThrow(/retryAttempts must be between 0 and 10000 inclusive, got -1/);
105+
})).not.toThrow();
106106
});
107107

108108
test('throws if retryAttempts is over 10000', () => {
109109
expect(() => new EventSourceMapping(stack, 'test', {
110110
target: fn,
111111
eventSourceArn: '',
112112
retryAttempts: 10001,
113-
})).toThrow(/retryAttempts must be between 0 and 10000 inclusive, got 10001/);
113+
})).toThrow(/retryAttempts must be -1 \(for infinite\) or between 0 and 10000 inclusive, got 10001/);
114114
});
115115

116116
test('accepts if retryAttempts is a token', () => {

0 commit comments

Comments
 (0)