Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudformation - how to set filter policy of SNS subscription in code?

UPDATE: Cloudformation now supports SNS Topic Filters, so this question is not relevant anymore, no custom plugins or code is needed.

I am building a system with a number of SNS topics, and a number of Lambdas which are each reading messages from their assigned SQS queue. The SQS queues are subscribed to the SNS topics, but also have a filter policy so the messages will end up in the relevant SQS queues.

It works well when I set up the subscriptions in the AWS console.

Now I'm trying to do the same in my code, but the AWS Cloudformation documentation does not describe how to add a filter policy to a subscription. Based on the python examples here, I tried the following:

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

But then I get this error:

An error occurred: StopOperationSubscription - Encountered unsupported property FilterPolicy.

How can I set the filter policy that I need, using CloudFormation? And If that's not supported, what do you suggest as an alternative?

I want it to be set up automatically when I deploy my serverless app, with no manual steps required.

like image 456
Esben von Buchwald Avatar asked Jan 02 '23 04:01

Esben von Buchwald


1 Answers

Cloudformation just started to support FilterPolicy yesterday. I have been struggling for a while too :)

Syntax

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

Ref:

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/

like image 50
user3558541 Avatar answered Feb 03 '23 13:02

user3558541