Skip to content

Commit

Permalink
feat(AWS SQS): Support filterPatterns
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Dec 1, 2021
1 parent d52526b commit 3f0a80a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
19 changes: 19 additions & 0 deletions docs/providers/aws/events/sqs.md
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/providers/aws/events/streams.md
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions docs/providers/aws/guide/serverless.yml.md
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/plugins/aws/package/compile/events/sqs.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 1 addition & 6 deletions lib/plugins/aws/package/compile/events/stream.js
Expand Up @@ -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: [
Expand Down
6 changes: 6 additions & 0 deletions lib/plugins/aws/provider.js
Expand Up @@ -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: {
Expand Down
14 changes: 14 additions & 0 deletions test/unit/lib/plugins/aws/package/compile/events/sqs.test.js
Expand Up @@ -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] }],
},
},
],
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 3f0a80a

Please sign in to comment.