Manage AWS SNS Topics using aws-cli

It is very easy to create SNS topics, subscriptions and perform other operations on SNS topics from the terminal using the aw-cli utility. In this article, I will show you the commands to create an SNS Topic, Subscription, confirm, and delete the Subscription using aws-cli. It is assumed that you are already familiar with AWS SNS Service.

To know about the operations that can be performed on SNS topics using command line utility aws-cli, visit the official documentation here.


  1. AWS Account  (Create if you don’t have one).
  2. Basic understanding of SNS Topics (Click here to learn to create an SNS Topic using Terraform).
  3. Basic understanding of SNS Subscription.
  4. AWS IAM user with AmazonSNSFullAccess policy attached to it and its access and secret keys (Click here to learn to create an IAM User).
  5. AWS CLI installed on your local machine.

What will we do?

  1. Check aws cli and export the AWS access & secret key on your local machine.
  2. Manage SNS Topics using aws cli.

Check aws cli and export aws access & secret key on your local machine.

If you don't have the aws-cli then refer to the official documentation here to install it on your local machine and then check the version of it using the following command. 

aws --version

If you execute the following command, you will get an error as you have not configured access to your AWS account in the terminal yet.

aws sts get-caller-identity

Export AWS IAM user access and secret keys on your terminal using the following commands.

export AWS_ACCESS_KEY_ID=<aws-user-access-key>
export AWS_SECRET_ACCESS_KEY=<aws-user-secret-key>

This time, you can check your identity by executing the following command. You should see user details in the output.

aws sts get-caller-identity

Manage SNS Topics using aws-cli

Let's check SNS Topics that we have in our current region.

aws sns list-topics

List SNS Topics

To simply create an SNS topic execute the following command. You can specify any other name if you want.

aws sns create-topic --name my-sns-topic-by-cli

While creating an SNS Topic, we can also specify tags to it. You can even specify multiple tags at a time.

aws sns create-topic --name my-sns-topic-by-cli-with-tag --tags Key=env,Value=test

List the topics and you can see the newly created topics this time.

aws sns list-topics

Create SNS Topics

You can also check the attributes of a particular SNS topic by specifying its arn. To get the arn of the topic, list the topics first and then copy the arn of the desired topic.

aws sns list-topics
aws sns get-topic-attributes --topic-arn arn:aws:sns:us-east-1:<your-account-number>:my-sns-topic-by-cli

Get Topic Attributes

You can check all the subscriptions in the current region using the following command.

aws sns list-subscriptions

To create a Subscription, you need to specify the protocol and arn of the SNS Topic. In this case, we will create a Subscription with Email as an endpoint. There are various endpoints supported by the subscription, you can specify the desired one.

aws sns subscribe --topic-arn arn:aws:sns:us-east-1:<your-account-number>:my-sns-topic-by-cli --protocol email --notification-endpoint <email-id>@<domain>
aws sns list-subscriptions

Create a Subscription

After the Subscription is created, you will receive a confirmation email. The subscription can be confirmed by clicking on the link specified in the email or a token can be used to confirm from the CLI. Copy the link address to retrieve the token from it, we will see the command to confirm the subscription from the terminal.

Subscription email

The confirmation link address will contain the token as highlighted below. Copy it to confirm from the CLI.

Subscription Token

To confirm the Subscription using the aws-cli, use the following command and specify the topic-arn and the token in the command.

aws sns list-subscriptions
aws sns confirm-subscription --topic-arn arn:aws:sns:us-east-1:<your-account-number>:my-sns-topic-by-cli --token 2336412f37fb687f5d51e6e2425e90ccf51427f70d347f743e373ed99c34222d699f93426571175db9bc0b81296ae69f53a889e1fbd11571c642308353d4648c67a2851b17a7f3cc1b7e1aaccae0881c5855df5d37bcae458305855d98fcd012d1ed4728a2064c6425b77b00e62b8d4fcf7fcccfb212d6850e3fa450c25cdf9a
aws sns list-subscriptions

Subscription can be deleted or unsubscribed simply by specifying the subscription arn. Once the Endpoint is unsubscribed, notifications will no longer reach the endpoint.

aws sns list-subscriptions
aws sns unsubscribe --subscription-arn arn:aws:sns:us-east-1:<your-account-number>:my-sns-topic-by-cli:cde0b12a-437c-49da-ab25-bc467701156d
aws sns list-subscriptions

Delete Subscription

Once you no longer need the topic, it is better to delete it. Be careful while deleting the topic as once deleted it can not be restored.

aws sns list-topics
aws sns delete-topic --topic-arn arn:aws:sns:us-east-1:<your-account-number>:my-sns-topic-by-cli
  614  aws sns list-topics

Delete SNS Topic


In this article, we saw the steps to create an SNS Topic, Subscription,  confirm Subscription. We also saw how the Subscription and the SNS topic can be deleted using the aws-cli command.

Share this page:

Suggested articles

0 Comment(s)

Add comment