Cloudformation-如何在代码中设置SNS订阅的过滤策略?

时间:2018-07-04 22:21:09

标签: amazon-cloudformation amazon-sqs amazon-sns serverless-framework

更新:Cloudformation现在支持SNS主题过滤器,因此此问题不再相关,不需要自定义插件或代码。

我正在构建一个包含多个SNS主题和多个Lambda的系统,每个Lambda都从其分配的SQS队列中读取消息。 SQS队列订阅了SNS主题,但也具有过滤策略,因此消息将最终出现在相关的SQS队列中。

当我在AWS控制台中设置订阅时,它运行良好。

现在,我正在尝试在代码中执行相同的操作,但是AWS Cloudformation文档未描述如何向订阅添加过滤器策略。基于python examples here,我尝试了以下操作:

  StopOperationSubscription:
    Type: "AWS::SNS::Subscription"
    Properties:
      Protocol: sqs
      TopicArn: 
        Ref: StatusTopic
      Endpoint: 
        Fn::GetAtt: [StopActionQueue, Arn]
      FilterPolicy: '{"value": ["stop"]}'

但随后出现此错误:

An error occurred: StopOperationSubscription - Encountered unsupported property FilterPolicy.

如何使用CloudFormation设置所需的过滤器策略?如果不支持,那么您有什么建议呢?

我希望在部署无服务器应用程序时自动进行设置,而无需手动操作。

3 个答案:

答案 0 :(得分:4)

Cloudformation昨天才开始支持FilterPolicy。我也挣扎了一段时间:)

语法

JSON

{
  "Type" : "AWS::SNS::Subscription",
  "Properties" : {
    "DeliveryPolicy" : JSON object,
    "Endpoint" : String,
    "FilterPolicy" : JSON object,
    "Protocol" : String,
    "RawMessageDelivery" : Boolean,
    "Region" : String,
    "TopicArn" : String
  }
}

YAML

Type: "AWS::SNS::Subscription"
Properties:
  DeliveryPolicy: JSON object
  Endpoint: String
  FilterPolicy: JSON object
  Protocol: String
  RawMessageDelivery: Boolean,
  Region: String
  TopicArn: String

参考:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html#cfn-sns-subscription-filterpolicy

https://aws.amazon.com/blogs/compute/managing-amazon-sns-subscription-attributes-with-aws-cloudformation/

答案 1 :(得分:3)

我这样修复:

serverless.yml

plugins:
  - serverless-plugin-scripts
custom:
  scripts:
    commands:
      update-topic-filters: sls invoke local -f configureSubscriptions --path resources/lambdaTopicFilters.json
    hooks:
      before:deploy:finalize: sls update-topic-filters


functions:
  configureSubscriptions:
    handler: src/configurationLambdas/configureSubscriptions.main
    # Only invoked when deploying - therefore, no permissions or triggers are needed.

configureSubscriptions.js

import AWS from 'aws-sdk'

const nameFromArn = arn => arn.split(':').pop()
const lambdaNameFromArn = arn =>
  nameFromArn(arn)
    .split('-')
    .pop()

exports.main = async event => {
  const sns = new AWS.SNS({ apiVersion: '2010-03-31' })
  const params = {}
  const { Topics } = await sns.listTopics(params).promise()
  for (const { TopicArn } of Topics) {
    const topicName = nameFromArn(TopicArn)
    const filtersForTopic = event[topicName]
    if (!filtersForTopic) {
      continue
    }
    const { Subscriptions } = await sns.listSubscriptionsByTopic({ TopicArn }).promise()
    for (const { Protocol, Endpoint, SubscriptionArn } of Subscriptions) {
      if (Protocol === 'lambda') {
        const lambdaName = lambdaNameFromArn(Endpoint)
        const filterForLambda = filtersForTopic[lambdaName]
        if (!filterForLambda) {
          continue
        }

        const setPolicyParams = {
          AttributeName: 'FilterPolicy',
          SubscriptionArn,
          AttributeValue: JSON.stringify(filterForLambda),
        }
        await sns.setSubscriptionAttributes(setPolicyParams).promise()
        // eslint-disable-next-line no-console
        console.log('Subscription filters has been set')
      }
    }
  }
}

顶层是不同的主题名称,下一层是lambda名称,第三层是相关订阅的过滤策略:

lambdaTopicFilters.json

{
  "user-event": {
    "activateUser": {
      "valueType": ["status"],
      "value": ["awaiting_activation"]
    },
    "findActivities": {
      "messageType": ["event"],
      "value": ["awaiting_activity_data"],
      "valueType": ["status"]
    }
  },
  "system-event": {
    "startStopProcess": {
      "valueType": ["status"],
      "value": ["activated", "aborted", "limit_reached"]
    }
  }
}

答案 2 :(得分:1)

如果您使用的是无服务器,则现在支持本地的sns过滤器

functions:
  pets:
    handler: pets.handler
    events:
      - sns:
          topicName: pets
          filterPolicy:
            pet:
              - dog
              - cat

https://serverless.com/framework/docs/providers/aws/events/sns#setting-a-filter-policy