diff --git a/docs/providers/aws/events/sqs.md b/docs/providers/aws/events/sqs.md index a19d9f8ce0b..40ba80713ce 100644 --- a/docs/providers/aws/events/sqs.md +++ b/docs/providers/aws/events/sqs.md @@ -69,6 +69,25 @@ functions: functionResponseType: ReportBatchItemFailures ``` +## Setting filter patterns + +This configuration allows customers to filter event before lambda invocation. It accepts up to 5 filter patterns by default and up to 10 with quota extension. If one event matches at least 1 pattern, lambda will process it. + +For more details and examples of filter patterns, please see the [AWS event filtering documentation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html) + +Note: Serverless only sets this property if you explicitly add it to the `sqs` configuration (see an example below). The following example will only process records where field `a` is equal to 1 or 2. + +```yml +functions: + onlyOneOrTwo: + handler: handler.preprocess + events: + - sqs: + arn: arn:aws:sqs:region:XXXXXX:myQueue + filterPatterns: + - a: [1, 2] +``` + ## IAM Permissions The Serverless Framework will automatically configure the most minimal set of IAM permissions for you. However you can still add additional permissions if you need to. Read the official [AWS documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-configure-lambda-function-trigger.html) for more information about IAM Permissions for SQS events. diff --git a/docs/providers/aws/events/streams.md b/docs/providers/aws/events/streams.md index b0c615e0d87..1a53ba41660 100644 --- a/docs/providers/aws/events/streams.md +++ b/docs/providers/aws/events/streams.md @@ -332,7 +332,7 @@ functions: ## Setting filter patterns -This configuration allows customers to filter event before lambda invocation. It accepts up to 5 filter criterion by default and up to 10 with quota extension. If one event matches at least 1 pattern, lambda will process it. +This configuration allows customers to filter event before lambda invocation. It accepts up to 5 filter patterns by default and up to 10 with quota extension. If one event matches at least 1 pattern, lambda will process it. For more details and examples of filter patterns, please see the [AWS event filtering documentation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html) diff --git a/docs/providers/aws/guide/serverless.yml.md b/docs/providers/aws/guide/serverless.yml.md index 6bd1506282c..a64afb501ee 100644 --- a/docs/providers/aws/guide/serverless.yml.md +++ b/docs/providers/aws/guide/serverless.yml.md @@ -451,6 +451,8 @@ functions: batchSize: 10 maximumBatchingWindow: 10 # optional, minimum is 0 and the maximum is 300 (seconds) enabled: true + filterPatterns: + - a: [ 1, 2 ] - stream: arn: arn:aws:kinesis:region:XXXXXX:stream/foo batchSize: 100 diff --git a/lib/plugins/aws/package/compile/events/sqs.js b/lib/plugins/aws/package/compile/events/sqs.js index 0caaed870da..4461fcda788 100644 --- a/lib/plugins/aws/package/compile/events/sqs.js +++ b/lib/plugins/aws/package/compile/events/sqs.js @@ -23,6 +23,7 @@ class AwsCompileSQSEvents { enabled: { type: 'boolean' }, maximumBatchingWindow: { type: 'integer', minimum: 0, maximum: 300 }, functionResponseType: { enum: ['ReportBatchItemFailures'] }, + filterPatterns: { $ref: '#/definitions/filterPatterns' }, }, required: ['arn'], additionalProperties: false, @@ -102,6 +103,14 @@ class AwsCompileSQSEvents { sqsTemplate.Properties.FunctionResponseTypes = [event.sqs.functionResponseType]; } + if (event.sqs.filterPatterns) { + sqsTemplate.Properties.FilterCriteria = { + Filters: event.sqs.filterPatterns.map((pattern) => ({ + Pattern: JSON.stringify(pattern), + })), + }; + } + // add event source ARNs to PolicyDocument statements sqsStatement.Resource.push(EventSourceArn); diff --git a/lib/plugins/aws/package/compile/events/stream.js b/lib/plugins/aws/package/compile/events/stream.js index 21377addd94..191cb9f3f9b 100644 --- a/lib/plugins/aws/package/compile/events/stream.js +++ b/lib/plugins/aws/package/compile/events/stream.js @@ -69,12 +69,7 @@ class AwsCompileStreamEvents { required: ['onFailure'], }, tumblingWindowInSeconds: { type: 'integer', minimum: 0, maximum: 900 }, - filterPatterns: { - type: 'array', - minItems: 1, - maxItems: 10, - items: { type: 'object' }, - }, + filterPatterns: { $ref: '#/definitions/filterPatterns' }, }, additionalProperties: false, anyOf: [ diff --git a/lib/plugins/aws/provider.js b/lib/plugins/aws/provider.js index 6b80f5ff98d..cd5e20b2412 100644 --- a/lib/plugins/aws/provider.js +++ b/lib/plugins/aws/provider.js @@ -660,6 +660,12 @@ class AwsProvider { pattern: '^\\d+\\.dkr\\.ecr\\.[a-z0-9-]+..amazonaws.com\\/([^@]+)|([^@:]+@sha256:[a-f0-9]{64})$', }, + filterPatterns: { + type: 'array', + minItems: 1, + maxItems: 10, + items: { type: 'object' }, + }, }, provider: { properties: { diff --git a/test/unit/lib/plugins/aws/package/compile/events/sqs.test.js b/test/unit/lib/plugins/aws/package/compile/events/sqs.test.js index 8cb3c5b6d4b..7525676e54d 100644 --- a/test/unit/lib/plugins/aws/package/compile/events/sqs.test.js +++ b/test/unit/lib/plugins/aws/package/compile/events/sqs.test.js @@ -611,6 +611,7 @@ describe('AwsCompileSQSEvents #2', () => { arn: 'arn:aws:sqs:region:account:MyQueue', batchSize: 10, maximumBatchingWindow: 100, + filterPatterns: [{ a: [1, 2] }, { b: [3, 4] }], }, }, ], @@ -642,6 +643,19 @@ describe('AwsCompileSQSEvents #2', () => { it('should have correct batching window size', () => { expect(eventSourceMappingResource.Properties.MaximumBatchingWindowInSeconds).to.equal(100); }); + + it('should have correct filtering patterns', () => { + expect(eventSourceMappingResource.Properties.FilterCriteria).to.deep.equal({ + Filters: [ + { + Pattern: JSON.stringify({ a: [1, 2] }), + }, + { + Pattern: JSON.stringify({ b: [3, 4] }), + }, + ], + }); + }); }); it('should not depend on default IAM role when custom role defined', async () => {