Create an SNS Topic to Send SMS Messages

Hello fellow zombies!

Today we’re going to take a look at one of the services that AWS offers, the SNS service, or Simple Notification Service.  This service was one of the first I used as a developer and it holds a near and dear place in my undead heart.

The SNS service can be used to send a message to a group of subscribers.  The subscriber can be something like an email or SMS, where the message itself has value to the end user, or it can be an application program, where the message itself is processed by the program for further purposes.

The scenario: our zombie wants to have a way to notify other zombies when humans are nearby.  That way all the zombies can easily find humans to … hang out with, yeah.

For our demo today, we’re going use SNS to send an SMS message (a text message to a telephone number) to other zombies who have subscribed to his SNS Topic.  We’ll walk through the process of doing this with the AWS console.  Notifications can also be sent using a program, and we’ll explore that in a future post.

Sign in to AWS

To begin, go ahead and sign in to the AWS console.  Last time we talked about creating an IAM user to do this with, rather than using our root account.  It’s good to get in the habit of not using the root account any more than necessary.  I’ll be using the zombie user we created last time, “Cher”.

Select SNS from the list of services

You’ll find Simple Notification Service under the “Messaging” group of services.  Click on “Simple Notification Service”.

The Simple Notification Service Welcome Screen

One nice thing about AWS is they share a welcome screen with information about a service every time you navigate to a new one.  You’ll see this screen once normally, after that you’re usually directed to a dashboard.  From the welcome screen, click on “Get started” to go to the Dashboard.

At the dashboard, you’ll see we have a number of choices.  First off, we want to create a new Topic.  Topics are the things we publish a message to. Think of them as the name of our broadcast list. So you can have multiple topics, all for different purposes.

Create a New Topic

Click on “Create Topic” from the Common actions area of the screen.

A Topic name can be up to 256 characters long, and contains alphanumeric characters, dashes, or underscores.  It doesn’t contain spaces though.  In my case, I’m naming it “HumansNearby”, as our zombie is going to send SNS messages to his fellow zombies, the ones who subscribe to the HumansNearby topic.

The Display name needs to be MUCH shorter: it needs to be 10 characters or less, but can include spaces and other special characters.  This feels artificially short to me, but 256 characters would probably feel really long.

Note, for SMS messages, this will be prepended to any message you send.  For me, I’ll choose “HumansNear” for my Display name.

Go ahead and click “Create Topic”.

You’re now at the Topic details page.  This has a few things we want to take a look at.

The first is the Topic ARN.  An ARN is an “Amazon Resource Name”, and is a unique way to identify your resource.  The topic owner is your account number for AWS, and is incorporated into your ARN. Your ARN also indicates what service it is (“sns”) and the name of the service, to make it unique.

Let’s take a look at some of the actions under “Other topic actions”.

There’s a lot of choices under the topic actions actually, and we’ll go into each in future posts.  For now, feel free to change the topic display name if you don’t like your 10 character choice.  You can do so by clicking “Edit topic display name”.  Also you can delete a topic here if you want to get rid of a topic you’ve created.

Add a Subscription

Now, Topics by themselves don’t really do anything.  What a topic needs is some friends, someone or something to subscribe to the topic.  These are called “Subscriptions”.  You can have lots of subscriptions to your topic, and each subscription can do something different when it receives a notification message from the topic.

Let’s go ahead and create a subscription.  Click on the “Create Subscription” button.

You’ll see that your Topic ARN is pre-populated for you, awesome!

The protocol offers a lot of choices, let’s take a look at those.

Here are some of your choices.

  • HTTP
  • HTTPS
  • Email
  • Email-JSON
  • Amazon SQS
  • Application
  • AWS Lambda
  • SMS

Each of these is big enough to cover in a separate topic. For now, we will want to select the SMS choice.

Note that when you change the protocol, AWS will try to help you determine the right format for the protocol.  In our case, we want a phone number.

IMPORTANT NOTE: Make sure to use a “1-” at the start of your phone number.  You’ll see why this is important in the “Gotcha” section below.

 

 

Go ahead and type in one that belongs to you and click “Create subscription”.  You’ll see the following briefly appear (and disappear within 3 seconds).

AWS is telling us that it will verify that we have permission to send to that endpoint.  One might expect to see a message on your phone saying something like “Does this person have the right to subscribe you to the topic.”  One would think, at least.

I waited about 15 minutes after creating my subscription to get a confirmation request on my device.  Nothing.  So I logged into Skyrim and mined some iron ore, and checked my device again.  Nothing.  I wandered into the mountains and killed a dragon (after he killed me first, autosave is great).  I checked my device again, nada.

I decided to subscribe my “other” phone number, a Google voice number I have.  Silence. It was late, so I went to bed.  When I awoke, I checked my phone.  Nothing at all from AWS.

What happened?

Being a seasoned zombie programmer, I began to suspect the awful truth.  AWS lied to me. It’s okay, sometimes the documentation lies; sometimes the comments lie; sometimes the UI lies.  After drying away my undead tears, I accepted the truth, AWS was lying to me.  There would be no confirmation message to make sure I want to subscribe.

I logged back into AWS and decided to proceed as if it had sent me a confirmation. You’ll notice my topics are displayed below.

It looks like I have subscriptions, but of course it looked like that before I went to bed and before the dragon burned my Nord to a crisp.

The Test: Publish To Topic

I decided to click on the “Publish to topic” button and send myself a notification.

I clicked send, and voila!  I got a message almost immediately on my Google Voice/Hangouts notification:

You’ll see the Display Name for my topic prepended the message (with a trailing “>”).

Money Gotcha Alert!

While my Google voice number received the test message just fine, my main phone number didn’t get a message.  I checked the number I entered for my subscription, and I forgot to add the “1-” in front of the number. Because I didn’t include the “1” before the number, and my number starts with area code “404”, it sent the SMS message to Romania (which has a dialing code of “40”).  This means I was charged for the SMS by AWS, despite being on the free tier.  It was only $0.23, but I only sent two messages to that number.  So make sure you check the phone number you provide to your SMS subscription before sending a test message.

I changed the subscription number to include the “1” at the beginning, sent another test message, and my main phone phone got the message.  Looks like that’s a “gotcha”, and one that can cost you money, so make sure to add the leading “1-” before the number.

Summary

So today we learned how to create an SNS Topic and subscribe an SMS phone number to receive that topic.

We also learned how to accidentally send message to Romania.  Romania is the land of vampires, not zombies.  Please be careful about entering the phone number you use for an SMS message subscription – it should have a leading “1-” in front of the number.

I noticed the charge to Romania when I checked my billing.  I had been billed $0.23 for the SNS SMS messages I sent.  You may want to familiarize yourself with the pricing details for SNS.  I found the following to be of interest:

I would suggest taking some time to read through the pricing page.  I will be using emails instead of SMS for our testing going forward to avoid an accidental “gotcha” like this.

A final note: SMS messages are not MMS messages – they’re simple text and not multi-media.

Postscript

I contacted AWS Support to ask why I had been billed $0.23, before I figured out the reason being that I had sent my message to Romania.  AWS Support apologized and actually kindly credited my account $1.00 to cover the charge.  I didn’t ask for this, they just offered.  Wasn’t that nice?   See, AWS likes zombies too!