如何在CDK中定义SNS的消息正文过滤策略?

问题描述 投票:0回答:2

根据标题,我想知道如何使用CDK为CDK中的消息正文定义负载过滤策略

我尝试让它在 TypeScript 中正常工作,但很难让它正确编译。

amazon-sns
2个回答
2
投票

缺少这方面的示例,但这里有一个示例,介绍如何定义 OR 策略,该策略查看消息正文中的

event
字段,并且仅接受值为
ASSIGNED
CANCELLED
的消息:

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

0
投票

为了通过 @bjrnt 响应来补充响应,如果您想使用有效负载过滤将 Lambda 订阅到 SNS 主题,此处 是一个很好的示例。

import * as lambda from 'aws-cdk-lib/aws-lambda';

const myTopic = new sns.Topic(this, 'MyTopic');
declare const fn: lambda.Function;

// Lambda should receive only message matching the following conditions on message body:
// color: 'red' or 'orange'
myTopic.addSubscription(new subscriptions.LambdaSubscription(fn, {
  filterPolicyWithMessageBody: {
    background: sns.FilterOrPolicy.policy({
      color: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.stringFilter({
        allowlist: ['red', 'orange'],
      })),
    }),
  },
}));
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.