When a SNS topic is deleted, subscriptions are not deleted automatically (bug?)

1

According to the documentation (https://docs.aws.amazon.com/sns/latest/dg/sns-delete-subscription-topic.html) when a topic is deleted, all associated subscriptions should be automatically deleted as well.

However this does not seem to be the case. We have an application using SNS for push notifications. When a topic is deleted, associated subscriptions are NOT deleted and must be deleted manually.

Here's a test program to reproduce the problem; in order to run this you must provide a valid AWS key id, secret, and region, and a valid SNS platform endpoint.

import time
import boto3

AWS_KEY_ID = "AKIAIOSFODNN7EXAMPLE"
AWS_KEY_SECRET = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
AWS_REGION = "eu-west-1"
ENDPOINT = "arn:aws:sns:eu-west-1:1234567890:endpoint/GCM/Application/12345678-abcd-9012-efgh-345678901234"

session = boto3.Session(
    aws_access_key_id=AWS_KEY_ID,
    aws_secret_access_key=AWS_KEY_SECRET,
    region_name=AWS_REGION,
)
sns_client = session.client("sns")

# Create topic, add subscription
topic_arn = sns_client.create_topic(Name="Test_bug")["TopicArn"]
subscription = sns_client.subscribe(
    TopicArn=topic_arn, Protocol="application", Endpoint=ENDPOINT
)["SubscriptionArn"]

# Delete topic
sns_client.delete_topic(TopicArn=topic_arn)
time.sleep(60)

# Check if subscription was deleted
try:
    sns_client.get_subscription_attributes(SubscriptionArn=subscription)
    print("Unexpected behaviour -- subscription still exists")
except sns_client.exceptions.NotFoundException:
    print("Expected behaviour -- subscription deleted")

Running this shows that the subscription is not deleted automatically when the topic is deleted.

Is this a bug in AWS ?

질문됨 9달 전294회 조회
2개 답변
0

Hi,

re:Post is not the right place to report bugs: AWS service teams usually do not monitor this site to get aware of those reports.

The right place to go is the AWS service console, which has features to open such tickets for any service. So, please, go there to announce this issue. Additionally, you will be able to assign it to the right priority level to raise more attention if needed.

Best,

Didier

profile pictureAWS
전문가
답변함 9달 전
  • "Technical support is unavailable under the Basic Support plan", so no luck.

  • If there is a way to submit a bug report other than creating a "support case" (which I apparently cannot do) please let me know..

0

Hello.

I have confirmed its operation as well.
Indeed, the subscriptions did not disappear, but remained.
The following shell script can be used to delete subscriptions not associated with SNS topics.

#!/bin/bash

# Get a list of SNS topics
topics=$(aws sns list-topics | jq -r '.Topics[].TopicArn')

# Get a list of SNS subscriptions
subscriptions=$(aws sns list-subscriptions)

# Remove subscriptions not tied to a topic
echo $subscriptions | jq -c '.Subscriptions[]' | while read subscription; do
  subscriptionArn=$(echo $subscription | jq -r '.SubscriptionArn')
  topicArn=$(echo $subscription | jq -r '.TopicArn')

  # If the topicArn of the subscription is not included in the topics, delete it.
  if ! echo $topics | grep $topicArn > /dev/null
  then
    aws sns unsubscribe --subscription-arn $subscriptionArn
    echo "delete it: $subscriptionArn"
  fi
done
profile picture
전문가
답변함 9달 전
profile pictureAWS
전문가
검토됨 9달 전
  • Note that aws sns list-subscriptions will only return a limited set of subscriptions (up to 100). Thus this script will not work as expected when there is a large number of subscriptions.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠