Sunday, 23 February 2020

Using Amazon SNS with the AWS CLI - Part 7

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 the AWS CLI.

Create a Topic

To create a topic, use the create-topic command and specify the name to assign to the topic.
$ aws sns create-topic --name my-topic
{
    "TopicArn": "arn:aws:sns:ap-south-1: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 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 sujeet@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 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:ap-south-1:123456789012:my-topic --message "Hello World!"
{
    "MessageId": "08ac5412-d867-5372-85f3-02de50785f63"

}
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 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 list-subscriptions command to confirm that the ARN no longer appears in the list.
$ aws sns list-subscriptions
{
    "Subscriptions": [
        {
            "SubscriptionArn": "arn:aws:sns:ap-south-1:123456789012:sns-t-sqs:a3f3e8b5-d82c-44f1-8321-d671e1d00058",
            "Owner": "123456789012",
            "Protocol": "sqs",
            "Endpoint": "arn:aws:sqs:ap-south-1:123456789012:sqs-t-sns",
            "TopicArn": "arn:aws:sns:ap-south-1:123456789012:sns-t-sqs"
        }
    ]
}

Delete a Topic

To delete a topic, run the 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 list-topics command to confirm that the topic no longer appears in the list.
$ aws sns list-topics
{
    "Topics": [
        {
            "TopicArn": "arn:aws:sns:ap-south-1:123456789012:dynamodb"
        },
        {
            "TopicArn": "arn:aws:sns:ap-south-1:123456789012:my-topic"
        }
    ]
}

No comments:

Post a Comment