-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda): event-source maxBatchingWindow property (#4260)
* feat(lambda): event-source maximumBatchingWindow property * fix: remove duplicate import * revert: sqs doesn't support maximumBatchingWindow * chore: set @default * fix: missing import * chore: refactor maximum -> max * chore: refactor kinesis and dynamo in common stream class * chore: remove space * chore: fix method order * chore: refactor StreamingEventSourceProps -> StreamEventSourceProps * chore: quote fix * fix: switch stream.ts to @internal * chore: move batchSize check back to children classes * chore: more quote fixes * chore: refactor enrichMappingOptions * fix: abstract bind method * chore: remove trailing whitespace
- Loading branch information
1 parent
3917c4b
commit 4040032
Showing
6 changed files
with
174 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 10 additions & 24 deletions
34
packages/@aws-cdk/aws-lambda-event-sources/lib/kinesis.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,27 @@ | ||
import kinesis = require('@aws-cdk/aws-kinesis'); | ||
import lambda = require('@aws-cdk/aws-lambda'); | ||
import {StreamEventSource, StreamEventSourceProps} from './stream'; | ||
|
||
export interface KinesisEventSourceProps { | ||
/** | ||
* The largest number of records that AWS Lambda will retrieve from your event | ||
* source at the time of invoking your function. Your function receives an | ||
* event with all the retrieved records. | ||
* | ||
* Valid Range: Minimum value of 1. Maximum value of 10000. | ||
* | ||
* @default 100 | ||
*/ | ||
readonly batchSize?: number; | ||
|
||
/** | ||
* Where to begin consuming the Kinesis stream. | ||
*/ | ||
readonly startingPosition: lambda.StartingPosition; | ||
export interface KinesisEventSourceProps extends StreamEventSourceProps { | ||
} | ||
|
||
/** | ||
* Use an Amazon Kinesis stream as an event source for AWS Lambda. | ||
*/ | ||
export class KinesisEventSource implements lambda.IEventSource { | ||
constructor(readonly stream: kinesis.IStream, private readonly props: KinesisEventSourceProps) { | ||
export class KinesisEventSource extends StreamEventSource { | ||
constructor(readonly stream: kinesis.IStream, props: KinesisEventSourceProps) { | ||
super(props); | ||
|
||
if (this.props.batchSize !== undefined && (this.props.batchSize < 1 || this.props.batchSize > 10000)) { | ||
throw new Error(`Maximum batch size must be between 1 and 10000 inclusive (given ${this.props.batchSize})`); | ||
} | ||
} | ||
|
||
public bind(target: lambda.IFunction) { | ||
target.addEventSourceMapping(`KinesisEventSource:${this.stream.node.uniqueId}`, { | ||
batchSize: this.props.batchSize || 100, | ||
startingPosition: this.props.startingPosition, | ||
eventSourceArn: this.stream.streamArn, | ||
}); | ||
target.addEventSourceMapping(`KinesisEventSource:${this.stream.node.uniqueId}`, | ||
this.enrichMappingOptions({eventSourceArn: this.stream.streamArn}) | ||
); | ||
|
||
this.stream.grantRead(target); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import lambda = require('@aws-cdk/aws-lambda'); | ||
import {Duration} from '@aws-cdk/core'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export interface StreamEventSourceProps { | ||
/** | ||
* The largest number of records that AWS Lambda will retrieve from your event | ||
* source at the time of invoking your function. Your function receives an | ||
* event with all the retrieved records. | ||
* | ||
* Valid Range: Minimum value of 1. Maximum value of 10000. | ||
* | ||
* @default 100 | ||
*/ | ||
readonly batchSize?: number; | ||
|
||
/** | ||
* Where to begin consuming the stream. | ||
*/ | ||
readonly startingPosition: lambda.StartingPosition; | ||
|
||
/** | ||
* The maximum amount of time to gather records before invoking the function. | ||
* Maximum of Duration.minutes(5) | ||
* | ||
* @default Duration.seconds(0) | ||
*/ | ||
readonly maxBatchingWindow?: Duration; | ||
} | ||
|
||
/** | ||
* Use an stream as an event source for AWS Lambda. | ||
* | ||
* @internal | ||
*/ | ||
export abstract class StreamEventSource implements lambda.IEventSource { | ||
protected constructor(protected readonly props: StreamEventSourceProps) { | ||
} | ||
|
||
public abstract bind(_target: lambda.IFunction): void; | ||
|
||
protected enrichMappingOptions(options: lambda.EventSourceMappingOptions): lambda.EventSourceMappingOptions { | ||
return { | ||
...options, | ||
batchSize: this.props.batchSize || 100, | ||
startingPosition: this.props.startingPosition, | ||
maxBatchingWindow: this.props.maxBatchingWindow, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters