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

112 Views Asked by At

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.

1

There are 1 best solutions below

0
bjrnt On

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',
          ],
        })
      ),
    },
  })
)