Created
April 3, 2020 03:09
-
-
Save hide32767/896be323ec27f44a8200cf922bd00108 to your computer and use it in GitHub Desktop.
Confirmation of Amazon SNS Subscription with disabled `Unsubscribe` Link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/path/to/your_python | |
# | |
# Confirmation of Amazon SNS Subscription with disabled `Unsubscribe` Link. | |
# cf. https://aws.amazon.com/premiumsupport/knowledge-center/prevent-unsubscribe-all-sns-topic/ | |
# | |
# Usage: ./confirm_sns_subs_with_disabled_unsubs_link.py <URL_to_confirm_SNS_Subscrption> | |
# | |
import sys | |
import boto3 | |
from urllib.parse import urlparse, parse_qs | |
url_to_confirm_sns_subs = sys.argv[1] | |
# To parse the URL and query. | |
# Notice: items parsed query are 'list' type. | |
# | |
# >>> parsed_query | |
# {'TopicArn': ['arn:aws:sns:region-name:012345678901:your-topic-name'], | |
# 'Token': ['<[0-9a-f]+>'], | |
# 'Endpoint': ['<e.g. mailaddr>']} | |
parsed_query = parse_qs(urlparse(url_to_confirm_sns_subs).query) | |
# To get region-name from ARN. | |
# cf. https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazonsns.html#amazonsns-resources-for-iam-policies | |
region = parsed_query['TopicArn'][0].split(':')[3] | |
# To initialize SNS Client with region-name | |
sns = boto3.client('sns', region_name=region) | |
# Confirmation | |
response = sns.confirm_subscription( | |
TopicArn=parsed_query['TopicArn'][0], | |
Token=parsed_query['Token'][0], | |
AuthenticateOnUnsubscribe='true' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment