How to use Amazon Simple Notification Service (SNS)

how to use amazon sns

How to use (Amazon SNS)

On this post we will continue with the preparation for the AWS CERTIFIED SOLUTIONS ARCHITECT EXAM. Remember the exercises we are solving here are from AWS Certified Solutions Architect Official Study Guide: Associate Exam by Joe Baron and others. We are going to resolve the exercises from Chapter 8, Databases and AWS related to Amazon Simple Notification Service (Amazon SNS)

EXERCISE 8.1: Create an Amazon SNS Topic

In this exercise, you will create an Amazon SNS message.

Create a new topic, and use MyTopic for both the topic name and the display name.

aws sns create-topic --name MyTopic


After running the AWS CLI command, the output will be similar to the one below:

{
     "TopicArn": "arn:aws:sns:sa-east-1:772378070873:MyTopic"
 }

Congratulations! You have created your first topic. Make a note of the ARN, we will use it for the next step.

EXERCISE 8.2: Create a Subscription to Your Topic


In this exercise, you will create a subscription to the newly created topic using your email address. Then you confirm your email address.

Create the Subscription

aws sns subscribe --topic-arn arn:aws:sns:sa-east-1:772378070873:MyTopic --protocol email --notification-endpoint <your_email_address>


After running the command you will get an output similar to the following:

{
 "SubscriptionArn": "pending confirmation"
}


The service sends a confirmation email to your email address like the one below:

You have chosen to subscribe to the topic:
arn:aws:sns:sa-east-1:772378070873:MyTopic

To confirm this subscription, click or visit the link below (If this was in error no action is necessary):
Confirm subscription

Before this subscription can go live, you need to click on the link in the email that AWS sent you to confirm your email address. Check your email, and confirm your address (example below)

Congratulations! You have now confirmed your email address and created a subscription to a topic.

EXERCISE 8.3: Publish to a Topic

In this exercise, you will publish a message to your newly created topic.

Publish the message.

aws sns publish --topic-arn "arn:aws:sns:sa-east-1:772378070873:MyTopic" --message "This is a message published on topic MyTopic"


The output from the command will be the message ID

{
     "MessageId": "73c4fbc1-419f-54bd-9e1a-aee6a6617fa6"
}

You should receive an email from your topic name with the subject that you specified. If you do not receive this email, check your junk folder. The message should look like the one below:

Congratulations! In this exercise, you created a new topic, added a new subscription, and then published a message to your new topic. Delete your newly created topic and subscriptions after you are finished.