📖 Scenario: You are building a notification system where one message sent to a topic needs to be delivered to multiple queues for different processing tasks.This is a common pattern called fan-out, where a single message is copied and sent to multiple destinations.
🎯 Goal: Create an AWS CloudFormation template that sets up an SNS topic and two SQS queues. Then subscribe both queues to the SNS topic to enable fan-out messaging.
📋 What You'll Learn
Create an SNS topic named MyTopic
Create two SQS queues named QueueA and QueueB
Subscribe both QueueA and QueueB to MyTopic
Ensure the subscriptions use the correct protocol and reference the queue ARNs
💡 Why This Matters
🌍 Real World
Fan-out messaging is used in systems where one event triggers multiple independent processing tasks, such as sending notifications, updating databases, and logging.
💼 Career
Understanding SNS and SQS integration is essential for cloud architects and developers building scalable, decoupled event-driven systems on AWS.
Progress0 / 4 steps
1
Create the SNS topic
Create an SNS topic resource named MyTopic in the CloudFormation template.
AWS
Hint
Use AWS::SNS::Topic as the resource type and set TopicName to MyTopic.
2
Create two SQS queues
Add two SQS queue resources named QueueA and QueueB to the CloudFormation template.
AWS
Hint
Use AWS::SQS::Queue as the resource type and set QueueName to QueueA and QueueB respectively.
3
Subscribe QueueA to the SNS topic
Add a subscription resource named SubscriptionA that subscribes QueueA to MyTopic using the sqs protocol and the ARN of QueueA.
AWS
Hint
Use AWS::SNS::Subscription and set TopicArn to !Ref MyTopic, Protocol to sqs, and Endpoint to !GetAtt QueueA.Arn.
4
Subscribe QueueB to the SNS topic
Add a subscription resource named SubscriptionB that subscribes QueueB to MyTopic using the sqs protocol and the ARN of QueueB.
AWS
Hint
Use AWS::SNS::Subscription and set TopicArn to !Ref MyTopic, Protocol to sqs, and Endpoint to !GetAtt QueueB.Arn.
Practice
(1/5)
1. What is the main purpose of using SNS with multiple SQS queues in a fan-out pattern?
easy
A. To send the same message to multiple queues for parallel processing
B. To store messages permanently in one queue
C. To send messages only to one queue at a time
D. To delete messages immediately after sending
Solution
Step 1: Understand SNS role in fan-out
SNS publishes messages to multiple subscribers simultaneously.
Step 2: Understand SQS role in fan-out
Each SQS queue receives and holds messages independently for processing.
Final Answer:
To send the same message to multiple queues for parallel processing -> Option A
Quick Check:
SNS fan-out = multiple queues get same message [OK]
Hint: SNS fan-out means one message to many queues [OK]
Common Mistakes:
Thinking SNS sends to only one queue
Confusing message storage with deletion
Assuming messages are deleted immediately
2. Which of the following is the correct way to subscribe an SQS queue to an SNS topic using AWS CLI?
easy
A. aws sns subscribe --topic-arn --protocol sqs --notification-endpoint
B. aws sqs subscribe --topic-arn --queue-url
C. aws sns create-subscription --topic --queue
D. aws sns add-queue --topic-arn --queue-arn
Solution
Step 1: Identify correct AWS CLI command
The AWS CLI command to subscribe is 'aws sns subscribe'.
Step 2: Check parameters for SQS subscription
Use '--protocol sqs' and '--notification-endpoint' with the queue ARN.
Hint: Confirm all subscriptions to receive messages [OK]
Common Mistakes:
Assuming SNS sends to only one queue
Blaming message size without checking subscription
Thinking message deletion affects delivery
5. You want to build a system where an SNS topic sends messages to three SQS queues. One queue processes orders, another sends notifications, and the third logs events. How should you configure the system to ensure reliable processing and no message loss?
hard
A. Subscribe only the order queue to SNS and forward messages to others manually
B. Send messages directly to each SQS queue without SNS
C. Use one SQS queue for all processing and filter messages inside the consumer
D. Subscribe all three SQS queues to the SNS topic, enable dead-letter queues for each, and set appropriate message retention
Solution
Step 1: Use SNS fan-out to send messages to all queues
Subscribing all three SQS queues to the SNS topic ensures each gets the message.
Step 2: Enable dead-letter queues and set retention