

**This documentation is for Version 1 of the Amazon CLI only.**

We announced the upcoming end-of-support for the Amazon CLI version 1. We recommend that you migrate to the Amazon CLI version 2. For dates, additional details, and information on how to migrate, see the [announcement](https://amazonaws-china.com/blogs/developer/cli-v1-maintenance-mode-announcement/). For documentation related to Version 2 of the Amazon CLI, see the [Version 2 User Guide](https://docs.amazonaws.cn/cli/latest/userguide/).

# Accessing Amazon SNS in the Amazon CLI
<a name="cli-services-sns"></a>

You can access the features of Amazon Simple Notification Service (Amazon SNS) using the Amazon Command Line Interface (Amazon CLI). To list the Amazon CLI commands for Amazon SNS, use the following command.

```
aws sns help
```

Before you run any commands, set your default credentials. For more information, see [Configuring settings for the Amazon CLI](cli-chap-configure.md).

This topic shows examples of Amazon CLI commands that perform common tasks for Amazon SNS.

**Topics**
+ [Create a topic](#cli-create-sns-topic)
+ [Subscribe to a topic](#cli-subscribe-sns-topic)
+ [Publish to a topic](#cli-publish-sns-topic)
+ [Unsubscribe from a topic](#cli-unsubscribe-sns-topic)
+ [Delete a topic](#cli-delete-sns-topic)

## Create a topic
<a name="cli-create-sns-topic"></a>

To create a topic, use the [https://docs.amazonaws.cn/cli/v1/reference/sns/create-topic.html](https://docs.amazonaws.cn/cli/v1/reference/sns/create-topic.html) command and specify the name to assign to the topic.

```
$ aws sns create-topic --name {{my-topic}}
{
    "TopicArn": "arn:aws-cn:sns:us-west-2:123456789012:my-topic"
}
```

Make a note of the response's `TopicArn`, which you use later to publish a message.

## Subscribe to a topic
<a name="cli-subscribe-sns-topic"></a>

To subscribe to a topic, use the [https://docs.amazonaws.cn/cli/v1/reference/sns/subscribe.html](https://docs.amazonaws.cn/cli/v1/reference/sns/subscribe.html) command. 

The following example specifies the `email` protocol and an email address for the `notification-endpoint`.

```
$ aws sns subscribe --topic-arn arn:aws-cn:sns:us-west-2:123456789012:{{my-topic}} --protocol {{email}} --notification-endpoint {{saanvi@example.com}}
{
    "SubscriptionArn": "pending confirmation"
}
```

Amazon immediately sends a confirmation message by email to the address you specified in the `subscribe` command. The email message has the following text.

```
You have chosen to subscribe to the topic:
arn:aws-cn:sns:us-west-2:123456789012:my-topic
To confirm this subscription, click or visit the following link (If this was in error no action is necessary):
Confirm subscription
```

After the recipient clicks the **Confirm subscription** link, the recipient's browser displays a notification message with information similar to the following.

```
Subscription confirmed!

You have subscribed saanvi@example.com to the topic:my-topic.

Your subscription's id is:
arn:aws-cn:sns:us-west-2:123456789012:my-topic:1328f057-de93-4c15-512e-8bb22EXAMPLE

If it was not your intention to subscribe, click here to unsubscribe.
```

## Publish to a topic
<a name="cli-publish-sns-topic"></a>

To send a message to all subscribers of a topic, use the [https://docs.amazonaws.cn/cli/v1/reference/sns/publish.html](https://docs.amazonaws.cn/cli/v1/reference/sns/publish.html) command. 

The following example sends the message "Hello World\!" to all subscribers of the specified topic.

```
$ aws sns publish --topic-arn {{arn:aws-cn:sns:us-west-2:123456789012:my-topic}} --message {{"Hello World!"}}
{
    "MessageId": "4e41661d-5eec-5ddf-8dab-2c867EXAMPLE"
}
```

In this example, Amazon sends an email message with the text "Hello World\!" to `saanvi@example.com`.

## Unsubscribe from a topic
<a name="cli-unsubscribe-sns-topic"></a>

To unsubscribe from a topic and stop receiving messages published to that topic, use the [https://docs.amazonaws.cn/cli/v1/reference/sns/unsubscribe.html](https://docs.amazonaws.cn/cli/v1/reference/sns/unsubscribe.html) command and specify the ARN of the topic you want to unsubscribe from.

```
$ aws sns unsubscribe --subscription-arn {{arn:aws-cn:sns:us-west-2:123456789012:my-topic:1328f057-de93-4c15-512e-8bb22EXAMPLE}}
```

To verify that you successfully unsubscribed, use the [https://docs.amazonaws.cn/cli/v1/reference/sns/list-subscriptions.html](https://docs.amazonaws.cn/cli/v1/reference/sns/list-subscriptions.html) command to confirm that the ARN no longer appears in the list.

```
$ aws sns list-subscriptions
```

## Delete a topic
<a name="cli-delete-sns-topic"></a>

To delete a topic, run the [https://docs.amazonaws.cn/cli/v1/reference/sns/delete-topic.html](https://docs.amazonaws.cn/cli/v1/reference/sns/delete-topic.html) command.

```
$ aws sns delete-topic --topic-arn arn:aws-cn:sns:us-west-2:123456789012:my-topic
```

To verify that Amazon successfully deleted the topic, use the [https://docs.amazonaws.cn/cli/v1/reference/sns/list-topics.html](https://docs.amazonaws.cn/cli/v1/reference/sns/list-topics.html) command to confirm that the topic no longer appears in the list.

```
$ aws sns list-topics
```