aws-cdkamazon-sns

How do I define a message body filter policy for SNS in CDK?


Per the title, I want to know how to use CDK to define a payload filter policy for a message body in CDK?

I've tried to get this to work correctly in TypeScript, but am struggling to get it to compile correctly.


Solution

  • Examples for this are lacking but here is an example on how to define a OR policy that looks at the event field in the message body and only accepts messages where the value is either ASSIGNED or CANCELLED:

    myTopic.addSubscription(
      new SqsSubscription(myQueue, {
        filterPolicyWithMessageBody: {
          event: sns.FilterOrPolicy.filter(
            sns.SubscriptionFilter.stringFilter({
              allowlist: [
                'ASSIGNED',
                'CANCELLED',
              ],
            })
          ),
        },
      })
    )