What is an SNS Topic?

What is an AWS SNS Topic and what is it used for?

This article is party of my AWS Concept Definitions series. Check out more concepts by clicking here.

Definition

According to AWS, an SNS topic is “a logical access point that acts as a communication channel. A topic lets you group multiple endpoints (such as AWS Lambda, Amazon SQS, HTTP/S, or an email address).

This doesn’t tell us much, let me try to explain it better.

An SNS topic is an entity in AWS that allows publishers (usually topic owners) to publish messages to the topic, and subscribers (interest parties) to receive those messages.

An SNS topic is one to many. In other words, a message published (sometimes also known as broadcasted) to the SNS topic is delivered to each independent subscriber. Each subscriber is unaware of eachother, and they receive a copy of the original message.

This pattern is also called pubsub in distributed systems.

An SNS topic is an important way for a single service to share information with other services. This can be done by the subscriber using SQS, Lambda, or other methods to receive messages. Regardless, message delivery through SNS broadcasting is a powerful technique to asynchronously deliver information to multiple receivers.

Message receivers (also sometimes called subscribers) can register to start receiving messages from an SNS topic by subscribing. Subscribing can be done in one of two ways:

  1. The SNS topic owner can subscribe the receiver’s resource to the SNS Topic using the receiver’s ARN (Amazon Resource Name).
  2. The SNS topic owner can grant the receiver the ability to subscribe through an IAM permission (sns:subscribe), and the subscriber can subscribe themselves.

Example Usage

Consider a scenario where we are the owner of a Banking system. In our banking system, we wish to notify the fraud department of any suspicious transactions. More specifically, we are interested in alerting them to cash deposits over $10,000.

In this system, we might create a SNS topic called AccountTransactions. Whenever we handle a cash deposit, we will broadcast a message to this SNS topic that contains a payload like:

{
  "accountId": "..."
  "transactionType": "..." //DEPOSIT or WITHDRAW or TRANSFER
  "method": "..." //CASH or CREDIT
  "amount": "..."
}

The fraud department is interesting in receiving and processing these events. The department would then set up a subscriber to the SNS topic. This could either be a SQS queue (from which they will poll messages from), a Lambda function, an HTTP endpoint, or others.

The subscriber’s service will get invoked upon a message being broadcasted to the topic. From there, the subscriber can inspect the contents of the transaction. If it happens to be a DEPOSIT using a CASH method and the amount exceeds $10,000, we can flag the transaction as suspicious!

For more on SNS, check out these useful articles:

  1. What is AWS SNS? (with examples)
  2. AWS SQS vs SNS vs Eventbridge – When to Use What?
  3. AWS SNS to Lambda Step by Step Tutorial

You can also check out my YouTube video on the SNS service here.

Exit mobile version