Skip to content

fix(lambda): allow retryAttempts = -1 for infinite retries in EventSourceMapping #34009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions packages/aws-cdk-lib/aws-lambda-event-sources/lib/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,13 @@ export interface StreamEventSourceProps extends BaseStreamEventSourceProps {
readonly maxRecordAge?: Duration;

/**
* Maximum number of retry attempts
* Valid Range:
* * Minimum value of 0
* * Maximum value of 10000
* Maximum number of retry attempts.
*
* The default value is -1, which sets the maximum number of retries to infinite.
* When MaximumRetryAttempts is infinite, Lambda retries failed records until
* the record expires in the event source.
* Set to -1 for infinite retries (until the record expires in the event source).
*
* @default -1
* Valid Range: -1 (infinite) or 0 to 10000
*
* @default -1 (infinite retries)
*/
readonly retryAttempts?: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,9 @@ describe('DynamoEventSource', () => {
// THEN
expect(() =>
fn.addEventSource(new sources.DynamoEventSource(table, {
retryAttempts: -1,
retryAttempts: -2, // Test an invalid negative value
startingPosition: lambda.StartingPosition.LATEST,
}))).toThrow(/retryAttempts must be between 0 and 10000 inclusive, got -1/);
}))).toThrow(/retryAttempts must be -1 \(for infinite\) or between 0 and 10000 inclusive, got -2/);
});

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

test('specific bisectBatchOnFunctionError', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/aws-cdk-lib/aws-lambda/lib/event-source-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,9 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ describe('event source mapping', () => {
})).toThrow(/maxRecordAge must be between 60 seconds and 7 days inclusive/);
});

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

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

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