简体   繁体   中英

boto3 SNS: Add a filter policy while subscribing to a topic

I built a Python API that subscribes people to an SNS topic to receive emails about their Spotify accounts. I have a single topic that I publish all the emails to.

I am aware that I can set a subscription filter policy when I create a subscription from console. However, boto3 needs the subscription ARN to set a filter, and the ARN needs the email to be confirmed.

How to set the filter when creating the subscription from boto3, like in the console?

EDIT

>>> import boto3
>>> sns = boto3.client('sns')
>>> sns.subscribe(TopicArn='{the_arn}', Protocol='EMAIL', Endpoint='{an_email}')
{'SubscriptionArn': 'pending confirmation', ...

According to boto3 sns documentation , in order to set the FilterPolicy attribute, I need the subscription ARN.

The example in the docs:

import boto3

sns = boto3.resource('sns')
subscription = sns.Subscription('arn')

response = subscription.set_attributes(
    AttributeName='FilterPolicy',
    AttributeValue={policy json}
)

I ended up replacing SNS with SQS and a Lambda function that sends the emails instead. The filtering is done inside the lambda, plus it sends a pretty text/html instead of the SNS json emails. Will keep the question open if someone comes up with an answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM