Accessing Amazon SNS in the AWS CLI - AWS Command Line Interface

Accessing Amazon SNS in the AWS CLI

You can access the features of Amazon Simple Notification Service (Amazon SNS) using the AWS Command Line Interface (AWS CLI). To list the AWS 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 AWS CLI.

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

Create a topic

To create a topic, use the sns create-topic command and specify the name to assign to the topic.

$ aws sns create-topic --name my-topic { "TopicArn": "arn:aws: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

To subscribe to a topic, use the sns subscribe command.

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

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

AWS 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: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: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

To send a message to all subscribers of a topic, use the sns publish command.

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

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

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

Unsubscribe from a topic

To unsubscribe from a topic and stop receiving messages published to that topic, use the sns unsubscribe command and specify the ARN of the topic you want to unsubscribe from.

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

To verify that you successfully unsubscribed, use the sns list-subscriptions command to confirm that the ARN no longer appears in the list.

$ aws sns list-subscriptions

Delete a topic

To delete a topic, run the sns delete-topic command.

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

To verify that AWS successfully deleted the topic, use the sns list-topics command to confirm that the topic no longer appears in the list.

$ aws sns list-topics