Skip to content

Commit

Permalink
fix(lambda-event-sources): dynamo batch size cannot be a CfnParameter (
Browse files Browse the repository at this point in the history
…#16540)

fixes #16221 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
i05nagai authored Oct 29, 2021
1 parent 7a333b0 commit 56974ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/@aws-cdk/aws-lambda-event-sources/lib/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as lambda from '@aws-cdk/aws-lambda';
import { Names } from '@aws-cdk/core';
import { Names, Token } from '@aws-cdk/core';
import { StreamEventSource, StreamEventSourceProps } from './stream';

export interface DynamoEventSourceProps extends StreamEventSourceProps {
Expand All @@ -15,7 +15,9 @@ export class DynamoEventSource extends StreamEventSource {
constructor(private readonly table: dynamodb.ITable, props: DynamoEventSourceProps) {
super(props);

if (this.props.batchSize !== undefined && (this.props.batchSize < 1 || this.props.batchSize > 1000)) {
if (this.props.batchSize !== undefined
&& !Token.isUnresolved(this.props.batchSize)
&& (this.props.batchSize < 1 || this.props.batchSize > 1000)) {
throw new Error(`Maximum batch size must be between 1 and 1000 inclusive (given ${this.props.batchSize})`);
}
}
Expand Down
43 changes: 43 additions & 0 deletions packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,49 @@ describe('DynamoEventSource', () => {
});


});

test('pass validation if batchsize is token', () => {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const table = new dynamodb.Table(stack, 'T', {
partitionKey: {
name: 'id',
type: dynamodb.AttributeType.STRING,
},
stream: dynamodb.StreamViewType.NEW_IMAGE,
});
const batchSize = new cdk.CfnParameter(stack, 'BatchSize', {
type: 'Number',
default: 100,
minValue: 1,
maxValue: 1000,
});
// WHEN
fn.addEventSource(new sources.DynamoEventSource(table, {
batchSize: batchSize.valueAsNumber,
startingPosition: lambda.StartingPosition.LATEST,
}));

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::EventSourceMapping', {
'EventSourceArn': {
'Fn::GetAtt': [
'TD925BC7E',
'StreamArn',
],
},
'FunctionName': {
'Ref': 'Fn9270CBC0',
},
'BatchSize': {
'Ref': 'BatchSize',
},
'StartingPosition': 'LATEST',
});


});

test('fails if streaming not enabled on table', () => {
Expand Down

0 comments on commit 56974ac

Please sign in to comment.