Skip to main content

Creating and Subscribing to AWS SNS Topics

Hands-On Lab


Photo of

Training Architect





In this live AWS environment, you will be using the AWS Simple Notification Service (SNS). You will use it to create an SNS topic and then subscribe to that topic using multiple endpoints (SMS, Email, and AWS Lambda). This environment will allow you to demonstrate that you can interact effectively with the SNS service, through creating SNS topics and adding subscribers to those topics. At the end of this activity, you will have demonstrated you have a basic understanding of the SNS service, the components within it, and how to use the service in the AWS console.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating and Subscribing to AWS SNS Topics


Welcome, AWS voyager. It's time we sit down and have a talk about Simple Notification Services (SNS). We're going to walk through creating a topic, and then pushing it out to different subscribers.

Log into the AWS Management Console, using the credentials provided on the lab instructions page, and let's make sure we're using the us-east-1 region.

Create an SNS Topic

We'll start off by getting into the Simple Notification Service, and we can find that in the main Services menu of the AWS console under Application Integration. This isn't the SNS dashboard itself, more of a welcome screen. But we can type mytopic in a box and click a Create Topic button. Once we've done that, we just have to enter a Display name (we'll use MyTopic) and click the Create topic button down at the bottom of the screen.

Now, in the Subscriptions screen, click Create subscription. The Topic ARN should contain just our single arn:... in it. We want the Protocol set to Email, and then we'll put our own email address in the Endpoint box. Once we click the Create subscription button, we've got to then confirm the subscription. In order to do the confirming, we've got to check our email, and click on the Confirm subscription link that AWS sent.

Now, if we refresh the Subscriptions page, we'll see ours listed with a Confirmed status.

Let's create a new one. Go through the same steps, but this time set SMS as a Protocol, and put in a cell phone number as an Endpoint. There is no confirmation step to this, so we're good to go.

Create a Lambda Function

Let's create a Lambda function that we'll use to subscribe to our SNS topic as well, and process the notifications from that topic. Leave the SNS tab alone for now, and open up a new one. Up in the main Services menu, find Lambda under the Compute section. At the Lambda "welcome screen," click Create a function.

On the next screen, give the function a Name of SNSProcessor, a Runtime of Python 3.6, and use the existing role of LambdaRoleLA. Click the Create function button.

Back in the SNS tab, create a new subscription with a Protocol of AWS Lambda, and pick our SNSProcessor as an endpoint. Leave the Version or alias set to default. Now we can click Create subscription.

Back in the Lambda tab, click on SNSProcessor in the upper half of the screen, then scroll down so that we can see the raw code. You'll notice it's JSON. We've got a Python script up on GitHub that we're going to use instead.

In a third browser tab, navigate to, and we'll see a file sitting there. If we click on it, we can look at the code itself. Replace the code that's currently in our Lambda function with four lines from GitHub.

Hint: If we click on the Raw button above the code in Github, we'll get to a page with text we can copy, minus the line numbers.

Once we've pasted the new code in, we can click Save. We could test, but this code is so simple we're going to just keep moving.

Publish to the Topic

Back in the SNS page, click on Publish to topic, and get into where we can type up a message to send.

Our Topic ARN is already set to ours. Let's give this a Subject of Our first message. Set the Message format to JSON. Instead of typing in a message manually, click the JSON message generator button. Type Hello, this is our first message in the message box, and unselect all of the check boxes we don't need. The only check boxes that should be checked when we're done (out of the 16 or so there) will be Email, Lambda, and SMS.

Now click Generate JSON. Back out in the Publish window, we'll see some JSON code in the Message text area. In the interests of science, we may want to add - This is an email to the email line, - This is a Lambda function to the Lanbda line, and - This is a text message to the SMS line. Here's the email line:

"email": "Hello, this is our first message - This is an email",

Now when we click Publish message, the topic should go out three ways. We should get an email and a text. And if we head into the monitoring section of our Lambda function, we'll see the third message sitting there in the logs (by clicking the Jump to logs link). And each of them should have the proper - This is... message in it.


Well, we've finished. We learned a bit about SNS, and got to play with it. We could set up more subscriptions (different phone numbers, emails, etc) but now that we've got the hang of it, we can call this lab done. Congratulations.