Skip to content

Commit

Permalink
feat(AWS ActiveMQ): Support functions[].events[].filterPatterns (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyance-jain committed Jan 5, 2023
1 parent 5696f88 commit 1b55710
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
22 changes: 22 additions & 0 deletions docs/providers/aws/events/activemq.md
Expand Up @@ -72,6 +72,28 @@ functions:
basicAuthArn: arn:aws:secretsmanager:us-east-1:01234567890:secret:MySecret
```

## 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.

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 `activemqmsk` configuration (see an example below). The following example will only process records that are published in to Amazon MQ for Apache ActiveMQ where field `a` is equal to 1 or 2.

```yml
functions:
compute:
handler: handler.compute
events:
- activemq:
arn: arn:aws:mq:us-east-1:0000:broker:ExampleMQBroker:b-xxx-xxx
queue: queue-name
basicAuthArn: arn:aws:secretsmanager:us-east-1:01234567890:secret:MySecret
filterPatterns:
- value:
a: [1]
```

## 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/lambda/latest/dg/with-mq.html#events-mq-permissions) for more information about IAM Permissions for Amazon MQ events.
4 changes: 4 additions & 0 deletions docs/providers/aws/guide/serverless.yml.md
Expand Up @@ -1010,6 +1010,10 @@ functions:
startingPosition: LATEST
# (default: true)
enabled: false
# Optional, specifies event pattern content filtering
filterPatterns:
- value:
a: [1, 2]
```

### Kafka
Expand Down
20 changes: 18 additions & 2 deletions lib/plugins/aws/package/compile/events/activemq.js
Expand Up @@ -45,6 +45,7 @@ class AwsCompileActiveMQEvents {
queue: {
type: 'string',
},
filterPatterns: { $ref: '#/definitions/filterPatterns' },
},
additionalProperties: false,
required: ['basicAuthArn', 'arn', 'queue'],
Expand Down Expand Up @@ -91,8 +92,15 @@ class AwsCompileActiveMQEvents {
if (!event.activemq) return;

hasMQEvent = true;
const { basicAuthArn, arn, batchSize, maximumBatchingWindow, enabled, queue } =
event.activemq;
const {
basicAuthArn,
arn,
batchSize,
maximumBatchingWindow,
enabled,
queue,
filterPatterns,
} = event.activemq;

const mqEventLogicalId = this.provider.naming.getActiveMQEventLogicalId(
functionName,
Expand Down Expand Up @@ -131,6 +139,14 @@ class AwsCompileActiveMQEvents {
mqResource.Properties.Enabled = enabled;
}

if (filterPatterns) {
mqResource.Properties.FilterCriteria = {
Filters: filterPatterns.map((pattern) => ({
Pattern: JSON.stringify(pattern),
})),
};
}

brokerStatement.Resource.push(arn);
secretsManagerStatement.Resource.push(basicAuthArn);
cfTemplate.Resources[mqEventLogicalId] = mqResource;
Expand Down
16 changes: 16 additions & 0 deletions test/unit/lib/plugins/aws/package/compile/events/activemq.test.js
Expand Up @@ -14,6 +14,7 @@ describe('test/unit/lib/plugins/aws/package/compile/events/activemq.test.js', ()
const enabled = false;
const batchSize = 5000;
const maximumBatchingWindow = 20;
const filterPatterns = [{ value: { a: [1, 2] } }, { value: [3] }];

describe('when there are activemq events defined', () => {
let minimalEventSourceMappingResource;
Expand Down Expand Up @@ -47,6 +48,7 @@ describe('test/unit/lib/plugins/aws/package/compile/events/activemq.test.js', ()
batchSize,
maximumBatchingWindow,
enabled,
filterPatterns,
},
},
],
Expand Down Expand Up @@ -116,6 +118,20 @@ describe('test/unit/lib/plugins/aws/package/compile/events/activemq.test.js', ()
FunctionName: {
'Fn::GetAtt': [naming.getLambdaLogicalId('other'), 'Arn'],
},
FilterCriteria: {
Filters: [
{
Pattern: JSON.stringify({
value: { a: [1, 2] },
}),
},
{
Pattern: JSON.stringify({
value: [3],
}),
},
],
},
});
});

Expand Down

0 comments on commit 1b55710

Please sign in to comment.