Skip to content

Commit

Permalink
feat(sns): add support for attribute key matching in message filtering (
Browse files Browse the repository at this point in the history
#3709)

* feat(sns): add support for attribute key matching in message filtering

SNS message filtering now supports attribute key matching. This feature lets you create an Amazon
SNS subscription filter policy that matches incoming messages which contain an attribute key,
regardless of the attribute value associated with this key. This feature lets you offload additional
message filtering logic to Amazon SNS.

Update `SubscriptionFilter` to support this new feature.

See https://aws.amazon.com/about-aws/whats-new/2019/08/amazon-sns-message-filtering-adds-support-for-attribute-key-matching/

* update README

* add new test for existsFilter
  • Loading branch information
jogold authored and mergify[bot] committed Sep 12, 2019
1 parent e9f46da commit dbf0134
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-sns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const fn = new lambda.Function(this, 'Function', ...);
// color: 'red' or 'orange' or begins with 'bl'
// size: anything but 'small' or 'medium'
// price: between 100 and 200 or greater than 300
// store: attribute must be present
topic.subscribeLambda(new subs.LambdaSubscription(fn, {
filterPolicy: {
color: sns.SubscriptionFilter.stringFilter({
Expand All @@ -69,7 +70,8 @@ topic.subscribeLambda(new subs.LambdaSubscription(fn, {
price: sns.SubscriptionFilter.numericFilter({
between: { start: 100, stop: 200 },
greaterThan: 300
})
}),
store: sns.SubscriptionFilter.existsFilter(),
}
}));
```
Expand Down
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-sns/lib/subscription-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,12 @@ export class SubscriptionFilter {
return new SubscriptionFilter(conditions);
}

/**
* Returns a subscription filter for attribute key matching.
*/
public static existsFilter() {
return new SubscriptionFilter([{ exists: true }]);
}

constructor(public readonly conditions: any[] = []) {}
}
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-sns/test/test.subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ export = {
test.done();
},

'with existsFilter'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const topic = new sns.Topic(stack, 'Topic');

// WHEN
new sns.Subscription(stack, 'Subscription', {
endpoint: 'endpoint',
filterPolicy: {
size: sns.SubscriptionFilter.existsFilter(),
},
protocol: sns.SubscriptionProtocol.LAMBDA,
topic
});

// THEN
expect(stack).to(haveResource('AWS::SNS::Subscription', {
FilterPolicy: {
size: [{ exists: true }]
},
}));
test.done();
},

'throws with raw delivery for protocol other than http, https or sqs'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit dbf0134

Please sign in to comment.