Skip to content
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

fix(lambda-event-sources): use of CfnParameter for maxBatchSize, retryAttempts & parallelizationFactor fails #9064

Merged
merged 2 commits into from
Jul 14, 2020
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
9 changes: 6 additions & 3 deletions packages/@aws-cdk/aws-lambda-event-sources/lib/kinesis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as kinesis from '@aws-cdk/aws-kinesis';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import {StreamEventSource, StreamEventSourceProps} from './stream';

export interface KinesisEventSourceProps extends StreamEventSourceProps {
Expand All @@ -14,9 +15,11 @@ 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})`);
}
this.props.batchSize !== undefined && cdk.withResolved(this.props.batchSize, batchSize => {
if (batchSize < 1 || batchSize > 10000) {
throw new Error(`Maximum batch size must be between 1 and 10000 inclusive (given ${this.props.batchSize})`);
}
});
}

public bind(target: lambda.IFunction) {
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-lambda-event-sources/test/test.kinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ export = {
test.done();
},

'accepts if batch size is a token'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const stream = new kinesis.Stream(stack, 'S');

// WHEN
fn.addEventSource(new sources.KinesisEventSource(stream, {
batchSize: cdk.Lazy.numberValue({ produce: () => 10 }),
startingPosition: lambda.StartingPosition.LATEST,
}));

test.done();
},

'specific maxBatchingWindow'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down
17 changes: 11 additions & 6 deletions packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,18 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp
throw new Error('maxRecordAge must be between 60 seconds and 7 days inclusive');
}

if (props.retryAttempts && (props.retryAttempts < 0 || props.retryAttempts > 10000)) {
throw new Error(`retryAttempts must be between 0 and 10000 inclusive, got ${props.retryAttempts}`);
}
props.retryAttempts !== undefined && cdk.withResolved(props.retryAttempts, (attempts) => {
if (attempts < 0 || attempts > 10000) {
throw new Error(`retryAttempts must be between 0 and 10000 inclusive, got ${attempts}`);
}
});

props.parallelizationFactor !== undefined && cdk.withResolved(props.parallelizationFactor, (factor) => {
if (factor < 1 || factor > 10) {
throw new Error(`parallelizationFactor must be between 1 and 10 inclusive, got ${factor}`);
}
});

if ((props.parallelizationFactor || props.parallelizationFactor === 0) && (props.parallelizationFactor < 1 || props.parallelizationFactor > 10)) {
throw new Error(`parallelizationFactor must be between 1 and 10 inclusive, got ${props.parallelizationFactor}`);
}

let destinationConfig;

Expand Down
33 changes: 33 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.event-source-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ export = {

test.done();
},
'accepts if retryAttempts is a token'(test: Test) {
const stack = new cdk.Stack();
const fn = new Function(stack, 'fn', {
handler: 'index.handler',
code: Code.fromInline('exports.handler = ${handler.toString()}'),
runtime: Runtime.NODEJS_10_X,
});

new EventSourceMapping(stack, 'test', {
target: fn,
eventSourceArn: '',
retryAttempts: cdk.Lazy.numberValue({ produce: () => 100 }),
});

test.done();
},
'throws if parallelizationFactor is below 1'(test: Test) {
const stack = new cdk.Stack();
const fn = new Function(stack, 'fn', {
Expand Down Expand Up @@ -144,6 +160,23 @@ export = {
test.done();
},

'accepts if parallelizationFactor is a token'(test: Test) {
const stack = new cdk.Stack();
const fn = new Function(stack, 'fn', {
handler: 'index.handler',
code: Code.fromInline('exports.handler = ${handler.toString()}'),
runtime: Runtime.NODEJS_10_X,
});

new EventSourceMapping(stack, 'test', {
target: fn,
eventSourceArn: '',
parallelizationFactor: cdk.Lazy.numberValue({ produce: () => 20 }),
});

test.done();
},

'import event source mapping'(test: Test) {
const stack = new cdk.Stack(undefined, undefined, { stackName: 'test-stack' });
const imported = EventSourceMapping.fromEventSourceMappingId(stack, 'imported', '14e0db71-5d35-4eb5-b481-8945cf9d10c2');
Expand Down