How to delete the AWS SNS topic subscription using a script

From PheonixSolutions
Jump to navigation Jump to search

How to delete the AWS SNS topic subscription using a script

Step:1

 Install AWS CLI

Step:2

Configure AWS CLI

 aws configure

You will be prompted to enter your AWS Access Key ID, Secret Access Key, default region name, and default output format.

Step:3

Create Script File

 vi unsubscribe_all.sh

Step:4

Script Content:

 #!/bin/bash
 topic_arn="arn:aws:sns:ap-south-1:786712886076:nhf"
 # List subscriptions and extract ARNs
 subscription_arns=$(aws sns list-subscriptions-by-topic --topic-arn $topic_arn --query 'Subscriptions[].SubscriptionArn' --output text)
 # Loop through each subscription ARN and unsubscribe
 for subscription_arn in $subscription_arns; do
 echo "Unsubscribing $subscription_arn"
 aws sns unsubscribe --subscription-arn $subscription_arn
 done

Step:5

Make Script Executable

 chmod +x unsubscribe_all.sh

Step:6

Run Script

 ./unsubscribe_all.sh

By following these steps, you'll be able to run the script on your server and unsubscribe all the subscriptions from the specified SNS topic.