AWS Hands-On | SNS
๐ Notice
This is a hands-on tutorial accompanying the blog post:
โPass the AWS Certified Solutions Architect Associate Certification SAA-C03-(Episode 15: AWS Decoupling Applications)โ
๐น Episode 15: AWS Decoupling Applications Theory & Hands โ On & CDK
๐น Episode 15: AWS Cloud Practitioner Quiz
๐น Episode 15: AWS Solution Architect Preparation Quiz
๐นAWS SNS Theory โ Click Here
๐นAWS SNS Hands-On โ Click Here
๐นAWS SNS CDK โ Click Here (Will update soon)
Use Case 1 : Simple SNS
๐น Step 1: Create an SNS Topic
- Log in to the AWS Console, and go to the Simple Notification Service (SNS).
- Click on โTopicsโ in the left sidebar.
- Click โCreate topicโ.
- Choose the Topic type:
- Select Standard (high throughput, best-effort ordering, at-least-once delivery).
- You could also select FIFO if you need strict ordering and exactly-once delivery (requires .fifo suffix).
5. Enter a name for your topic โ e.g., MyFirstTopic.
6. Optional:
- Enable encryption using AWS KMS if needed.
- Access policy: Leave this as default unless you need cross-account or service-to-service access.
7. Click โCreate topicโ.
โ Done! Your topic is ready.
๐น Step 2: Create a Subscription (Using Email)
- In your newly created topic page, click โCreate subscriptionโ.
- Choose Protocol: Select Email.
- Enter Endpoint: Input the email address you want to subscribe (e.g.,
issackpaul95@gmail.com). - Optional:
- Subscription filter policy: This lets you filter messages that specific subscribers receive โ skip it for now.
5. Click โCreate subscriptionโ.
โ Your subscription is created, but itโs in โPending confirmationโ status.
๐น Step 3: Confirm the Subscription
- Open your email and access the inbox.
- Open the confirmation email from AWS SNS.
- Click โConfirm subscriptionโ.
๐ Go back to the AWS Console and refresh the subscription page. You should now see the status as โConfirmedโ.
๐น Step 4: Publish a Message
Now letโs send a message to test the SNS topic!
- In the MyFirstTopic page, click โPublish messageโ.
- Fill in:
- Subject: e.g.,
Test Notification - Message body: e.g.,
Hello, world!
3. Leave other options as default.
4. Click โPublish messageโ.
๐ก Within seconds, you should receive the message in the email inbox you subscribed.
๐น Step 5: Clean Up (Optional but Recommended)
Once done testing, itโs good practice to clean up:
- Delete the subscription:
- Go to your topic โ Subscriptions tab.
- Select the subscription and click โDeleteโ.
2. Delete the topic:
- Go back to Topics.
- Select
MyFirstTopic. - Click โDeleteโ and confirm by typing delete me.
โ Clean and tidy!
Use Case 2 : SNS + SQS โ Fan Out Pattern
This pattern allows you to broadcast a message to multiple SQS queues through a single SNS topic. Itโs great for microservices and loosely coupled systems.
โ What Youโll Build:
- One SNS topic
- Two (or more) SQS queues
- Both queues subscribed to the SNS topic
๐ธ Steps:
- Create SNS Topic
If not already created, repeat Use Case 1 from Part 1 (e.g.,MyFanoutTopic). - Create SQS Queues
- Go to Amazon SQS โ Create queue
- Choose Standard queue
- Name the queue (e.g.,
QueueA, then repeat forQueueB) - Leave settings as default and Create queues
- For Queue Hands-On โ Click Here
3. Subscribe Queues to SNS Topic
- Go back to your SNS topic (
MyFanoutTopic) โ Create subscription - Protocol: Amazon SQS
- Endpoint: Select ARN of
QueueA - Repeat for
QueueB
4. Confirm Subscription Permissions
SNS needs permission to publish to SQS:
- SNS sets this up automatically with access policies, but if it fails, manually edit the SQS access policy to allow the SNS topic ARN.
5. Publish a Test Message
- Go to
MyFanoutTopicโ Publish message - Enter
Hello Fan Out!and publish - Check
QueueAandQueueBโ both should receive the message
โ Success! Youโve implemented the SNS โ SQS fan-out pattern.
Use Case 3: Create an SNS FIFO Topic
FIFO (First-In-First-Out) topics guarantee strict message ordering and exactly-once delivery โ ideal for financial or transactional systems.
๐ธ Steps:
- Go to SNS โ Topics โ Create topic
- Select FIFO topic
- Name it (must end with
.fifo, e.g.,MyFIFOTopic.fifo) - Choose a Content-Based Deduplication option:
- ON = deduplication based on message body
- OFF = must provide
MessageDeduplicationIdon publish
5. Click Create topic
๐ง Note: Only SQS FIFO queues can subscribe to SNS FIFO topics.
Use Case 4: SNS FIFO + SQS FIFO โ Fan Out
Now letโs connect a FIFO SNS Topic to multiple FIFO SQS Queues for an ordered fan-out system.
๐ธ Steps:
- Create FIFO SQS Queues
- Go to SQS โ Create queue
- Type: FIFO
- Name: Must end with
.fifo, e.g.,Queue1.fifo,Queue2.fifo - Enable content-based deduplication if preferred
2. Create FIFO SNS Topic
- Repeat Use Case 3 above to create
MyFIFOTopic.fifo
3. Subscribe FIFO Queues
- Go to
MyFIFOTopic.fifoโ Create subscription - Protocol: Amazon SQS
- Endpoint: Use ARN of
Queue1.fifo, then repeat forQueue2.fifo
4. Publish a FIFO Message
- Go to
MyFIFOTopic.fifoโ Publish message - Provide:
- Message group ID (required, e.g.,
group1) - Message deduplication ID (if required)
- Body:
FIFO message test - Click Publish
๐ฏ Check the SQS FIFO queues โ the order and uniqueness will be preserved.
Use Case 5: SNS โ Message Filtering
With message filtering, subscribers only receive messages they care about, reducing noise and increasing efficiency.
โ What Youโll Build:
- One SNS topic
- Two SQS queues
- Subscriptions with filter policies (e.g., messages tagged by
eventType)
๐ธ Steps:
- Create SNS Topic (e.g.,
FilteredTopic)
2. Create Two SQS Queues (check previous steps): OrderQueue, ShippingQueue
3. Subscribe Both Queues to Topic
- Go to
FilteredTopicโ Create subscription - Protocol: Amazon SQS
- Select
OrderQueueARN - Click โCreate subscriptionโ
4. Add Filter Policy
- Click on the
OrderQueuesubscription - Choose Edit subscription filter policy
- Add filter:
{
"eventType": ["order"]
}- Repeat for
ShippingQueue:
{
"eventType": ["shipping"]
}5. Publish a Message with Attributes
- Go to
FilteredTopicโ Publish message - Enter a message body:
Order created - Add Message Attribute:
- Key:
eventType - Type:
String - Value:
order - Click Publish
๐งช Only OrderQueue should receive this message.
Repeat with eventType: shipping โ now only ShippingQueue should get it.
