From 25bf76697679792187907e8253da7fcb21aefbc2 Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Sat, 17 Sep 2022 21:22:19 -0300 Subject: [PATCH 1/7] feat(lambda-event-sources): refactoring --- .../aws-lambda-event-sources/lib/sqs.ts | 2 +- .../aws-lambda-event-sources/lib/stream.ts | 2 +- .../test/dynamo.test.ts | 29 +++++++++++-------- .../aws-lambda/lib/event-source-filter.ts | 16 ++++++++-- .../aws-lambda/lib/event-source-mapping.ts | 12 ++++++-- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts b/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts index 105a7faf66766..458448f95ee55 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts @@ -45,7 +45,7 @@ export interface SqsEventSourceProps { * * @default - None */ - readonly filters?: Array<{[key: string]: any}>; + readonly filters?: Array; } /** diff --git a/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts b/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts index 7a3a4036cfc55..9f0f217e13e8d 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts @@ -115,7 +115,7 @@ export interface StreamEventSourceProps extends BaseStreamEventSourceProps { * * @default - None */ - readonly filters?: Array<{[key: string]: any}>; + readonly filters?: Array; } /** diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts index 214ea8612274b..e11c2fa542f32 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts @@ -255,21 +255,26 @@ describe('DynamoEventSource', () => { stream: dynamodb.StreamViewType.NEW_IMAGE, }); + const filters: Array = [ + lambda.FilterCriteria.filter({ + eventName: lambda.FilterRule.isEqual('INSERT'), + dynamodb: { + Keys: { + id: { + S: lambda.FilterRule.exists(), + }, + }, + }, + }), + ]; + + expect(Array.isArray(filters)).toBe(true); + expect(filters[0]).toBeInstanceOf(lambda.FilterCriteria); + // WHEN fn.addEventSource(new sources.DynamoEventSource(table, { startingPosition: lambda.StartingPosition.LATEST, - filters: [ - lambda.FilterCriteria.filter({ - eventName: lambda.FilterRule.isEqual('INSERT'), - dynamodb: { - Keys: { - id: { - S: lambda.FilterRule.exists(), - }, - }, - }, - }), - ], + filters: filters, })); // THEN diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts index 50bce839123ba..054509a059f2c 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts @@ -76,7 +76,19 @@ export class FilterCriteria { /** * Filter for event source */ - public static filter(filter: {[key:string]: any}): {[key:string]: any} { - return { pattern: JSON.stringify(filter) }; + public static filter(filter: {[key:string]: any}): FilterCriteria { + return new FilterCriteria(filter); + } + private filterCriteria: {[key: string]: any}; + private constructor(filter: {[key:string]: any}) { + this.filterCriteria = filter; + } + /** + * Returns a patter to filter criteria + * + * @returns pattern object + */ + public toPattern(): {[key: string]: string} { + return { pattern: JSON.stringify(this.filterCriteria) }; } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts index 9798a31c313f0..ab1e386540735 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts @@ -1,6 +1,7 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { IEventSourceDlq } from './dlq'; +import { FilterCriteria } from './event-source-filter'; import { IFunction } from './function-base'; import { CfnEventSourceMapping } from './lambda.generated'; @@ -239,7 +240,7 @@ export interface EventSourceMappingOptions { * * @default - none */ - readonly filters?: Array<{[key: string]: any}> + readonly filters?: Array; //Array<{[key: string]: any}>; } /** @@ -356,6 +357,13 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp let consumerGroupConfig = props.kafkaConsumerGroupId ? { consumerGroupId: props.kafkaConsumerGroupId } : undefined; + let filters: Array<{[key: string]: string}> = []; + if (props.filters) { + for (let filter of props.filters) { + filters.push(filter.toPattern()); + } + } + const cfnEventSourceMapping = new CfnEventSourceMapping(this, 'Resource', { batchSize: props.batchSize, bisectBatchOnFunctionError: props.bisectBatchOnError, @@ -374,7 +382,7 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp tumblingWindowInSeconds: props.tumblingWindow?.toSeconds(), sourceAccessConfigurations: props.sourceAccessConfigurations?.map((o) => {return { type: o.type.type, uri: o.uri };}), selfManagedEventSource, - filterCriteria: props.filters ? { filters: props.filters }: undefined, + filterCriteria: props.filters ? { filters: filters }: undefined, selfManagedKafkaEventSourceConfig: props.kafkaBootstrapServers ? consumerGroupConfig : undefined, amazonManagedKafkaEventSourceConfig: props.eventSourceArn ? consumerGroupConfig : undefined, }); From e066cf6deaefd62a76f27de5692c946e1cfd99ca Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Sat, 17 Sep 2022 21:36:38 -0300 Subject: [PATCH 2/7] chore(lambda-event-sources): removing comment --- packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts index ab1e386540735..916179e34cb1d 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts @@ -240,7 +240,7 @@ export interface EventSourceMappingOptions { * * @default - none */ - readonly filters?: Array; //Array<{[key: string]: any}>; + readonly filters?: Array; } /** From 1ea37bdf8ca8d8a33706e9281f7c89a3d6e24ffb Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Tue, 20 Sep 2022 11:47:22 -0300 Subject: [PATCH 3/7] fix: discontinuing filters --- .../aws-lambda-event-sources/lib/sqs.ts | 12 ++++++++++-- .../aws-lambda-event-sources/lib/stream.ts | 12 ++++++++++-- .../test/dynamo.test.ts | 2 +- .../test/integ.dynamodb-with-filter-criteria.ts | 2 +- .../test/integ.sqs-with-filter-criteria.ts | 2 +- .../aws-lambda-event-sources/test/sqs.test.ts | 2 +- .../aws-lambda/lib/event-source-mapping.ts | 17 +++++++++++++---- .../test/event-source-mapping.test.ts | 4 ++-- 8 files changed, 39 insertions(+), 14 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts b/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts index 458448f95ee55..e53e495bf1c1a 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts @@ -44,8 +44,16 @@ export interface SqsEventSourceProps { * Add filter criteria option * * @default - None + * @deprecated see `filterCriteria` */ - readonly filters?: Array; + readonly filters?: Array<{[key: string]: any}>; + + /** + * Add filter criteria option + * + * @default - none + */ + readonly filterCriteria?: Array; } /** @@ -80,7 +88,7 @@ export class SqsEventSource implements lambda.IEventSource { reportBatchItemFailures: this.props.reportBatchItemFailures, enabled: this.props.enabled, eventSourceArn: this.queue.queueArn, - filters: this.props.filters, + filterCriteria: this.props.filterCriteria, }); this._eventSourceMappingId = eventSourceMapping.eventSourceMappingId; diff --git a/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts b/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts index 9f0f217e13e8d..0e392bc94e73f 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts @@ -114,8 +114,16 @@ export interface StreamEventSourceProps extends BaseStreamEventSourceProps { * Add filter criteria option * * @default - None + * @deprecated see `filterCriteria` */ - readonly filters?: Array; + readonly filters?: Array<{[key: string]: any}>; + + /** + * Add filter criteria option + * + * @default - none + */ + readonly filterCriteria?: Array; } /** @@ -141,7 +149,7 @@ export abstract class StreamEventSource implements lambda.IEventSource { onFailure: this.props.onFailure, tumblingWindow: this.props.tumblingWindow, enabled: this.props.enabled, - filters: this.props.filters, + filterCriteria: this.props.filterCriteria, }; } } diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts index e11c2fa542f32..7995b95616eae 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts @@ -274,7 +274,7 @@ describe('DynamoEventSource', () => { // WHEN fn.addEventSource(new sources.DynamoEventSource(table, { startingPosition: lambda.StartingPosition.LATEST, - filters: filters, + filterCriteria: filters, })); // THEN diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb-with-filter-criteria.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb-with-filter-criteria.ts index 082964ef26518..e0a7e6ede6ecb 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb-with-filter-criteria.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb-with-filter-criteria.ts @@ -22,7 +22,7 @@ const table = new dynamodb.Table(stack, 'T', { fn.addEventSource(new DynamoEventSource(table, { batchSize: 5, startingPosition: lambda.StartingPosition.LATEST, - filters: [ + filterCriteria: [ lambda.FilterCriteria.filter({ eventName: lambda.FilterRule.isEqual('INSERT'), dynamodb: { diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/integ.sqs-with-filter-criteria.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/integ.sqs-with-filter-criteria.ts index b180736709fe9..54f97bd8c9293 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/integ.sqs-with-filter-criteria.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/integ.sqs-with-filter-criteria.ts @@ -14,7 +14,7 @@ const queue = new sqs.Queue(stack, 'Q'); fn.addEventSource(new SqsEventSource(queue, { batchSize: 5, - filters: [ + filterCriteria: [ lambda.FilterCriteria.filter({ body: { id: lambda.FilterRule.exists(), diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts index 24d9dd307e14a..635f058693ce3 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts @@ -403,7 +403,7 @@ describe('SQSEventSource', () => { // WHEN fn.addEventSource(new sources.SqsEventSource(q, { - filters: [ + filterCriteria: [ lambda.FilterCriteria.filter({ body: { id: lambda.FilterRule.exists(), diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts index 916179e34cb1d..ebc1235a821db 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts @@ -239,8 +239,17 @@ export interface EventSourceMappingOptions { * @see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html * * @default - none + * @deprecated see `filterCriteria` */ - readonly filters?: Array; + readonly filters?: Array<{[key: string]: any}>; + + /** + * Add filter criteria to Event Source + * @see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html + * + * @default - none + */ + readonly filterCriteria?: Array; } /** @@ -358,8 +367,8 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp let consumerGroupConfig = props.kafkaConsumerGroupId ? { consumerGroupId: props.kafkaConsumerGroupId } : undefined; let filters: Array<{[key: string]: string}> = []; - if (props.filters) { - for (let filter of props.filters) { + if (props.filterCriteria) { + for (let filter of props.filterCriteria) { filters.push(filter.toPattern()); } } @@ -382,7 +391,7 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp tumblingWindowInSeconds: props.tumblingWindow?.toSeconds(), sourceAccessConfigurations: props.sourceAccessConfigurations?.map((o) => {return { type: o.type.type, uri: o.uri };}), selfManagedEventSource, - filterCriteria: props.filters ? { filters: filters }: undefined, + filterCriteria: props.filterCriteria ? { filters: filters }: undefined, selfManagedKafkaEventSourceConfig: props.kafkaBootstrapServers ? consumerGroupConfig : undefined, amazonManagedKafkaEventSourceConfig: props.eventSourceArn ? consumerGroupConfig : undefined, }); diff --git a/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts b/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts index 728cfd5d161ee..061130fe163f0 100644 --- a/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts @@ -223,7 +223,7 @@ describe('event source mapping', () => { target: fn, eventSourceArn: eventSourceArn, kafkaTopic: topicNameParam.valueAsString, - filters: [ + filterCriteria: [ FilterCriteria.filter({ numericEquals: FilterRule.isEqual(1), }), @@ -252,7 +252,7 @@ describe('event source mapping', () => { target: fn, eventSourceArn: eventSourceArn, kafkaTopic: topicNameParam.valueAsString, - filters: [ + filterCriteria: [ FilterCriteria.filter({ orFilter: FilterRule.or('one', 'two'), stringEquals: FilterRule.isEqual('test'), From ec164d749edfb2533f6ce7bd8782bd8d6bab3be9 Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Mon, 26 Sep 2022 17:27:36 -0300 Subject: [PATCH 4/7] chore: refactoring to enabled the deprecated parameter --- .../aws-lambda-event-sources/lib/sqs.ts | 1 + .../aws-lambda-event-sources/lib/stream.ts | 1 + .../test/dynamo.test.ts | 121 ++++++++++++------ .../test/kafka.test.ts | 33 ++--- .../test/kinesis.test.ts | 17 +-- .../aws-lambda-event-sources/test/sqs.test.ts | 23 ++-- .../aws-lambda/lib/event-source-filter.ts | 2 +- .../aws-lambda/lib/event-source-mapping.ts | 4 +- .../test/event-source-mapping.test.ts | 32 +++++ 9 files changed, 155 insertions(+), 79 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts b/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts index e53e495bf1c1a..7e73fad9b70d1 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts @@ -88,6 +88,7 @@ export class SqsEventSource implements lambda.IEventSource { reportBatchItemFailures: this.props.reportBatchItemFailures, enabled: this.props.enabled, eventSourceArn: this.queue.queueArn, + filters: this.props.filters, filterCriteria: this.props.filterCriteria, }); this._eventSourceMappingId = eventSourceMapping.eventSourceMappingId; diff --git a/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts b/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts index 0e392bc94e73f..9d9a31b297e52 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts @@ -149,6 +149,7 @@ export abstract class StreamEventSource implements lambda.IEventSource { onFailure: this.props.onFailure, tumblingWindow: this.props.tumblingWindow, enabled: this.props.enabled, + filters: this.props.filters, filterCriteria: this.props.filterCriteria, }; } diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts index 7995b95616eae..65fe450a5453f 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts @@ -2,6 +2,7 @@ import { Template } from '@aws-cdk/assertions'; import * as dynamodb from '@aws-cdk/aws-dynamodb'; import * as lambda from '@aws-cdk/aws-lambda'; import * as sqs from '@aws-cdk/aws-sqs'; +import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import * as sources from '../lib'; import { TestFunction } from './test-function'; @@ -9,7 +10,7 @@ import { TestFunction } from './test-function'; /* eslint-disable quote-props */ describe('DynamoEventSource', () => { - test('sufficiently complex example', () => { + testDeprecated('sufficiently complex example', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -75,7 +76,7 @@ describe('DynamoEventSource', () => { }); - test('specific tumblingWindow', () => { + testDeprecated('specific tumblingWindow', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -102,7 +103,7 @@ describe('DynamoEventSource', () => { }); - test('specific batch size', () => { + testDeprecated('specific batch size', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -138,7 +139,7 @@ describe('DynamoEventSource', () => { }); - test('pass validation if batchsize is token', () => { + testDeprecated('pass validation if batchsize is token', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -222,7 +223,7 @@ describe('DynamoEventSource', () => { }); - test('fails if batch size > 10000', () => { + testDeprecated('fails if batch size > 10000', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -243,7 +244,7 @@ describe('DynamoEventSource', () => { }); - test('adding filter criteria', () => { + testDeprecated('adding filter criteria', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -299,7 +300,64 @@ describe('DynamoEventSource', () => { }); }); - test('specific maxBatchingWindow', () => { + testDeprecated('adding deprecated filters', () => { + // 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 filters = [ + { + pattern: JSON.stringify({ + eventName: lambda.FilterRule.isEqual('INSERT'), + dynamodb: { + Keys: { + id: { + S: lambda.FilterRule.exists(), + }, + }, + }, + }), + }, + ]; + + expect(Array.isArray(filters)).toBe(true); + + // WHEN + fn.addEventSource(new sources.DynamoEventSource(table, { + startingPosition: lambda.StartingPosition.LATEST, + filters: filters, + })); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::Lambda::EventSourceMapping', { + 'EventSourceArn': { + 'Fn::GetAtt': [ + 'TD925BC7E', + 'StreamArn', + ], + }, + 'FunctionName': { + 'Ref': 'Fn9270CBC0', + }, + 'FilterCriteria': { + 'Filters': [ + { + 'Pattern': '{"eventName":["INSERT"],"dynamodb":{"Keys":{"id":{"S":[{"exists":true}]}}}}', + }, + ], + }, + 'StartingPosition': 'LATEST', + }); + }); + + testDeprecated('specific maxBatchingWindow', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -335,7 +393,7 @@ describe('DynamoEventSource', () => { }); - test('throws if maxBatchingWindow > 300 seconds', () => { + testDeprecated('throws if maxBatchingWindow > 300 seconds', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -353,11 +411,9 @@ describe('DynamoEventSource', () => { maxBatchingWindow: cdk.Duration.seconds(301), startingPosition: lambda.StartingPosition.LATEST, }))).toThrow(/maxBatchingWindow cannot be over 300 seconds/); - - }); - test('contains eventSourceMappingId after lambda binding', () => { + testDeprecated('contains eventSourceMappingId after lambda binding', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -377,7 +433,6 @@ describe('DynamoEventSource', () => { // THEN expect(eventSource.eventSourceMappingId).toBeDefined(); - }); test('eventSourceMappingId throws error before binding to lambda', () => { @@ -399,7 +454,7 @@ describe('DynamoEventSource', () => { }); - test('specific retryAttempts', () => { + testDeprecated('specific retryAttempts', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -431,11 +486,9 @@ describe('DynamoEventSource', () => { 'MaximumRetryAttempts': 10, 'StartingPosition': 'LATEST', }); - - }); - test('fails if retryAttempts < 0', () => { + testDeprecated('fails if retryAttempts < 0', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -453,11 +506,9 @@ describe('DynamoEventSource', () => { retryAttempts: -1, startingPosition: lambda.StartingPosition.LATEST, }))).toThrow(/retryAttempts must be between 0 and 10000 inclusive, got -1/); - - }); - test('fails if retryAttempts > 10000', () => { + testDeprecated('fails if retryAttempts > 10000', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -479,7 +530,7 @@ describe('DynamoEventSource', () => { }); - test('specific bisectBatchOnFunctionError', () => { + testDeprecated('specific bisectBatchOnFunctionError', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -515,7 +566,7 @@ describe('DynamoEventSource', () => { }); - test('specific parallelizationFactor', () => { + testDeprecated('specific parallelizationFactor', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -547,11 +598,9 @@ describe('DynamoEventSource', () => { 'ParallelizationFactor': 5, 'StartingPosition': 'LATEST', }); - - }); - test('fails if parallelizationFactor < 1', () => { + testDeprecated('fails if parallelizationFactor < 1', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -569,11 +618,9 @@ describe('DynamoEventSource', () => { parallelizationFactor: 0, startingPosition: lambda.StartingPosition.LATEST, }))).toThrow(/parallelizationFactor must be between 1 and 10 inclusive, got 0/); - - }); - test('fails if parallelizationFactor > 10', () => { + testDeprecated('fails if parallelizationFactor > 10', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -591,11 +638,9 @@ describe('DynamoEventSource', () => { parallelizationFactor: 11, startingPosition: lambda.StartingPosition.LATEST, }))).toThrow(/parallelizationFactor must be between 1 and 10 inclusive, got 11/); - - }); - test('specific maxRecordAge', () => { + testDeprecated('specific maxRecordAge', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -627,11 +672,9 @@ describe('DynamoEventSource', () => { 'MaximumRecordAgeInSeconds': 100, 'StartingPosition': 'LATEST', }); - - }); - test('fails if maxRecordAge < 60 seconds', () => { + testDeprecated('fails if maxRecordAge < 60 seconds', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -653,7 +696,7 @@ describe('DynamoEventSource', () => { }); - test('fails if maxRecordAge > 7 days', () => { + testDeprecated('fails if maxRecordAge > 7 days', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -671,11 +714,9 @@ describe('DynamoEventSource', () => { maxRecordAge: cdk.Duration.seconds(604801), startingPosition: lambda.StartingPosition.LATEST, }))).toThrow(/maxRecordAge must be between 60 seconds and 7 days inclusive/); - - }); - test('specific destinationConfig', () => { + testDeprecated('specific destinationConfig', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -718,11 +759,9 @@ describe('DynamoEventSource', () => { }, 'StartingPosition': 'LATEST', }); - - }); - test('specific functionResponseTypes', () => { + testDeprecated('specific functionResponseTypes', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -758,7 +797,7 @@ describe('DynamoEventSource', () => { }); - test('event source disabled', () => { + testDeprecated('event source disabled', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts index 4688765ca1146..42e9ebdf84c67 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts @@ -2,13 +2,14 @@ import { Template, Match } from '@aws-cdk/assertions'; import { SecurityGroup, SubnetType, Vpc } from '@aws-cdk/aws-ec2'; import * as lambda from '@aws-cdk/aws-lambda'; import { Secret } from '@aws-cdk/aws-secretsmanager'; +import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import * as sources from '../lib'; import { TestFunction } from './test-function'; describe('KafkaEventSource', () => { describe('msk', () => { - test('default', () => { + testDeprecated('default', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -61,7 +62,8 @@ describe('KafkaEventSource', () => { }); - test('with secret', () => { + + testDeprecated('with secret', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -137,7 +139,7 @@ describe('KafkaEventSource', () => { }); describe('self-managed kafka', () => { - test('default', () => { + testDeprecated('default', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -205,6 +207,7 @@ describe('KafkaEventSource', () => { }); + test('without vpc, secret must be set', () => { const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -219,12 +222,10 @@ describe('KafkaEventSource', () => { startingPosition: lambda.StartingPosition.TRIM_HORIZON, })); }).toThrow(/secret must be set/); - - }); describe('vpc', () => { - test('correctly rendered in the stack', () => { + testDeprecated('correctly rendered in the stack', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -282,7 +283,7 @@ describe('KafkaEventSource', () => { }); - test('with secret', () => { + testDeprecated('with secret', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -419,7 +420,7 @@ describe('KafkaEventSource', () => { }); }); - test('using SCRAM-SHA-256', () => { + testDeprecated('using SCRAM-SHA-256', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -454,7 +455,7 @@ describe('KafkaEventSource', () => { }); }); - test('using BASIC_AUTH', () => { + testDeprecated('using BASIC_AUTH', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -489,7 +490,7 @@ describe('KafkaEventSource', () => { }); }); - test('using CLIENT_CERTIFICATE_TLS_AUTH', () => { + testDeprecated('using CLIENT_CERTIFICATE_TLS_AUTH', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -524,7 +525,7 @@ describe('KafkaEventSource', () => { }); }); - test('using CLIENT_CERTIFICATE_TLS_AUTH with rootCA', () => { + testDeprecated('using CLIENT_CERTIFICATE_TLS_AUTH with rootCA', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -567,7 +568,7 @@ describe('KafkaEventSource', () => { }); }); - test('with rootCA', () => { + testDeprecated('with rootCA', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -601,7 +602,7 @@ describe('KafkaEventSource', () => { }); }); - test('rootCACertificate can be ISecret', () => { + testDeprecated('rootCACertificate can be ISecret', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -634,7 +635,7 @@ describe('KafkaEventSource', () => { }); }); - test('consumerGroupId can be set for SelfManagedKafkaEventSource', () => { + testDeprecated('consumerGroupId can be set for SelfManagedKafkaEventSource', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -661,7 +662,7 @@ describe('KafkaEventSource', () => { }); - test('consumerGroupId can be set for ManagedKafkaEventSource', () => { + testDeprecated('consumerGroupId can be set for ManagedKafkaEventSource', () => { // GIVEN const stack = new cdk.Stack(); @@ -690,7 +691,7 @@ describe('KafkaEventSource', () => { }); - test('ManagedKafkaEventSource name conforms to construct id rules', () => { + testDeprecated('ManagedKafkaEventSource name conforms to construct id rules', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/kinesis.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/kinesis.test.ts index f42c0210786c8..6d7d7d0d6a1b7 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/kinesis.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/kinesis.test.ts @@ -1,6 +1,7 @@ import { Template } from '@aws-cdk/assertions'; import * as kinesis from '@aws-cdk/aws-kinesis'; import * as lambda from '@aws-cdk/aws-lambda'; +import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import * as sources from '../lib'; import { TestFunction } from './test-function'; @@ -8,7 +9,7 @@ import { TestFunction } from './test-function'; /* eslint-disable quote-props */ describe('KinesisEventSource', () => { - test('sufficiently complex example', () => { + testDeprecated('sufficiently complex example', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -77,7 +78,7 @@ describe('KinesisEventSource', () => { }); - test('specific tumblingWindowInSeconds', () => { + testDeprecated('specific tumblingWindowInSeconds', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -109,7 +110,7 @@ describe('KinesisEventSource', () => { }); - test('specific batch size', () => { + testDeprecated('specific batch size', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -169,7 +170,7 @@ describe('KinesisEventSource', () => { }); - test('accepts if batch size is a token', () => { + testDeprecated('accepts if batch size is a token', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -184,7 +185,7 @@ describe('KinesisEventSource', () => { }); - test('specific maxBatchingWindow', () => { + testDeprecated('specific maxBatchingWindow', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -214,7 +215,7 @@ describe('KinesisEventSource', () => { }); - test('contains eventSourceMappingId after lambda binding', () => { + testDeprecated('contains eventSourceMappingId after lambda binding', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -244,7 +245,7 @@ describe('KinesisEventSource', () => { }); - test('event source disabled', () => { + testDeprecated('event source disabled', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -264,7 +265,7 @@ describe('KinesisEventSource', () => { }); - test('AT_TIMESTAMP starting position', () => { + testDeprecated('AT_TIMESTAMP starting position', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts index 635f058693ce3..67162b3e899aa 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts @@ -2,6 +2,7 @@ import { Template } from '@aws-cdk/assertions'; import * as iam from '@aws-cdk/aws-iam'; import * as lambda from '@aws-cdk/aws-lambda'; import * as sqs from '@aws-cdk/aws-sqs'; +import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import { App } from '@aws-cdk/core'; import * as sources from '../lib'; @@ -10,7 +11,7 @@ import { TestFunction } from './test-function'; /* eslint-disable quote-props */ describe('SQSEventSource', () => { - test('defaults', () => { + testDeprecated('defaults', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -59,7 +60,7 @@ describe('SQSEventSource', () => { }); - test('specific batch size', () => { + testDeprecated('specific batch size', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -87,7 +88,7 @@ describe('SQSEventSource', () => { }); - test('unresolved batch size', () => { + testDeprecated('unresolved batch size', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -139,7 +140,7 @@ describe('SQSEventSource', () => { }); - test('batch size is > 10 and batch window is defined', () => { + testDeprecated('batch size is > 10 and batch window is defined', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -175,7 +176,7 @@ describe('SQSEventSource', () => { }); - test('specific batch window', () => { + testDeprecated('specific batch window', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -224,7 +225,7 @@ describe('SQSEventSource', () => { }); - test('contains eventSourceMappingId after lambda binding', () => { + testDeprecated('contains eventSourceMappingId after lambda binding', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -250,7 +251,7 @@ describe('SQSEventSource', () => { }); - test('event source disabled', () => { + testDeprecated('event source disabled', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -269,7 +270,7 @@ describe('SQSEventSource', () => { }); - test('reportBatchItemFailures', () => { + testDeprecated('reportBatchItemFailures', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -286,7 +287,7 @@ describe('SQSEventSource', () => { }); }); - test('warning added if lambda function imported without role', () => { + testDeprecated('warning added if lambda function imported without role', () => { const app = new App(); const stack = new cdk.Stack(app); const fn = lambda.Function.fromFunctionName(stack, 'Handler', 'testFunction'); @@ -313,7 +314,7 @@ describe('SQSEventSource', () => { Template.fromStack(stack).resourceCountIs('AWS::IAM::Policy', 0); }); - test('policy added to imported function role', () => { + testDeprecated('policy added to imported function role', () => { // GIVEN const stack = new cdk.Stack(); const fn = lambda.Function.fromFunctionAttributes(stack, 'Handler', { @@ -395,7 +396,7 @@ describe('SQSEventSource', () => { }); }); - test('adding filter criteria', () => { + testDeprecated('adding filter criteria', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts index 054509a059f2c..acaed0fb21bee 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts @@ -84,7 +84,7 @@ export class FilterCriteria { this.filterCriteria = filter; } /** - * Returns a patter to filter criteria + * Returns a pattern to filter criteria * * @returns pattern object */ diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts index ebc1235a821db..2c5ce76313e53 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts @@ -366,7 +366,7 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp let consumerGroupConfig = props.kafkaConsumerGroupId ? { consumerGroupId: props.kafkaConsumerGroupId } : undefined; - let filters: Array<{[key: string]: string}> = []; + const filters: Array<{[key: string]: string}> = props.filters ?? []; if (props.filterCriteria) { for (let filter of props.filterCriteria) { filters.push(filter.toPattern()); @@ -391,7 +391,7 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp tumblingWindowInSeconds: props.tumblingWindow?.toSeconds(), sourceAccessConfigurations: props.sourceAccessConfigurations?.map((o) => {return { type: o.type.type, uri: o.uri };}), selfManagedEventSource, - filterCriteria: props.filterCriteria ? { filters: filters }: undefined, + filterCriteria: filters.length > 0 ? { filters: filters }: undefined, selfManagedKafkaEventSourceConfig: props.kafkaBootstrapServers ? consumerGroupConfig : undefined, amazonManagedKafkaEventSourceConfig: props.eventSourceArn ? consumerGroupConfig : undefined, }); diff --git a/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts b/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts index 061130fe163f0..dabc19a710d55 100644 --- a/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts @@ -1,4 +1,5 @@ import { Match, Template } from '@aws-cdk/assertions'; +import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import { Code, EventSourceMapping, Function, Runtime, Alias, StartingPosition, FilterRule, FilterCriteria } from '../lib'; @@ -212,6 +213,37 @@ describe('event source mapping', () => { }); }); + testDeprecated('deprecated filters with one pattern', () => { + const topicNameParam = new cdk.CfnParameter(stack, 'TopicNameParam', { + type: 'String', + }); + + let eventSourceArn = 'some-arn'; + + new EventSourceMapping(stack, 'test', { + target: fn, + eventSourceArn: eventSourceArn, + kafkaTopic: topicNameParam.valueAsString, + filters: [ + { + pattern: JSON.stringify({ + numericEquals: FilterRule.isEqual(1), + }), + }, + ], + }); + + Template.fromStack(stack).hasResourceProperties('AWS::Lambda::EventSourceMapping', { + FilterCriteria: { + Filters: [ + { + Pattern: '{"numericEquals":[{"numeric":["=",1]}]}', + }, + ], + }, + }); + }); + test('filter with one pattern', () => { const topicNameParam = new cdk.CfnParameter(stack, 'TopicNameParam', { type: 'String', From 297bb8fd2929d3c2436808043396e9dd1b1d1017 Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Mon, 26 Sep 2022 21:21:50 -0300 Subject: [PATCH 5/7] fix: remove deprecated tag and make some corrections --- .../aws-lambda-event-sources/lib/sqs.ts | 9 --- .../aws-lambda-event-sources/lib/stream.ts | 9 --- .../test/dynamo.test.ts | 70 +++++++++---------- .../integ.dynamodb-with-filter-criteria.ts | 6 +- .../test/integ.sqs-with-filter-criteria.ts | 6 +- .../test/kafka.test.ts | 29 ++++---- .../test/kinesis.test.ts | 17 +++-- .../aws-lambda-event-sources/test/sqs.test.ts | 29 ++++---- .../aws-lambda/lib/event-source-mapping.ts | 19 ++--- .../test/event-source-mapping.test.ts | 48 +++---------- 10 files changed, 88 insertions(+), 154 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts b/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts index 7e73fad9b70d1..105a7faf66766 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/lib/sqs.ts @@ -44,16 +44,8 @@ export interface SqsEventSourceProps { * Add filter criteria option * * @default - None - * @deprecated see `filterCriteria` */ readonly filters?: Array<{[key: string]: any}>; - - /** - * Add filter criteria option - * - * @default - none - */ - readonly filterCriteria?: Array; } /** @@ -89,7 +81,6 @@ export class SqsEventSource implements lambda.IEventSource { enabled: this.props.enabled, eventSourceArn: this.queue.queueArn, filters: this.props.filters, - filterCriteria: this.props.filterCriteria, }); this._eventSourceMappingId = eventSourceMapping.eventSourceMappingId; diff --git a/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts b/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts index 9d9a31b297e52..7a3a4036cfc55 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/lib/stream.ts @@ -114,16 +114,8 @@ export interface StreamEventSourceProps extends BaseStreamEventSourceProps { * Add filter criteria option * * @default - None - * @deprecated see `filterCriteria` */ readonly filters?: Array<{[key: string]: any}>; - - /** - * Add filter criteria option - * - * @default - none - */ - readonly filterCriteria?: Array; } /** @@ -150,7 +142,6 @@ export abstract class StreamEventSource implements lambda.IEventSource { tumblingWindow: this.props.tumblingWindow, enabled: this.props.enabled, filters: this.props.filters, - filterCriteria: this.props.filterCriteria, }; } } diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts index 65fe450a5453f..c1bed0b6e0ca9 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/dynamo.test.ts @@ -2,7 +2,6 @@ import { Template } from '@aws-cdk/assertions'; import * as dynamodb from '@aws-cdk/aws-dynamodb'; import * as lambda from '@aws-cdk/aws-lambda'; import * as sqs from '@aws-cdk/aws-sqs'; -import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import * as sources from '../lib'; import { TestFunction } from './test-function'; @@ -10,7 +9,7 @@ import { TestFunction } from './test-function'; /* eslint-disable quote-props */ describe('DynamoEventSource', () => { - testDeprecated('sufficiently complex example', () => { + test('sufficiently complex example', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -76,7 +75,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('specific tumblingWindow', () => { + test('specific tumblingWindow', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -103,7 +102,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('specific batch size', () => { + test('specific batch size', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -139,7 +138,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('pass validation if batchsize is token', () => { + test('pass validation if batchsize is token', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -223,7 +222,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('fails if batch size > 10000', () => { + test('fails if batch size > 10000', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -244,7 +243,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('adding filter criteria', () => { + test('adding filter criteria', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -256,8 +255,8 @@ describe('DynamoEventSource', () => { stream: dynamodb.StreamViewType.NEW_IMAGE, }); - const filters: Array = [ - lambda.FilterCriteria.filter({ + const filters = [ + { eventName: lambda.FilterRule.isEqual('INSERT'), dynamodb: { Keys: { @@ -266,16 +265,15 @@ describe('DynamoEventSource', () => { }, }, }, - }), + }, ]; expect(Array.isArray(filters)).toBe(true); - expect(filters[0]).toBeInstanceOf(lambda.FilterCriteria); // WHEN fn.addEventSource(new sources.DynamoEventSource(table, { startingPosition: lambda.StartingPosition.LATEST, - filterCriteria: filters, + filters: filters, })); // THEN @@ -300,7 +298,7 @@ describe('DynamoEventSource', () => { }); }); - testDeprecated('adding deprecated filters', () => { + test('adding filters', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -314,16 +312,14 @@ describe('DynamoEventSource', () => { const filters = [ { - pattern: JSON.stringify({ - eventName: lambda.FilterRule.isEqual('INSERT'), - dynamodb: { - Keys: { - id: { - S: lambda.FilterRule.exists(), - }, + eventName: lambda.FilterRule.isEqual('INSERT'), + dynamodb: { + Keys: { + id: { + S: lambda.FilterRule.exists(), }, }, - }), + }, }, ]; @@ -357,7 +353,7 @@ describe('DynamoEventSource', () => { }); }); - testDeprecated('specific maxBatchingWindow', () => { + test('specific maxBatchingWindow', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -393,7 +389,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('throws if maxBatchingWindow > 300 seconds', () => { + test('throws if maxBatchingWindow > 300 seconds', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -413,7 +409,7 @@ describe('DynamoEventSource', () => { }))).toThrow(/maxBatchingWindow cannot be over 300 seconds/); }); - testDeprecated('contains eventSourceMappingId after lambda binding', () => { + test('contains eventSourceMappingId after lambda binding', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -454,7 +450,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('specific retryAttempts', () => { + test('specific retryAttempts', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -488,7 +484,7 @@ describe('DynamoEventSource', () => { }); }); - testDeprecated('fails if retryAttempts < 0', () => { + test('fails if retryAttempts < 0', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -508,7 +504,7 @@ describe('DynamoEventSource', () => { }))).toThrow(/retryAttempts must be between 0 and 10000 inclusive, got -1/); }); - testDeprecated('fails if retryAttempts > 10000', () => { + test('fails if retryAttempts > 10000', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -530,7 +526,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('specific bisectBatchOnFunctionError', () => { + test('specific bisectBatchOnFunctionError', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -566,7 +562,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('specific parallelizationFactor', () => { + test('specific parallelizationFactor', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -600,7 +596,7 @@ describe('DynamoEventSource', () => { }); }); - testDeprecated('fails if parallelizationFactor < 1', () => { + test('fails if parallelizationFactor < 1', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -620,7 +616,7 @@ describe('DynamoEventSource', () => { }))).toThrow(/parallelizationFactor must be between 1 and 10 inclusive, got 0/); }); - testDeprecated('fails if parallelizationFactor > 10', () => { + test('fails if parallelizationFactor > 10', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -640,7 +636,7 @@ describe('DynamoEventSource', () => { }))).toThrow(/parallelizationFactor must be between 1 and 10 inclusive, got 11/); }); - testDeprecated('specific maxRecordAge', () => { + test('specific maxRecordAge', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -674,7 +670,7 @@ describe('DynamoEventSource', () => { }); }); - testDeprecated('fails if maxRecordAge < 60 seconds', () => { + test('fails if maxRecordAge < 60 seconds', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -696,7 +692,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('fails if maxRecordAge > 7 days', () => { + test('fails if maxRecordAge > 7 days', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -716,7 +712,7 @@ describe('DynamoEventSource', () => { }))).toThrow(/maxRecordAge must be between 60 seconds and 7 days inclusive/); }); - testDeprecated('specific destinationConfig', () => { + test('specific destinationConfig', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -761,7 +757,7 @@ describe('DynamoEventSource', () => { }); }); - testDeprecated('specific functionResponseTypes', () => { + test('specific functionResponseTypes', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -797,7 +793,7 @@ describe('DynamoEventSource', () => { }); - testDeprecated('event source disabled', () => { + test('event source disabled', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb-with-filter-criteria.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb-with-filter-criteria.ts index e0a7e6ede6ecb..e0433b5f8ba81 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb-with-filter-criteria.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb-with-filter-criteria.ts @@ -22,8 +22,8 @@ const table = new dynamodb.Table(stack, 'T', { fn.addEventSource(new DynamoEventSource(table, { batchSize: 5, startingPosition: lambda.StartingPosition.LATEST, - filterCriteria: [ - lambda.FilterCriteria.filter({ + filters: [ + { eventName: lambda.FilterRule.isEqual('INSERT'), dynamodb: { Keys: { @@ -32,7 +32,7 @@ fn.addEventSource(new DynamoEventSource(table, { }, }, }, - }), + }, ], })); diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/integ.sqs-with-filter-criteria.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/integ.sqs-with-filter-criteria.ts index 54f97bd8c9293..52ef1f6291c2d 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/integ.sqs-with-filter-criteria.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/integ.sqs-with-filter-criteria.ts @@ -14,12 +14,12 @@ const queue = new sqs.Queue(stack, 'Q'); fn.addEventSource(new SqsEventSource(queue, { batchSize: 5, - filterCriteria: [ - lambda.FilterCriteria.filter({ + filters: [ + { body: { id: lambda.FilterRule.exists(), }, - }), + }, ], })); diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts index 42e9ebdf84c67..27883b6849a19 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts @@ -2,14 +2,13 @@ import { Template, Match } from '@aws-cdk/assertions'; import { SecurityGroup, SubnetType, Vpc } from '@aws-cdk/aws-ec2'; import * as lambda from '@aws-cdk/aws-lambda'; import { Secret } from '@aws-cdk/aws-secretsmanager'; -import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import * as sources from '../lib'; import { TestFunction } from './test-function'; describe('KafkaEventSource', () => { describe('msk', () => { - testDeprecated('default', () => { + test('default', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -63,7 +62,7 @@ describe('KafkaEventSource', () => { }); - testDeprecated('with secret', () => { + test('with secret', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -139,7 +138,7 @@ describe('KafkaEventSource', () => { }); describe('self-managed kafka', () => { - testDeprecated('default', () => { + test('default', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -225,7 +224,7 @@ describe('KafkaEventSource', () => { }); describe('vpc', () => { - testDeprecated('correctly rendered in the stack', () => { + test('correctly rendered in the stack', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -283,7 +282,7 @@ describe('KafkaEventSource', () => { }); - testDeprecated('with secret', () => { + test('with secret', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -420,7 +419,7 @@ describe('KafkaEventSource', () => { }); }); - testDeprecated('using SCRAM-SHA-256', () => { + test('using SCRAM-SHA-256', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -455,7 +454,7 @@ describe('KafkaEventSource', () => { }); }); - testDeprecated('using BASIC_AUTH', () => { + test('using BASIC_AUTH', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -490,7 +489,7 @@ describe('KafkaEventSource', () => { }); }); - testDeprecated('using CLIENT_CERTIFICATE_TLS_AUTH', () => { + test('using CLIENT_CERTIFICATE_TLS_AUTH', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -525,7 +524,7 @@ describe('KafkaEventSource', () => { }); }); - testDeprecated('using CLIENT_CERTIFICATE_TLS_AUTH with rootCA', () => { + test('using CLIENT_CERTIFICATE_TLS_AUTH with rootCA', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -568,7 +567,7 @@ describe('KafkaEventSource', () => { }); }); - testDeprecated('with rootCA', () => { + test('with rootCA', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -602,7 +601,7 @@ describe('KafkaEventSource', () => { }); }); - testDeprecated('rootCACertificate can be ISecret', () => { + test('rootCACertificate can be ISecret', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -635,7 +634,7 @@ describe('KafkaEventSource', () => { }); }); - testDeprecated('consumerGroupId can be set for SelfManagedKafkaEventSource', () => { + test('consumerGroupId can be set for SelfManagedKafkaEventSource', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -662,7 +661,7 @@ describe('KafkaEventSource', () => { }); - testDeprecated('consumerGroupId can be set for ManagedKafkaEventSource', () => { + test('consumerGroupId can be set for ManagedKafkaEventSource', () => { // GIVEN const stack = new cdk.Stack(); @@ -691,7 +690,7 @@ describe('KafkaEventSource', () => { }); - testDeprecated('ManagedKafkaEventSource name conforms to construct id rules', () => { + test('ManagedKafkaEventSource name conforms to construct id rules', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/kinesis.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/kinesis.test.ts index 6d7d7d0d6a1b7..f42c0210786c8 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/kinesis.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/kinesis.test.ts @@ -1,7 +1,6 @@ import { Template } from '@aws-cdk/assertions'; import * as kinesis from '@aws-cdk/aws-kinesis'; import * as lambda from '@aws-cdk/aws-lambda'; -import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import * as sources from '../lib'; import { TestFunction } from './test-function'; @@ -9,7 +8,7 @@ import { TestFunction } from './test-function'; /* eslint-disable quote-props */ describe('KinesisEventSource', () => { - testDeprecated('sufficiently complex example', () => { + test('sufficiently complex example', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -78,7 +77,7 @@ describe('KinesisEventSource', () => { }); - testDeprecated('specific tumblingWindowInSeconds', () => { + test('specific tumblingWindowInSeconds', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -110,7 +109,7 @@ describe('KinesisEventSource', () => { }); - testDeprecated('specific batch size', () => { + test('specific batch size', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -170,7 +169,7 @@ describe('KinesisEventSource', () => { }); - testDeprecated('accepts if batch size is a token', () => { + test('accepts if batch size is a token', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -185,7 +184,7 @@ describe('KinesisEventSource', () => { }); - testDeprecated('specific maxBatchingWindow', () => { + test('specific maxBatchingWindow', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -215,7 +214,7 @@ describe('KinesisEventSource', () => { }); - testDeprecated('contains eventSourceMappingId after lambda binding', () => { + test('contains eventSourceMappingId after lambda binding', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -245,7 +244,7 @@ describe('KinesisEventSource', () => { }); - testDeprecated('event source disabled', () => { + test('event source disabled', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -265,7 +264,7 @@ describe('KinesisEventSource', () => { }); - testDeprecated('AT_TIMESTAMP starting position', () => { + test('AT_TIMESTAMP starting position', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); diff --git a/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts b/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts index 67162b3e899aa..6a5cf7a32c934 100644 --- a/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts +++ b/packages/@aws-cdk/aws-lambda-event-sources/test/sqs.test.ts @@ -2,7 +2,6 @@ import { Template } from '@aws-cdk/assertions'; import * as iam from '@aws-cdk/aws-iam'; import * as lambda from '@aws-cdk/aws-lambda'; import * as sqs from '@aws-cdk/aws-sqs'; -import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; import { App } from '@aws-cdk/core'; import * as sources from '../lib'; @@ -11,7 +10,7 @@ import { TestFunction } from './test-function'; /* eslint-disable quote-props */ describe('SQSEventSource', () => { - testDeprecated('defaults', () => { + test('defaults', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -60,7 +59,7 @@ describe('SQSEventSource', () => { }); - testDeprecated('specific batch size', () => { + test('specific batch size', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -88,7 +87,7 @@ describe('SQSEventSource', () => { }); - testDeprecated('unresolved batch size', () => { + test('unresolved batch size', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -140,7 +139,7 @@ describe('SQSEventSource', () => { }); - testDeprecated('batch size is > 10 and batch window is defined', () => { + test('batch size is > 10 and batch window is defined', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -176,7 +175,7 @@ describe('SQSEventSource', () => { }); - testDeprecated('specific batch window', () => { + test('specific batch window', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -225,7 +224,7 @@ describe('SQSEventSource', () => { }); - testDeprecated('contains eventSourceMappingId after lambda binding', () => { + test('contains eventSourceMappingId after lambda binding', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -251,7 +250,7 @@ describe('SQSEventSource', () => { }); - testDeprecated('event source disabled', () => { + test('event source disabled', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -270,7 +269,7 @@ describe('SQSEventSource', () => { }); - testDeprecated('reportBatchItemFailures', () => { + test('reportBatchItemFailures', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -287,7 +286,7 @@ describe('SQSEventSource', () => { }); }); - testDeprecated('warning added if lambda function imported without role', () => { + test('warning added if lambda function imported without role', () => { const app = new App(); const stack = new cdk.Stack(app); const fn = lambda.Function.fromFunctionName(stack, 'Handler', 'testFunction'); @@ -314,7 +313,7 @@ describe('SQSEventSource', () => { Template.fromStack(stack).resourceCountIs('AWS::IAM::Policy', 0); }); - testDeprecated('policy added to imported function role', () => { + test('policy added to imported function role', () => { // GIVEN const stack = new cdk.Stack(); const fn = lambda.Function.fromFunctionAttributes(stack, 'Handler', { @@ -396,7 +395,7 @@ describe('SQSEventSource', () => { }); }); - testDeprecated('adding filter criteria', () => { + test('adding filter criteria', () => { // GIVEN const stack = new cdk.Stack(); const fn = new TestFunction(stack, 'Fn'); @@ -404,12 +403,12 @@ describe('SQSEventSource', () => { // WHEN fn.addEventSource(new sources.SqsEventSource(q, { - filterCriteria: [ - lambda.FilterCriteria.filter({ + filters: [ + { body: { id: lambda.FilterRule.exists(), }, - }), + }, ], })); diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts index 2c5ce76313e53..cca99fdfcb6e9 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts @@ -239,17 +239,8 @@ export interface EventSourceMappingOptions { * @see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html * * @default - none - * @deprecated see `filterCriteria` */ readonly filters?: Array<{[key: string]: any}>; - - /** - * Add filter criteria to Event Source - * @see https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html - * - * @default - none - */ - readonly filterCriteria?: Array; } /** @@ -366,10 +357,10 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp let consumerGroupConfig = props.kafkaConsumerGroupId ? { consumerGroupId: props.kafkaConsumerGroupId } : undefined; - const filters: Array<{[key: string]: string}> = props.filters ?? []; - if (props.filterCriteria) { - for (let filter of props.filterCriteria) { - filters.push(filter.toPattern()); + const filterCriteria: Array<{[key: string]: string}> = []; + if (props.filters) { + for (const filter of props.filters) { + filterCriteria.push(FilterCriteria.filter(filter).toPattern()); } } @@ -391,7 +382,7 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp tumblingWindowInSeconds: props.tumblingWindow?.toSeconds(), sourceAccessConfigurations: props.sourceAccessConfigurations?.map((o) => {return { type: o.type.type, uri: o.uri };}), selfManagedEventSource, - filterCriteria: filters.length > 0 ? { filters: filters }: undefined, + filterCriteria: props.filters ? { filters: filterCriteria }: undefined, selfManagedKafkaEventSourceConfig: props.kafkaBootstrapServers ? consumerGroupConfig : undefined, amazonManagedKafkaEventSourceConfig: props.eventSourceArn ? consumerGroupConfig : undefined, }); diff --git a/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts b/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts index dabc19a710d55..7000c98e8378a 100644 --- a/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/event-source-mapping.test.ts @@ -1,7 +1,6 @@ import { Match, Template } from '@aws-cdk/assertions'; -import { testDeprecated } from '@aws-cdk/cdk-build-tools'; import * as cdk from '@aws-cdk/core'; -import { Code, EventSourceMapping, Function, Runtime, Alias, StartingPosition, FilterRule, FilterCriteria } from '../lib'; +import { Code, EventSourceMapping, Function, Runtime, Alias, StartingPosition, FilterRule } from '../lib'; let stack: cdk.Stack; let fn: Function; @@ -213,7 +212,7 @@ describe('event source mapping', () => { }); }); - testDeprecated('deprecated filters with one pattern', () => { + test('filters with one pattern', () => { const topicNameParam = new cdk.CfnParameter(stack, 'TopicNameParam', { type: 'String', }); @@ -226,39 +225,8 @@ describe('event source mapping', () => { kafkaTopic: topicNameParam.valueAsString, filters: [ { - pattern: JSON.stringify({ - numericEquals: FilterRule.isEqual(1), - }), - }, - ], - }); - - Template.fromStack(stack).hasResourceProperties('AWS::Lambda::EventSourceMapping', { - FilterCriteria: { - Filters: [ - { - Pattern: '{"numericEquals":[{"numeric":["=",1]}]}', - }, - ], - }, - }); - }); - - test('filter with one pattern', () => { - const topicNameParam = new cdk.CfnParameter(stack, 'TopicNameParam', { - type: 'String', - }); - - let eventSourceArn = 'some-arn'; - - new EventSourceMapping(stack, 'test', { - target: fn, - eventSourceArn: eventSourceArn, - kafkaTopic: topicNameParam.valueAsString, - filterCriteria: [ - FilterCriteria.filter({ numericEquals: FilterRule.isEqual(1), - }), + }, ], }); @@ -284,14 +252,14 @@ describe('event source mapping', () => { target: fn, eventSourceArn: eventSourceArn, kafkaTopic: topicNameParam.valueAsString, - filterCriteria: [ - FilterCriteria.filter({ + filters: [ + { orFilter: FilterRule.or('one', 'two'), stringEquals: FilterRule.isEqual('test'), - }), - FilterCriteria.filter({ + }, + { numericEquals: FilterRule.isEqual(1), - }), + }, ], }); From 9fa6a9081fe34264fb947972e1d989aa515b366d Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Tue, 27 Sep 2022 11:15:55 -0300 Subject: [PATCH 6/7] fix: deprecated filter and add addFilter to FilterCriteria class --- .../aws-lambda/lib/event-source-filter.ts | 16 +++++++++++----- .../aws-lambda/lib/event-source-mapping.ts | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts index acaed0fb21bee..6ac6b7df7d835 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts @@ -73,15 +73,21 @@ export class FilterRule { * Filter criteria for Lambda event filtering */ export class FilterCriteria { + + private constructor(private filterCriteria: {[key:string]: any}) {} + /** * Filter for event source + * @deprecated use `addFilter` */ - public static filter(filter: {[key:string]: any}): FilterCriteria { - return new FilterCriteria(filter); + public static filter(filter: {[key:string]: any}): {[key:string]: any} { + return { pattern: JSON.stringify(filter) }; } - private filterCriteria: {[key: string]: any}; - private constructor(filter: {[key:string]: any}) { - this.filterCriteria = filter; + /** + * Filter for event source + */ + public static addFilter(filter: {[key:string]: any}): FilterCriteria { + return new FilterCriteria(filter); } /** * Returns a pattern to filter criteria diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts index cca99fdfcb6e9..89d499a90bcc5 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts @@ -360,7 +360,7 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp const filterCriteria: Array<{[key: string]: string}> = []; if (props.filters) { for (const filter of props.filters) { - filterCriteria.push(FilterCriteria.filter(filter).toPattern()); + filterCriteria.push(FilterCriteria.addFilter(filter).toPattern()); } } From 3f602511aedc9f0e8379a9f1c54da3c8a6cfa32b Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Tue, 27 Sep 2022 19:20:28 -0300 Subject: [PATCH 7/7] chore: eslint correction --- packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts b/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts index 6ac6b7df7d835..8add21a8857bb 100644 --- a/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts +++ b/packages/@aws-cdk/aws-lambda/lib/event-source-filter.ts @@ -74,8 +74,6 @@ export class FilterRule { */ export class FilterCriteria { - private constructor(private filterCriteria: {[key:string]: any}) {} - /** * Filter for event source * @deprecated use `addFilter` @@ -89,6 +87,9 @@ export class FilterCriteria { public static addFilter(filter: {[key:string]: any}): FilterCriteria { return new FilterCriteria(filter); } + + private constructor(private filterCriteria: {[key:string]: any}) {} + /** * Returns a pattern to filter criteria *