aws-sns-subscriptions: add support for not exists filter in a subscription filter
Describe the feature
The existsFilter method of SubscriptionFilter class does not take any parameters and hard codes the value to true. I want to be able to set the value to false.
See: https://docs.aws.amazon.com/sns/latest/dg/attribute-key-matching.html
Use Case
I want to subscribe to messages on a topic that do not include the specified property.
Proposed Solution
In: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-sns/lib/subscription-filter.ts
Add a default parameter on the existsFilter method that has default value true for backwards compatibility.
/**
* Returns a subscription filter for attribute key matching.
*/
public static existsFilter(existsCondition: boolean = true) {
return new SubscriptionFilter([{ exists: existsCondition }]);
}
Other Information
No response
Acknowledgements
- [x] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
AWS CDK Library version (aws-cdk-lib)
2.198.0
AWS CDK CLI version
2.1018.0 (build e629e30)
Environment details (OS name and version, etc.)
WSL Ubuntu 22.04.5
Hi @jaw111,
Thank you for this well-documented feature request! This is indeed a valid enhancement that would expose AWS SNS's full capability for attribute key matching.
Your analysis is correct - the current existsFilter method hardcodes exists: true and doesn't expose the exists: false functionality that AWS SNS supports.
Your Proposed Solution: Your suggested implementation is excellent and follows good API design principles:
public static existsFilter(existsCondition: boolean = true) {
return new SubscriptionFilter([{ exists: existsCondition }]);
}
This approach:
- ✅ Maintains backward compatibility (default
true) - ✅ Enables the missing functionality (
falseoption) - ✅ Aligns with AWS SNS capabilities
- ✅ Follows existing CDK patterns
Implementation Notes:
- This would be a straightforward enhancement requiring minimal changes
- Should include unit tests for both
trueandfalsecases - Documentation should explain both use cases
- No breaking changes required
Since you indicated you may be able to implement this feature, we'd welcome a pull request! This would be a great community contribution that enhances the CDK's SNS subscription filtering capabilities.
Thanks again for the detailed issue and proposed solution! 🚀
@pahud whilst exploring other filter rule implementations in CDK, I found the Amazon.CDK.AWS.Lambda.FilterRule class has exists() and notExists() methods. So I was thinking to rework the approach here for consistency to add a notExistsCondition() method on the Amazon.CDK.AWS.SNS.SubscriptionFilter class, rather than change existsCondition() method.
Any guidance/preference on which approach to take?
Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.